Yes, the memory provided by malloc will not be in the structure. Only
the pointer to that memory will be in the structure. The size of a
struct is defined at compile time, so it can't be dynamically sized at
run time.

struct junk
{
  int size;
  int *data;
};

Somewhere in the code:

struct junk myJunk;
myJunk.size = n;
myJunk.data = (int *)malloc(n * sizeof(int));

for(i = 0; i < n; ++i)
  myJunk.data[i] = i;

That should work for values of n which your memory can support.
sizeof(struct junk) is eight bytes even if it contains a pointer to a
million integers.

Don

On Aug 24, 11:04 am, sagar pareek <[email protected]> wrote:
> See if we use dynamic memory allocation then still the size of pointer will
> be 4 bytes only....
> Mean that int* pointer still have the size equals to pointer ... malloc only
> returns new alloted memory which is now only  *pointed *by that pointer
>
> check this out :-http://www.ideone.com/20ayq
>
>
>
> On Wed, Aug 24, 2011 at 8:10 PM, Don <[email protected]> wrote:
> > If you are working in C++, stl has a vector container class which will
> > do this. Otherwise, declare an integer pointer in the struct and use
> > malloc to allocate memory for it. Then you can use it like an array.
> > Don
>
> > On Aug 23, 11:51 pm, Arun Vishwanathan <[email protected]> wrote:
> > > say that you have a structure with some fields of known size and unknown
> > > size.For example, a char, an integer and an integer array.I do not know
> > the
> > > size of the integer array until the user mentions the number of bytes he
> > > needs this integer array to cover in the command line as an argument.Is
> > it
> > > possible to have a structure with an integer dynamic array?
>
> > > Arun
>
> > > --
> > >  "People often say that motivation doesn't last. Well, neither does
> > bathing
> > > - that's why we recommend it daily."
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> **Regards
> SAGAR PAREEK
> COMPUTER SCIENCE AND ENGINEERING
> NIT ALLAHABAD

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to