Re: Pointer refering to an array of structutes?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:11943AB3-72E6-4A21-AB38-3B9974AF4156@xxxxxxxxxxxxx
Igor, will your code work on raw C language, I have checked my
compiler's guide and as you must already know, "new" is not a valid
command, I only have calloc and malloc!

Doesn't change much:

S* p = malloc(100 * sizeof(S));
f(p, 100);
free(p);

1) S arr[100]; >>>> you create an array of "S" structures called
arr, right?

Right.

2) If you may refresh my memory, what does this line do? S* p = new
S[100]; ---Does it create an array of pointers on the heap?

No, it allocates an array of one hundred S structures on the heap, and
makes p point to the first element of this array.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: [OT] C programming, variable size array
    ... > 'arr' has to be initialized during compilation, ... > it using malloc() and friends. ... About initializing the array as static, well I thought that way, when I ... know of malloc and realoc functoins. ...
    (Debian-User)
  • Re: efficeincy ( Am I correct ?)
    ... > calloc does the initialisation which is not achived in the malloc ... > to allocate the bytes (no need to initialise it) ... gcc calls a function called bzero to init array to 0. ...
    (comp.lang.c)
  • Re: Casting a generic or void pointer to point to a struct...
    ... that what I really need is known in C as a "dynamic array". ... with malloc() or calloc. ... would use to cast a gneeric or void pointer to a struct that I have ... memory obtained with the calloc() function. ...
    (comp.lang.c)
  • Re: Dynamic allocation of a 2 dimensional array
    ... MazeArray is not an array of pointers to pointers. ... malloc or calloc. ...
    (comp.lang.cpp)
  • Re: efficeincy ( Am I correct ?)
    ... >> Keith Thompson wrote: ... >>> As for malloc() vs. calloc, mallocis likely to be slightly ... >>> quicker than calloc() because callocinitializes the allocated space ... char *array; ...
    (comp.lang.c)