473,403 Members | 2,222 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,403 software developers and data experts.

memory use global vs auto

ok
I have the following code:

void foo(void)
{
const int array[100] = ....;

// do stuff with array
}

I mostly use code like this so my program doesnt waste memory because array
only gets allocated when foo is called.
But now I heard from somebody that the following code is the same in terms
of memory footprint:

static int array[100] = .....

void foo(void)
{
// do stuff with array
}

is that true?
Mar 10 '06 #1
4 1619
"ok" <la@hm.com> writes:

[question about comparative memory footprint of two examples
below]
void foo(void)
{
const int array[100] = ....;

// do stuff with array
} static int array[100] = .....


I imagine that these would have approximately the same memory
footprint on many implementations, given that the former array is
const. If it was non-const, then it would likely take *more*
memory than the latter, because the implementation would likely
initialize the array by copying a statically allocated copy.
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Mar 10 '06 #2
In article <du**********@news5.zwoll1.ov.home.nl>, "ok" <la@hm.com>
wrote:
I have the following code:

void foo(void)
{
const int array[100] = ....;

// do stuff with array
}

I mostly use code like this so my program doesnt waste memory because array
only gets allocated when foo is called.


Think for ten minutes what kind of code your compiler would have to
produce to implement this. Then have a look at the assembler code that
it generates. It will be a good learning experience.
Mar 10 '06 #3
ok wrote:
I have the following code:

void foo(void)
{
const int array[100] = ....;

// do stuff with array
}

I mostly use code like this so my program doesnt waste memory because array
only gets allocated when foo is called.
But now I heard from somebody that the following code is the same in terms
of memory footprint:

static int array[100] = .....

void foo(void)
{
// do stuff with array
}

is that true?


That is entierly up to the implementation (your particular compiler),
possibly also which optimizations you enable, etc., too.
Mar 11 '06 #4
ok

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <du**********@news5.zwoll1.ov.home.nl>, "ok" <la@hm.com>
wrote:
I have the following code:

void foo(void)
{
const int array[100] = ....;

// do stuff with array
}

I mostly use code like this so my program doesnt waste memory because
array
only gets allocated when foo is called.


Think for ten minutes what kind of code your compiler would have to
produce to implement this. Then have a look at the assembler code that
it generates. It will be a good learning experience.


will do, installing my fav compiler now :)
So I guess it doesnt matter to use global static or auto in this case
mostly?
Mar 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that...
12
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine...
11
by: tomgee | last post by:
I saw it many times, // T.h: class T { public: static T* instance(); private: T() {}
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
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...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
8
by: Horacius ReX | last post by:
Hi, I am developing some code in C which first I compile on my linux machine and afterwards I test on another special hardware which has almost no debug capabilities at all. Usually I get a lot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.