473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory allocation for array

I have a question regarding the memory allocation of array.

For example
int array[10];

Now according to my understanding 10 subsequent memory location will
be allocated of size sizeof(int) *10 and this is done at compile
time.

Does it mean C compiler reserve some amount of memory for the array
at compile time?
That means with out the line of code being executed some amount of
memory is getting exhausted.

I think I am misinterpreting the idea of compile time memory
allocation.
Please provide some inputs to correct my understanding.

--
Somenath
Jun 27 '08 #1
4 2471
somenath wrote:
I have a question regarding the memory allocation of array.

For example
int array[10];

Now according to my understanding 10 subsequent memory location will
be allocated of size sizeof(int) *10 and this is done at compile
time.

Does it mean C compiler reserve some amount of memory for the array
at compile time?
The C standard doesn't specify how memory is allocated, so the answer is
likely to vary between implementations .

Having said that, if the array is a local object it is most likely
allocated on a hardware stack provided by the system. In this case
the "space" for the array doesn't exist until the machine instructions
that allocate the space on the stack are encountered. This will
typically be some type of SUB SP,NNN or a series of PUSH instructions.
They will be executed each time the block (or function) is entered and
the opposite clean-up instructions (POP or more likely ADD SP,NNN) will
be executed when the block (or function) is about to be left.

I suppose that allocating memory for local objects from a software stack
would be considerably different. I have not encountered any C compiler
do such a thing, but an interpreter is likely to employ this method.

If your array is a static or file scope object, then the "space" for it
most likely "coded" into the executable itself in some way, either by
actually reserving the necessary bytes or through special instructions
to the program loader which then allocates the memory before the
program is started. The details are likely to vary quite a lot between
systems.
That means with out the line of code being executed some amount of
memory is getting exhausted.
I think you might want to reconsider this.
I think I am misinterpreting the idea of compile time memory
allocation. Please provide some inputs to correct my understanding.
It's hard to provide more input without actually considering an
implementation, and that is off-topic here. Perhaps you might want to
consult a group for your system, or look-up the online version of the
book /Linkers and Loaders/ by Levine, which deals with some aspects of
your question. Also read about stack frames.

Jun 27 '08 #2
somenath wrote:
I have a question regarding the memory allocation of array.

For example
int array[10];

Now according to my understanding 10 subsequent memory location will
be allocated of size sizeof(int) *10 and this is done at compile
time.

Does it mean C compiler reserve some amount of memory for the array
at compile time?
That means with out the line of code being executed some amount of
memory is getting exhausted.

I think I am misinterpreting the idea of compile time memory
allocation.
Please provide some inputs to correct my understanding.

--
Somenath
No. real physical memory is not used until program is loaded (executed).
The compiler (including assembler and linker) just puts instructions
in executable file about how much physical memory should be reserved for
the array should this piece of code ever get executed.

Tejas Kokje
Jun 27 '08 #3
On 18 Jun 2008 at 16:31, Tejas Kokje wrote:
somenath wrote:
>int array[10];
No. real physical memory is not used until program is loaded (executed).
The compiler (including assembler and linker) just puts instructions
in executable file about how much physical memory should be reserved for
the array should this piece of code ever get executed.
That's a reasonable explanation when array is an automatic variable,
though in that case it's slightly misleading to say that "memory is
reserved", since the array will probably occupy different memory
addresses on the stack each time the containing function is called.

If array is a static variable, then it's true to say that a specific
fixed array of memory is reserved for it in the bss segment, but it's
slightly misleading to talk about the code being "executed" in that case
- we usually think of this sort of memory setup "just happening" before
the execution of main() begins.

Jun 27 '08 #4
somenath wrote:
I have a question regarding the memory allocation of array.

For example
int array[10];

Now according to my understanding 10 subsequent memory location will
be allocated of size sizeof(int) *10 and this is done at compile
time.

Does it mean C compiler reserve some amount of memory for the array
at compile time?
Compiler does not literally "allocate" this memory, of course, since it
will only be allocated when the program is executed. When someone says
that the memory is "allocated" at compile time, it really means that the
memory layout can be determined and hardcoded at compile time. I.e. it
really means that the compiler has enough information to predict the
memory layout (location, size, etc) for these variables with the level
of certainty that permits it to hardcode the absolute (or relative)
address of the variable in the actual code. (As opposed to obtaining,
storing and later using an "unpredicta ble" address in a pointer
variable). This is possible with static variables (which are normally
located at fixed absolute address, known at compile time) and automatic
variables (which are normally located at fixed offset from the beginning
of the function stack frame, known at compile time).

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #5

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

Similar topics

2
5345
by: hall | last post by:
I have a question regarding where memory is allocated when arrays are created. I'll illustrate this by example. I may be wrong on some details, do feel free to correct me. The code piece: int p; Creates a 2dimensional array. p can be thought of as a pointer, containing the adres of the first element in the array. The memory is
11
8696
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can return a pointer to an arbirary sized element in an array given an index and it will allocate the memory to back it if necessary: struct varray *tests = varray_new(sizeof(struct test)); struct test *t = (struct test *)varray_get(tests, 50));
6
1909
by: Rex_chaos | last post by:
I have allocated a array like double a; We know that a 1-D array just like an 1-D pointer. So I wonder that should I release the memory of the array myself like free(a); Thanks in advance.
74
4680
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique seems to be setting a NULL pointer to the end of the list, and here we know that the allocated memory has been exhausted. All good. When this is a pointer to another type, say int, I could have a variable that records how much memory is being...
7
4691
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now are the memory layout is contiguous? i.e all the character resides side by side just like array, and all Points side by side insede the
24
19089
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.
81
4564
by: Peter Olcott | last post by:
It looks like System::Collections::Generic.List throws and OUT_OF_MEMORY exception whenever memory allocated exceeds 256 MB. I have 1024 MB on my system so I am not even out of physical RAM, much less virtual memory. Are other people experiencing this same problem?
1
7975
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
9
2518
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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
9689
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...
1
7226
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
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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

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.