C Dynamic allocation of Pointers 
Author Message
 C Dynamic allocation of Pointers

Hello,
    I know this is not the right group for this but I can't seem to find the
right C group.

So

I want to allocate an array of int dynamically.  This is what I got now

DBINT tDBINT[1000];

what I want to do is something like this

NumberofElements = 200;
DBINT tDBINT[NumberofElements];

this of doesn't work.

Can anyone help me with the C code to do this?

My next question is.  If I allocate memory using malloc,  do you have to use
the free function or will it free up by itself?

regards
Greg Obleshchuk



Tue, 20 Jan 2004 09:27:03 GMT  
 C Dynamic allocation of Pointers
Hi Greg,

1) In your code, declaration of this array on the stack will require the
variable "NumberofElements" to be of a constant type. Only "work around" is
to use dynamic memory allocation. So, if you simply change the variable
"NumberofElements" to be of type "const UINT or const INT", depending on
your size requirements, your code will work fine...

2) malloc() does NOT automatically free up the memory. use free() to free
the memory block.

3) Have you seen the Hungarian naming convention??? It works for C too : )

Take care,
-Moe Khosravy


Quote:
> Hello,
>     I know this is not the right group for this but I can't seem to find
the
> right C group.

> So

> I want to allocate an array of int dynamically.  This is what I got now

> DBINT tDBINT[1000];

> what I want to do is something like this

> NumberofElements = 200;
> DBINT tDBINT[NumberofElements];

> this of doesn't work.

> Can anyone help me with the C code to do this?

> My next question is.  If I allocate memory using malloc,  do you have to
use
> the free function or will it free up by itself?

> regards
> Greg Obleshchuk



Tue, 20 Jan 2004 12:33:50 GMT  
 C Dynamic allocation of Pointers
Moe,
    Sorry I didn't make the example as clear as I should have.  The variable
NumberofElements is set by a function that returns the number of columns in
a database.  It may be 5 or it may be any number.
So I can not set it to a constant.  It is changed in my program in several
places.  I want to use dynamic allocation but I can't seem to define it
right.  Could you help?

regards
Greg


Quote:
> Hi Greg,

> 1) In your code, declaration of this array on the stack will require the
> variable "NumberofElements" to be of a constant type. Only "work around"
is
> to use dynamic memory allocation. So, if you simply change the variable
> "NumberofElements" to be of type "const UINT or const INT", depending on
> your size requirements, your code will work fine...

> 2) malloc() does NOT automatically free up the memory. use free() to free
> the memory block.

> 3) Have you seen the Hungarian naming convention??? It works for C too : )

> Take care,
> -Moe Khosravy



> > Hello,
> >     I know this is not the right group for this but I can't seem to find
> the
> > right C group.

> > So

> > I want to allocate an array of int dynamically.  This is what I got now

> > DBINT tDBINT[1000];

> > what I want to do is something like this

> > NumberofElements = 200;
> > DBINT tDBINT[NumberofElements];

> > this of doesn't work.

> > Can anyone help me with the C code to do this?

> > My next question is.  If I allocate memory using malloc,  do you have to
> use
> > the free function or will it free up by itself?

> > regards
> > Greg Obleshchuk



Tue, 20 Jan 2004 13:13:44 GMT  
 C Dynamic allocation of Pointers

Quote:
> 1) In your code, declaration of this array on the stack will require the
> variable "NumberofElements" to be of a constant type. Only "work around"
is
> to use dynamic memory allocation. So, if you simply change the variable
> "NumberofElements" to be of type "const UINT or const INT", depending on
> your size requirements, your code will work fine...

Not 100% true, you can allocate dynamic amounts of stack memory using the
alloca() function.  You should be careful when using it though.

Either

DBINT* tDBINT = (DBINT*)malloc(sizeof(DBINT)*NumberOfElements);
...
free(tDBINT);

or

DBINT* tDBINT = (DBINT*)alloca(sizeof(DBINT)*NumberOfElements);
...

no free required when allocated on the stack - though it goes out of scope
so don't hold onto any pointers to it (just like other stack based
variables).

If you're using C++, you can do an easier, safer allocation via:

DBINT* tDBINT = new DBINT[NumberOfElements];
...
delete tDBINT[];

Ken



Tue, 20 Jan 2004 13:29:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Dynamic Array of pointer memory allocation

2. Problem with dynamic allocation - pointers

3. Dynamic allocation of pointer arrays

4. memory allocation for pointer to pointer to char

5. Pointers to Pointers allocation

6. Newbie: separate big .cs file into small .cs files

7. dynamic allocation

8. Multi-dimensional array using dynamic memory allocation

9. Dynamic array allocation;

10. char **info; /*dynamic allocation*/

11. Need help with dynamic memory allocation (malloc) in C

12. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

 

 
Powered by phpBB® Forum Software