Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create a dynamic array inside a structure?
Can I do the following inside a structure?
Int *p=(int*)malloc(sizeof(int));

What I have tried:

I have tried but it shows some error .
Posted
Updated 26-Aug-20 3:49am
Comments
Rick York 18-Aug-20 2:34am    
Try something more useful. Allocating a single integer is virtually never done because the pointer is the same size as the value in a 32-bit world and it is smaller in a 64-bit world. Try allocating an array of something or an entire structure. That will be much more realistic.

Reference: pointers - How to include a dynamic array INSIDE a struct in C? - Stack Overflow[^]
Putting what's there here for quick view:
C++
#include <stdlib.h>

struct my_struct {
    int n;
    char s[];
};

When you allocate space for this, you want to allocate the size of the struct plus the amount of space you want for the array:
C++
struct my_struct *s = malloc(sizeof(struct my_struct) + 50);

In this case, the flexible array member is an array of char, and sizeof(char)==1, so you don't need to multiply by its size, but just like any other malloc you'd need to if it was an array of some other type:
C++
struct dyn_array { 
    int size;
    int data[];
};

struct dyn_array* my_array = malloc(sizeof(struct dyn_array) + 100 * sizeof(int));

There are more discussions on web for it.
 
Share this answer
 
Comments
KarstenK 18-Aug-20 2:30am    
it is better to use a pointer, here cahr *s.
And best is to use the correct data type, because it helps other coders and the compiler. ;-)
jeron1 18-Aug-20 10:11am    
Agreed.
On the rare occasions I must use calloc and free I like to use two helpful macros :
C++
   // allocate memory of a given type - must use free to release it

#define AllocateMemory(count,type)   (type*)calloc(count,sizeof(type))

    // free an object and null it

#define ReleaseMemory(ptr)           { if(ptr) { free(ptr); ptr=nullptr; } }
I always use calloc because it zeros the memory also so it saves a step. I use the macros because I think they make the code more clear and eliminate a cast. Here is a sample of them in use - allocating an array of integers.
C++
int * integerArray = NULL;
integerArray = AllocateMemory( 1024, int );

// do something with the array here

ReleaseMemory( integerArray );
It is very important to always release any memory that you allocate. When you don't it results in a memory leak and those are a very bad thing. If you write software that has to run for months and months leaks can cause crashes and those are very, very bad.
 
Share this answer
 
Comments
CPallini 26-Aug-20 9:22am    
Hey Rick, C has no nullptr, yet.
As direct answer to your question, try
C
#include <stdio.h>
#include <stdlib.h>

struct Foo
{
  // other members here
  int * p;
};


int main()
{
  const size_t ITEMS = 10;

  struct Foo foo;

  foo.p = (int*) malloc( sizeof(int) * ITEMS);

  printf("%p\n", foo.p);

  size_t n;
  for (n = 0; n<ITEMS; ++n)
    foo.p[n]  = n*n;


  for (n = 0; n<ITEMS; ++n)
    printf("foo.p[%lu]=%d\n", n, foo.p[n]);

  free(foo.p);

  return 0;
}



Anyway, note that, as Sandeep Mewara suggested, the 'dynamic array' in the struct it is frequently accompained by another member indicating the size of the array itself. This is useful when passing the struct as argument to a function. For instance:
C
#include <stdio.h>
#include <stdlib.h>

struct DynArray
{
  size_t items;
  int *p;
};


void print_dynarray(struct DynArray da)
{
  size_t n;
  for (n = 0; n < da.items; ++n)
    printf("item[%lu] = %d\n", n, da.p[n]);
}

int main()
{

  const size_t ITEMS = 10;

  struct DynArray da;

  da.items = ITEMS;
  da.p = (int*) malloc( sizeof(int) * da.items);

  size_t n;
  for (n = 0; n<ITEMS; ++n)
    da.p[n]  = n*n;

  print_dynarray( da );

  free(da.p);
  da.items = 0;

  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900