Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > when are Arrays allocated memory? are they affected by code blocks?

Reply
Thread Tools

when are Arrays allocated memory? are they affected by code blocks?

 
 
Noobzo
Guest
Posts: n/a
 
      02-15-2010
Hello, i'm learning about arrays and my question is, when do arrays
get their memory reserved? here are some exaples

1)
void Function1()
{
int arr[10];
arr[0] = 1;
}

int main( )
{
Function1();
}

////////////////////////////////////////////////////
2)
int main( )
{
Array1[5];

{
Array2[15];

{
Array3[20];
}
}
}// does the block prevent the array from being allocated until it
reaches execution?

thank you.
 
Reply With Quote
 
 
 
 
Branimir Maksimovic
Guest
Posts: n/a
 
      02-15-2010
Noobzo wrote:
> Hello, i'm learning about arrays and my question is, when do arrays
> get their memory reserved? here are some exaples
>
> 1)
> void Function1()
> {
> int arr[10];
> arr[0] = 1;
> }
>
> int main( )
> {
> Function1();
> }
>
> 2)
> int main( )
> {
> Array1[5];
>
> {
> Array2[15];
>
> {
> Array3[20];
> }
> }
> }// does the block prevent the array from being allocated until it
> reaches execution?

I believe that with optimization turned on, none of those arrays would
be allocated at all , it's up to implementation

For example g++ creates code by the book with -O0 but with -O2,
generates completely different code ...

All in all you can't access arrays out of their scope...
I mean you can, but code won;t work...

Greets
 
Reply With Quote
 
 
 
 
Noobzo
Guest
Posts: n/a
 
      02-15-2010
thanks Branimir Maksimovic for your reply,

but given that these arrays were used and the optimizer didn't remove
them, would putting an array in a scope help reducing overheads?
because sometimes in big functions, many arrays would be defined for
small tasks, would it help to place these arrays in smaller code
blocks inside the functions? something like this:


void function()
{
int array1[100];
.
.
.
.

{
//will this array get on the stack now? or once the
function entered its scope?

int array2[100];//would it be removed from the stack
after existing this code block?
.
.
.
.

}

int array3[100];
.
.
.
.

}
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      02-15-2010
In article <858a112e-8963-4bab-b336-
http://www.velocityreviews.com/forums/(E-Mail Removed)>, (E-Mail Removed) says...

[ ... ]

> int main( )
> {
> Array1[5];
>
> {
> Array2[15];
>
> {
> Array3[20];
> }
> }
> }// does the block prevent the array from being allocated until it
> reaches execution?


At least in theory, it could vary between compilers. All of them I've
looked at, however, did all the stack allocations for a function upon
entry to that function.

--
Later,
Jerry.
 
Reply With Quote
 
Branimir Maksimovic
Guest
Posts: n/a
 
      02-15-2010
Noobzo wrote:
> {
> //will this array get on the stack now? or once the
> function entered its scope?
>
> int array2[100];//would it be removed from the stack
> after existing this code block?
> .
> .
> .
> .
>
> }


Stack is just memory area of very limited size. If you have too much
local arrays sooner or later you'll reach maximum size, which
will trigger exception,segfault etc
Perhaps os can grow stack (run time overhead with cooperation form
compiler?), perhaps not. I don;t know.
Answer depends a lot on how stack frame is set up.
Usually you have place for parameters , return address, and local
variables, something else ? So anyhow if you want to grow/shrink
stack local vars should be down below, which is not always, possible
and simple I guess.

Greets
 
Reply With Quote
 
jamm
Guest
Posts: n/a
 
      02-15-2010
Noobzo wrote:

> Hello, i'm learning about arrays and my question is, when do arrays
> get their memory reserved? here are some exaples
>
> 1)
> void Function1()
> {
> int arr[10];
> arr[0] = 1;
> }
>
> int main( )
> {
> Function1();
> }
>
> ////////////////////////////////////////////////////
> 2)
> int main( )
> {
> Array1[5];
>
> {
> Array2[15];
>
> {
> Array3[20];
> }
> }
> }// does the block prevent the array from being allocated until it
> reaches execution?
>
> thank you.


Allocated on the stack upon entry of function.

--
*From the 1966 TV series:*
Robin: You can't get away from Batman that easy!
Batman: Easily.
Robin: Easily.
Batman: Good grammar is essential, Robin.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-16-2010
On Feb 15, 7:31 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
> "Noobzo" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...
> > thanks Branimir Maksimovic for your reply,


> > but given that these arrays were used and the optimizer
> > didn't remove them, would putting an array in a scope help
> > reducing overheads? because sometimes in big functions,
> > many arrays would be defined for small tasks, would it help
> > to place these arrays in smaller code blocks inside the
> > functions? something like this:


> > void function()
> > {
> > int array1[100];
> > .
> > .
> > .
> > .

>
> > {
> > //will this array get on the stack now? or once the
> > function entered its scope?


> > int array2[100];//would it be removed from the stack
> > after existing this code block?
> > .
> > .
> > .
> > .
> > }


> > int array3[100];
> > .
> > .
> > .
> > .


> > }


> Typically stack space is reserved for arrays just the once on
> entry to the function even if the array exists in a child
> scope within the function.


Yes, but there's no way a conforming program can tell, so the
question is rather irrelevant.

If the arrays are in non overlapping blocks, e.g.:

void f()
{
{
int array1[20];
// ...
}
{
int array2[20]
// ...
}
}

I would hope that the compiler didn't reserve two distinct,
non-overlapping areas on the stack for the two arrays. But
again, there's no way to really know from within the program.

--
James Kanze
 
Reply With Quote
 
Noobzo
Guest
Posts: n/a
 
      02-16-2010
well thank you guys for the helpful discussion, am gonna try to find
my specific compiler documentation about this, hope that Microsoft has
it documented though lol, thanks
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      02-16-2010
On 15 Feb, 15:50, Noobzo <(E-Mail Removed)> wrote:
> Hello, i'm learning about arrays and my question is, when do arrays
> get their memory reserved? here are some exaples
>
> 1)
> void Function1()
> {
> * * * * int arr[10];
> * * * * arr[0] = 1;
>
> }
>
> int *main( )
> {
> * * * * Function1();
>
> }
>
> ////////////////////////////////////////////////////
> 2)
> int *main( )
> {
> * * * * Array1[5];
>
> * * * * {
> * * * * * * * * Array2[15];
>
> * * * * * * * * {
> * * * * * * * * * * * * Array3[20];
> * * * * * * * * }
> * * * * }}// does the block prevent the array from being allocated until it
>
> reaches execution?



and generally speaking use std::vector rather tha arrays

 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      02-16-2010
On Feb 15, 4:50*pm, Noobzo <(E-Mail Removed)> wrote:
> Hello, i'm learning about arrays and my question is, when do arrays
> get their memory reserved? here are some exaples


That is completely up to the compiler. Basically, the only requirement
is that it happens somewhere between starting your program and the
moment when execution reaches the definition of your array. Typically,
it is more restricted to between entering the function and reaching
the definition of the array.

Modern compilers are reasonably good at reducing the amount of memory
that is needlessly allocated. For example, if you have two or more
variables (or arrays) that do not exist at the same time, most
compilers will try let them share the same memory locations.

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Dynamically Allocated Multi-Dimensional Arrays bwaichu@yahoo.com C Programming 6 09-03-2006 06:05 AM
finding element width in dynamically allocated arrays tricard@gmail.com C Programming 5 01-23-2006 10:22 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments