I have a C Syntax 101 question.  Suppose I have a function which takes one kind of pointer as a argument and returns a different type of pointer, and both pointers point to the same data from within the calling function.  For example:  #include &lt;stdio.h&gt;  float* convertPtr(const int *a){   return (float*)a; }  int main() {   float *ptrF;   int x = 5;    ptrF = convertPtr(&amp;x);   printf("%d ::converted to::  %f\n", x, *ptrF);   return 0; }   Output of this program is:  5 ::converted to::  0.000000   So I'm a bit baffled by this.  The original data is created within main() and is not changed.  The convertPtr() function never alters the data, but should only change the type of the pointer.  I also note that when I step through the code on GDB, the pointer (the address itself) does not change:  Breakpoint 1, main () at exp.c:9 9         int x = 5; (gdb) p &amp;x $3 = (int *) 0x7fffffffe224 ...continue to after calling convertPtr()... (gdb) p ptrF $4 = (float *) 0x7fffffffe224 (gdb)   Yet the value pointed to that data has:  (gdb) p x $8 = 5 (gdb) p *(&amp;x)       // sanity check $9 = 5 (gdb) p *ptrF $10 = 7.00649232e-45 (gdb)   So... what the heck?  After my convertPtr() function, the pointer has been recast but the value of the pointer has not changed... yet dereferencing the converted pointer seems to be pointing to garbage data.  (I'm assuming.)  Does anyone see the issue here?  Supposed I HAD to fix my function; how do I do that?  Thanks...  -ROA  PS:  Apologies if this is a duplicate question.  I searched for about an hour for "C", "pointer", "casting", "change value", and the posts I pulled up were all over the map.  Sometimes its best to ask your question directly.
3271
When asking about , people sometimes refer to the strict aliasing rule. What are they talking about?
84232
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,  void deleteForMe(int* pointer) {      delete[] pointer; }   The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let's assume that we are indeed passing an array pointer,  int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; }   My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn't even know whether arr was dynamically created. Yet, if I do the following instead,  int main() {      int* num = new int(1);      deleteForMe(num);      return 0; }   The OS is smart enough to only delete one int and not go on some type of 'killing spree' by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string -- it will keep going until it hits 0).  So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the 'killing spree' scenario doesn't happen, so therefore in the practical world someone is remembering.)
63674
I am just now learning about function pointers and, as I was reading the K&amp;R chapter on the subject, the first thing that hit me was, "Hey, this is kinda like a closure." I knew this assumption is fundamentally wrong somehow and after a search online I didn't find really any analysis of this comparison.  So why are C-style function pointers fundamentally different from closures or lambdas? As far as I can tell it has to do with the fact that the function pointer still points to a defined (named) function as opposed to the practice of anonymously defining the function.  Why is passing a function to a function seen as more powerful in the second case, where it is unnamed, than the first where it is just a normal, everyday function that is being passed?  Please tell me how and why I am wrong to compare the two so closely.  Thanks.
10581
I have the following code:  typedef void VOID; int f(void); int g(VOID);   which compiles just fine in C (using gcc 4.3.2 on Fedora 10). The same code compiled as C++ gives me the following error:  void.c:3: error: ‘&lt;anonymous&gt;’ has incomplete type void.c:3: error: invalid use of ‘VOID’   Now, this is something in external library and I would like the owner to fix that problem. So I have a question - does C++ standard forbids this construct? Could you give me a pointer/citation? The only thing I can recall is that function declaration with (void) to signal empty parameter list is deprecated in C++, but I don't understand why typedefed VOID does not work.
82841
This seems like a pretty softball question, but I always have a hard time looking up this function because there seem there are so many variations regarding the referencing of char and tchar.
7396
Why the following example prints "0" and what must change for it to print "1" as I expected ?  #include &lt;iostream&gt; struct base {    virtual const int value() const {       return 0;    }    base() {       std::cout &lt;&lt; value() &lt;&lt; std::endl;    }    virtual ~base() {} };  struct derived : public base {    virtual const int value() const {       return 1;    } };  int main(void) {    derived example; }
64269
My friend produced a small proof-of-concept assembler that worked on x86. I decided to port it for x86_64 as well, but I immediately hit a problem.  I wrote a small piece of program in C, then compiled and objdumped the code. After that I inserted it to my python script, therefore the x86_64 code is correct:  from ctypes import cast, CFUNCTYPE, c_char_p, c_long  buffer = ''.join(map(chr, [ #0000000000000000 &lt;add&gt;:   0x55,                     # push   %rbp   0x48, 0x89, 0xe5,         # mov    %rsp,%rbp   0x48, 0x89, 0x7d, 0xf8,   # mov    %rdi,-0x8(%rbp)   0x48, 0x8b, 0x45, 0xf8,   # mov    -0x8(%rbp),%rax   0x48, 0x83, 0xc0, 0x0a,   # add    $0xa,%rax   0xc9,                     # leaveq    0xc3,                     # retq ]))  fptr = cast(c_char_p(buffer), CFUNCTYPE(c_long, c_long)) print fptr(1234)   Now, why does this script keeps doing segmentation fault whenever I run it?  I have yet a question about mprotect and no execution flag. It is said to protect against most basic security exploits like buffer overruns. But what is the real reason it's in use? You could just keep on writing until you hit the .text, then inject your instructions into a nice, PROT_EXEC -area. Unless, of course, you use a write protection in .text  But then, why have that PROT_EXEC everywhere anyway? Wouldn't it just help tremendously that your .text section is write protected?
680
First off, here is some code:  int main()  {     int days[] = {1,2,3,4,5};     int *ptr = days;     printf("%u\n", sizeof(days));     printf("%u\n", sizeof(ptr));      return 0; }   Is there a way to find out the size of the array that ptr is pointing to (instead of just giving its size, which is four bytes on a 32-bit system)?
89137
What would be better practice when giving a function the original variable to work with:  unsigned long x = 4;  void func1(unsigned long&amp; val) {      val = 5;             } func1(x);   or:  void func2(unsigned long* val) {      *val = 5; } func2(&amp;x);   IOW: Is there any reason to pick one over another?
30179
In C the following horror is valid:  myFunc() {   return 42;  // return type defaults to int. }   But, what about in C++? I can't find a reference to it either way...  My compiler (Codegear C++Builder 2007) currently accepts it without warning, but I've had comments that this is an error in C++.
5014
I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages.   Basically I have three questions:   Why use pointers over normal variables? When and where should I use pointers? How do you use pointers with arrays?
93044
What is better: void foo() or void foo(void)? With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?  Edit: I know some old compilers do weird things, but if I'm using just GCC, is void foo() Ok? Will foo(bar); then be accepted?
73942
Can someone explain to me how XOR swapping of two variables with no temp variable works?  void xorSwap (int *x, int *y) {     if (x != y) {         *x ^= *y;         *y ^= *x;         *x ^= *y;     } }   I understand WHAT it does, but can someone walk me through the logic of how it works?
59637
This seems like a simple question, but I can't find it with the Stack Overflow search or Google. What does a type followed by a _t mean? Such as  int_t anInt;   I see it a lot in C code meant to deal closely with hardware—I can't help but think that they're related.
45104
I am programming with normal pointers, but I have heard about libraries like Boost that implement smart pointers. I have also seen that in Ogre3D rendering engine there is a deep use of shared pointers.  What exactly is the difference between the three, and should I stick on using just a type of them?
83585
I think I've got it down to the most basic case:  int main(int argc, char ** argv) {   int * arr;    foo(arr);   printf("car[3]=%d\n",arr[3]);   free (arr);   return 1; }  void foo(int * arr) {   arr = (int*) malloc( sizeof(int)*25 );   arr[3] = 69; }   The output is this:  &gt; ./a.out   car[3]=-1869558540  a.out(4100) malloc: *** error for object 0x8fe01037: Non-aligned pointer                          being freed  *** set a breakpoint in malloc_error_break to debug &gt;   If anyone can shed light on where my understanding is failing, it'd be greatly appreciated.
55347
Quite often in ANSI C code I can see parenthesis sorrounding a single return value.   Like this:-  int foo(int x) {   if (x)     return (-1);   else     return (0); }   Why use () around the return value in those cases? Any ideas? I can see no reason for that.