Paul brought this little gem to my attention. You'll need to be familiar with C to answer it. (He spent 2 hours fixing a bug which hinged on this issue.) So consider the following snippets of code:
int foo[256];
and
int *bar;
bar = malloc(256*sizeof(int));
foo and bar are functionally equivalent in most ways. You can use foo as a pointer, and you can use bar as an array. C doesn't even do array bounds checking.
One difference between the two is that the memory allocated for foo gets automatically released at the end of the function, whereas you'll have to manually free the memory used by bar.
But when you go about using them, there is another crucial difference. What is it?
Post your answer in the comments. I'll post it myself in a few days if no one gets the answer I have in mind by then. (For the record, when Paul asked me this, I gave him the first difference above, but I couldn't think of the second difference.)
Comments (8)
One difference is that you can make bar point somewhere else, but you can't change where foo points. (i.e. bar = foo is legal, but foo = bar is not.) I think this is caught by the compiler, so I don't see how it would cause a bug. I'll continue to think about it...
Posted by Matt | July 2, 2003
Yeah. Good point. But Paul's problem gave a runtime error.
Posted by ToastyKen | July 2, 2003
Okay, so the problem Paul had was that, although you can use foo in pointer arithmetic and bar in array references, they are conceptually different in memory. If you run sizeof(foo), you get 256*sizeof(int). But if you run sizeof(bar), you get sizeof(int*), which is probably just sizeof(int). Big difference!
Posted by ToastyKen | July 8, 2003
The correct code for me:
int *bar;
bar = (int *) malloc(256*sizeof(int));
Without int it gives error.
Posted by Mike | October 18, 2008
Well that makes sense as pointers are just variables that hold a memory address. So compare an address value with an integer value and you will see the differences in size.
Posted by Johan | October 24, 2010
Four problems:
1) Don't cast malloc (unless C++).
2) int != *int
They might be similar in size now, but on 64 bit, no way.
So, please don't (int*)malloc(sizeof (int)) it just paints over rust in old, crufty code.
3) Malloc may return NULL. Always check for this.
if (!(bar=malloc(256*sizeof bar))) ERROR("out of memory");
4) Do not listen to the likes of me. Use the recommended manuals and run psychological self-tests if thinking of getting into C programming professionally. There is already enough bad code and faulty advice causing widespread damage and break-ins to systems.
Posted by Hellork | October 30, 2010
Oops, sizeof int is fine. The error is probably because was not included. This could lead to problems with the compiler using the wrong or different function for malloc which would be hidden by the cast to (int*). Anyway... be careful out there this Halloween and don't let undefined ghosts into your machine!
Posted by Hellork | October 30, 2010
I meant to say because stdlib.h was not included. It seems every time I post, a word gets taken out here and there. :confused:
Posted by Hellork | October 30, 2010