473,943 Members | 23,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory for global variable...

Hi Everyone,

When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?

And is it correct to understand that the resulting exe will have the
memory for the global variable?

Thanks in advance!!!

May 2 '07 #1
4 2478
sa*****@yahoo.c o.in said:
Hi Everyone,

When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?
Conceptually speaking, the memory required by a file scope object is
allocated before main() is called, and remains allocated until the
program stops executing. There need not be a compile time (it might be
an interpreter), a load time (the program might never run), or a link
time (the program might be a standalone program for an embedded system,
with no runtime library to link).
And is it correct to understand that the resulting exe will have the
memory for the global variable?
That depends on what you mean by "exe", "have", "memory", and "global
variable".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 2 '07 #2
Richard Heathfield wrote:
sa*****@yahoo.c o.in said:
>Hi Everyone,

When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?

Conceptually speaking, the memory required by a file scope object is
allocated before main() is called, and remains allocated until the
program stops executing.
Does it work just as well (I'm not saying that this is what the standards
say) to say that the memory required by a file scope object is allocated
before the first use of that object [1] and remains allocated until after
the last use of that object?

After all, no need to allocate memory for an object that's never used,
nor to retain it once it never will be used again.

[1] `&foo` uses `foo`.

--
"Who do you serve, and who do you trust?" /Crusade/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 2 '07 #3
In article <11************ ********@q75g20 00hsh.googlegro ups.com>,
<sa*****@yahoo. co.inwrote:
When is memory allocated for a global variable declared in sample.c
file? Is it during compile time or loading time or linking time?

And is it correct to understand that the resulting exe will have the
memory for the global variable?
I'm not familiar with "exe" files, but a common technique is to divide
variables into those whose initial value is zero, and the rest. The
zero ones are allocated in space which is set to zero when the program
starts up, so they don't need to be stored in the executable file.
The others are stored in an area copied in from the executable file.

One way or another, the executable file has to identify the initial
values. Treating zero as a special case is just an optimisation
that's worthwhile because (a) it's very common to use zero as the
initial value and (b) it's the default initial value for static and
global variables in C.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 2 '07 #4
Chris Dollin said:
Richard Heathfield wrote:
<snip>
>>
Conceptually speaking, the memory required by a file scope object is
allocated before main() is called, and remains allocated until the
program stops executing.

Does it work just as well (I'm not saying that this is what the
standards say) to say that the memory required by a file scope object
is allocated before the first use of that object [1] and remains
allocated until after the last use of that object?
Yes. That's the "as if" rule, working hard as usual...

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 2 '07 #5

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

Similar topics

4
5567
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects: http://groups.google.com/groups?selm=bcq6fn%24g53%241%248300dec7%40news.demon.co.uk This message summarizes some testing I've done and their results. These results somewhat contradict Cornford's conclusions; I haven't
5
3703
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the code. The specific location of the memory corruption depends on the names I give my local variables, on putting some of the codes in curly brackets - {}, and on having the watch window open. I already cleaned and re-built my project. Can anyone...
1
2332
by: Bryan Parkoff | last post by:
"U_BYTE Mem;" is stored in the global variable. It contains 10 megabytes. Do run-time automatically use new keyword to allocate 10 megabytes into memory, and then zero is filled automatically during the initialization before it reaches to use main() function? Do you think that it is always unsafe because it may unexpect crash by not having enough memory? Is it safe to use new keyowrd and fill zero manually inside main() function? How...
10
2794
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to access it (probably via a second pointer to the same
4
2047
by: voidtwerp | last post by:
Hi, I hope this is not too OT but I would like clarification on how classes are held in memory. each object obviously has an individual copy of its data. I guess a class would only have one copy of its functions held in memory (not sure what part of memory this would be refered to as). Then whenever an object has a function called on it this function would be copied onto the stack - am I right?
18
2651
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but tweaking the C++ code appears to fix the problem on Linux. I have a static array that is declared privately in one class and a structure that is declared publicly in another class. The program uses both classes. I was getting core dumps and traced...
24
19128
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
53
26460
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 variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
5
3885
by: vnpatriot7 | last post by:
Hi everybody, I have two questions: 1) About memory space in C++ 2) About global and static variable As what I read somewhere that after compilation process which translate C++ code into machine language, your application is given a certain amount of memory to use, that memory space is divided into 4 segments as follow a. Code segment, where all application code is stored b. Data segment, where global data is stored c. Stack segment,...
0
9970
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11532
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11299
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10662
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9864
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8219
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6308
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4910
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.