1

I am by no means a master of the C language, nor am i going to claim to be. But I was pretty sure I understood pointers until I came across these two very different implementations that the author claimed would have the same outcome. Im just not seeing it. Can somebody please explain to me what each of these are doing?

int (*AA)[4];
AA = malloc(sizeof(int[4])*size);

and the 2nd one:

int *BB;
BB = (int *)malloc(size*sizeof(int));

From my current understanding, If i wanted to make the 2nd one in c++, it is equivalent to:

int *CC;
CC=new int[size]

Is this assumption Correct?

2 Answers2

3

First part: int (*AA)[4]; defines AA to be a pointer to an int[4].

AA = malloc(sizeof(int[4])*size); allocates storage for size int[4]s

Second part: int *BB; defines BB to be a pointer to an int.

BB = (int *)malloc(size*sizeof(int)); allocates storage for size ints, then unnecessarily casts the result, thereby introducing a potential error you won't get warned about (namely, if the prototype for malloc() is not in scope).

Therefore, the result is arguably different

fstd
  • 574
  • 2
  • 7
0

comments notwithstanding, Given your code:

enter image description here

For illustration, here is what is shown when in debug mode looking at what was created: (ANSI C99 compiler, 32bit build)

At the very least, understanding fully what your author meant when stating same outcome is important.
Also illustrates why the use of calloc() as opposed to malloc() might be a good option (C99).

The second part question: C++ version

int* BB = new int[size];  

is valid.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • In C99, main's return can be omitted; calloc is entirely unrelated (especially looking at the copy-on-write semantics common implementations use with calloc()'ed buffers); does your answer imply that the storage pointed to by AA is in fact only 4 ints large? Finally, I'm not aware of an ANSI C99 compiler for windows -- which one is it? – fstd May 06 '14 at 13:34
  • @fstd - ***First***, my answer is not intended to stand in disagreement with any other. Its intent is to _illustrate_ what memory looked like when OP variables compiled and run. (as it does) ***Second***, agree'd, comment on `calloc()`-`malloc()` is not related to main thrust of OP. Just an observation (preference really) on my part that space provided my `malloc()` _always_ contains unwanted values, as such, I typically caution people of that, and suggest `calloc()` as an alternative. (see next comment for remainder) – ryyker May 06 '14 at 15:02
  • @fstd - ***third*** _does your answer imply...?_ As I mentioned, it _illustrates_ what memory looks like for the two variable arrays after compile and run. ***Forth*** ***[CLANG](http://en.wikipedia.org/wiki/C99)*** - which supports all C99 features, except floating point pragmas. – ryyker May 06 '14 at 15:03
  • First, I didn't say it would. Second, that's kind of what i was getting at - calloc() is /not/ the same as malloc()+memset()-to-zero (therefore i would avoid suggesting people to use it as such); Third, then I don't understand what we're looking at, in that image. Could you please explain? – fstd May 06 '14 at 16:48
  • @fstd - Two screen shots, 1) the execution being stopped just after all calls to allocate memory in my editor. 2) A view of size (== 4) AA located at 0x024A0080 & BB located at 0x024A00D0, both created on the heap using malloc(). ***Regarding*** the `malloc() - Calloc comment you made, it interested me so I did some reading, including ***[HERE](http://stackoverflow.com/questions/1538420/difference-between-malloc-and-calloc)***. AndreyT's comment in particular mirrors your concern. Thanks for bringing it up, I am a convert. – ryyker May 06 '14 at 18:45
  • actually it's @AshishChavan's and @SnappleLVR's answers which `mirror my concern'. Sickening to see wrong answers upvoted way above 100... – fstd May 06 '14 at 20:05
  • @fstd in Standard C, calloc is the same as malloc+memset. The OP asked about "C language" and did not put any OS-specific tags. If a particular platform has other differences then it's good to mention them but you should also clearly state that you are referring to a particular platform (and, if possible, also list which other platforms have the same behaviour). – M.M May 07 '14 at 02:02