0

I'm trying to allocate space for an array of n pointers to a struct named base in C. I don't want to allocate the space for a struct unless it is needed.

If more than n structs are required during a user session, then I'll realloc another set of n pointers.

Would you please tell me if this is the correct method of declaring them, excluding any reallocation?

One reason I'm asking is, that I don't understand why printf("%d", sizeof(ptr[0])) returns sizeof(base) before any memory has yet been allocated for an instance of base.

Is it simply because it's a pointer to base and will occupy that much space?

I just wanted to make sure that I'm not allocating the space for n structs of base before any are needed.

/* Global declaration */
struct base { ... };
struct base *ptr;

/* in main() */
ptr = calloc( n, sizeof ( char ) );

/* Function to return pointer to newly allocated struct base */
struct base *base_alloc( void )
{
    return ( struct base * ) malloc( sizeof ( struct base ) );
}

/* Within a function to create new base and populate it.*/
ptr[i] = base_alloc();
the busybee
  • 10,755
  • 3
  • 13
  • 30
Gary
  • 2,393
  • 12
  • 31
  • 1
    Does this answer your question?: _The sizeof() operator is calculated at compile time. The expressions are NOT evaluated. It is the type of the expression that is calculated_. From: https://stackoverflow.com/q/7788508/1606345 ? – David Ranieri Dec 27 '20 at 08:06
  • `sizeof x` gives you the amount of memory occupied by `x` – M.M Dec 27 '20 at 08:35
  • Why do you use the size of a `char` here: `...= calloc(n, sizeof( char ))`? – alk Dec 27 '20 at 09:38
  • @alk Because I was wrong and must've misunderstood the book example I was using. – Gary Dec 28 '20 at 03:57

1 Answers1

3

I will clear up a couple of things:

I don't understand why printf("%d", sizeof(ptr[0])) returns sizeof(base) before any memory has yet been allocated for an instance of base.

That is because sizeof evaluates the number of bytes occupied by an object of the type of an expression at compile time. E.g. here the expression ptr[0] has type struct base so sizeof returns the number of bytes needed to represent a struct base object. This is unrelated to memory allocation.

As for the rest of your code:

  • You want ptr to have type struct base **.
  • You also don't want to use calloc because NULL pointers are not guaranteed to actually have all bits set to zero.
  • Finally, there is no need to cast the value returned by malloc.

So in total:

/* Global declaration */
struct base { ... };
struct base **ptr;

/* in main() */
ptr = malloc( n * sizeof *ptr );
for (size_t i = 0; i < n; ++i)
{
  ptr[i] = NULL;
}

/* Function to return pointer to newly allocated struct base */
struct base *base_alloc( void )
{
  return malloc( sizeof ( struct base ) );
}

/* Within a function to create new base and populate it.*/
ptr[i] = base_alloc();
the busybee
  • 10,755
  • 3
  • 13
  • 30
Peter
  • 2,919
  • 1
  • 16
  • 35
  • There is no need to *cast* the value returned by malloc. – August Karlstrom Dec 27 '20 at 08:52
  • You know you can just suggest edits rather than leave comments, right? – Peter Dec 27 '20 at 08:53
  • @Peter, past a relatively low rep threshold, we just perform edits, and in fact *cannot* merely suggest them. I think I am not unusual in being very hesitant to do that with others' answers. Certainly one can easily observe that commenting instead is utterly routine. Besides, I am inclined to think that comments are more informative than edits -- but that is not to say that the answer should go unedited by its OP. – John Bollinger Dec 27 '20 at 12:24
  • Thank you. Would you mind explaining a bit why it's struct base **ptr rather than *ptr? – Gary Dec 27 '20 at 21:12
  • I learned this much anyway, it won't compile without the **ptr because it can't set the pointers to NULL. I'm having trouble understanding the difference between this and stack memory struct base *ptr[n]. – Gary Dec 28 '20 at 04:14
  • This link helped to understand thanks. https://dyclassroom.com/c/c-pointers-and-array-of-structures I was just being stupid. – Gary Dec 28 '20 at 04:20