RE: [Dev-C++] dynamic allocation and structs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-10-24 20:29:23
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Scott Williams
> Sent: Thursday, October 24, 2002 3:18 PM
> To: dev...@li...
> Subject: [Dev-C++] dynamic allocation and structs
>=20
>=20
> Hi,
>=20
> I'm working in C (no C++ please). Got a couple of questions=20
> related to
> dynamic allocation and in particular, dynamically allocating=20
> an array of
> structs.
>=20
> First, what is the duration of memory allocated by malloc (etc.)? In
> particular, I want to dynamically allocate/de-allocate several global
> variables within a single subroutine called from main and so=20
> want to be sure
> that the allocated memory exists beyond the scope of that subroutine.
>=20
> Second, it is my understanding that it is perfectly=20
> acceptable to have an
> array of structs. E.g.,
>=20
> struct mystruct {
> double var1;
> long var2;
> etc...
> }
>=20
> . . .
>=20
> struct mystruct data[100];
>=20
> Is this correct? If so, what then is the calling sequence to=20
> dynamically
> allocate an array of structs, and, in particularly, make the result
> available globally?
>=20
> Thanks for your help (& sorry if this is naive).
Yes you can have an array of structs, either in automatic storage like
your example above, or on the free store. Objects on the free store
exist till you free() that memory while your program is running. If you
do not free() that memory and lose its address (in some point you have
no pointer pointing to it), then you have a memory leak.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|