473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

question about dynamic memory allocation

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
6 1529
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
5
by: mickey | last post by:
I know this may sound ridiculous but I need sanity check. I am developing in c++ for an embedded device where memory is tight and I have a class which always upon initialization dynamically...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.