By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,940 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,940 IT Pros & Developers. It's quick & easy.

question about dynamic memory allocation

P: n/a
Hi all,

I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'? For instance, I have the following:
#define MESG_LENGTH 12 unsigned char packet[sizeof(struct ip) + MESG_LENGTH];


But what if I don't know beforehand the size of MESG_LENGTH? Can I have
#define a variable taking its value from outside the code? for instance,
from a file or a parameter?

I know that for these situations one usually uses the 'heap' (malloc,
etc).. but I was wondering if there is a way to do it as asked... just
curiosity..

Thanks,

FBM
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Fernando Barsoba" <fb******@verizon.net> wrote in message
news:Sltcf.11829$Bx.4727@trnddc01...
I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'?


With a C99 compiler, you may use variable length arrays. OT: on many
systems, a function alloca() exists which can achieve a similar effect.

Alex
Nov 15 '05 #2

P: n/a
Alex Fraser wrote:
"Fernando Barsoba" <fb******@verizon.net> wrote in message
news:Sltcf.11829$Bx.4727@trnddc01...
I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'?


With a C99 compiler, you may use variable length arrays. OT: on many
systems, a function alloca() exists which can achieve a similar effect.

Alex


Is this good programming style? Any problems doing this?
Nov 15 '05 #3

P: n/a
Fernando Barsoba wrote:

Alex Fraser wrote:
"Fernando Barsoba" <fb******@verizon.net> wrote in message
news:Sltcf.11829$Bx.4727@trnddc01...
I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'?


With a C99 compiler, you may use variable length arrays. OT: on many
systems, a function alloca()
exists which can achieve a similar effect.

Alex


Is this good programming style? Any problems doing this?


malloc is more portable than alloc.

--
pete
Nov 15 '05 #4

P: n/a
On 2005-11-09, Fernando Barsoba <fb******@verizon.net> wrote:
Alex Fraser wrote:
"Fernando Barsoba" <fb******@verizon.net> wrote in message
news:Sltcf.11829$Bx.4727@trnddc01...
I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'?


With a C99 compiler, you may use variable length arrays. OT: on many
systems, a function alloca() exists which can achieve a similar effect.

Alex


Is this good programming style? Any problems doing this?


alloca() is never good programming style. There are many problems with
doing it - In particular, many historical implementations are quite
buggy and in particular cannot be safely called in the middle of an
argument list.
Nov 15 '05 #5

P: n/a
Fernando Barsoba wrote:
Hi all,

I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'? For instance, I have the following:
> #define MESG_LENGTH 12

> unsigned char packet[sizeof(struct ip) + MESG_LENGTH];


But what if I don't know beforehand the size of MESG_LENGTH? Can I have
#define a variable taking its value from outside the code? for instance,
from a file or a parameter?


#define MAX_MESG_LENGTH 1024
is the easiest way to go in many cases.

Nov 15 '05 #6

P: n/a
Alex Fraser wrote:
"Fernando Barsoba" <fb******@verizon.net> wrote in message
news:Sltcf.11829$Bx.4727@trnddc01...
I have a simple question regarding dynamic memory allocation.. is there
a way to create a variable for which we don't know its size using the
'stack' instead of the 'heap'?


With a C99 compiler, you may use variable length arrays.


So, do you mean by variable length arrays that this is valid?

int size_array = sizeof(struct ip) + cnf->msg_length;
/* cnf->msg_length returns an 'int' */
unsigned char packet[size_array];

FBM
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.