arrays and stack 
Author Message
 arrays and stack

Hi,

Can someone please help me out with this?

If I make a call to a procedure

int foo(char bar[xxx]) { /* where xxx is some value */
....

Quote:
}

Does the entire array bar gets loaded to the run time stack, or only the address
of the first element? Right now, I believe that only the address of the first
element is pushed onto the stack, but I might be on crack!! Thanks in advance!

James



Fri, 23 Aug 2002 03:00:00 GMT  
 arrays and stack


Quote:
>Hi,

>Can someone please help me out with this?

>If I make a call to a procedure

>int foo(char bar[xxx]) { /* where xxx is some value */
>....
>}

>Does the entire array bar gets loaded to the run time stack, or only the address
>of the first element? Right now, I believe that only the address of the first
>element is pushed onto the stack, but I might be on crack!! Thanks in advance!

When you pass an array to a function, it decays into a pointer to its
first element and only that pointer gets passed to the function, not
the entire array.

<pedantic>
This pointer may or may not be passed by pushing it onto the stack.  I
know of no implementation that doesn't do it this way, but the C
language doesn't guarantee it (or, to the best of my knowledge, even
guarantee that the code is running on a machine that _has_ a stack).
</pedantic>

dave
--
Dave Vandervies

I have no mouth, and I must scream.



Fri, 23 Aug 2002 03:00:00 GMT  
 arrays and stack

Quote:
James Pan writes:
>If I make a call to a procedure

>int foo(char bar[xxx]) { /* where xxx is some value */
>....
>}

>Does the entire array bar gets loaded to the run time stack, or only the
>address
>of the first element? Right now, I believe that only the address of the first
>element is pushed onto the stack, but I might be on crack!! Thanks in
>advance!

xxx is a variable name.  73 is a value.  But other than that nit pick you're
head is right, just the address.


Fri, 23 Aug 2002 03:00:00 GMT  
 arrays and stack

wrote in comp.lang.c:

Quote:
> Hi,

> Can someone please help me out with this?

> If I make a call to a procedure

> int foo(char bar[xxx]) { /* where xxx is some value */
> ....
> }

> Does the entire array bar gets loaded to the run time stack, or only the address
> of the first element? Right now, I believe that only the address of the first
> element is pushed onto the stack, but I might be on crack!! Thanks in advance!

> James

That depends on whether the implementation uses a run-time stack.  The
language does not define or require one, although that is a common
method.  A single parameter might well be passed in a processor
register.

But to answer the rest of your question:

int foo(char bar[100])

is exactly equivalent to:

int foo(char bar[])

is exactly equivalent to:

int foo(char *bar)

You can't ever pass an array in C, the name always decays into a
pointer to the first element in a function call and most (but not all)
other circumstances.

It is an unfortunate historical accident that the brackets are allowed
in function declarations and definitions, because they imply that the
array is passed by value when it is not.  The number in the brackets
has no meaning, by the way.

Jack Klein
--
Home: http://jackklein.home.att.net



Sat, 24 Aug 2002 03:00:00 GMT  
 arrays and stack
Mostly its the address
Quote:

> Hi,

> Can someone please help me out with this?

> If I make a call to a procedure

> int foo(char bar[xxx]) { /* where xxx is some value */
> ....
> }

> Does the entire array bar gets loaded to the run time stack, or only the address
> of the first element? Right now, I believe that only the address of the first
> element is pushed onto the stack, but I might be on crack!! Thanks in advance!

> James



Sat, 24 Aug 2002 03:00:00 GMT  
 arrays and stack


Quote:


[snip]
> >int foo(char bar[xxx]) { /* where xxx is some value */
[snip]
> >Does the entire array bar gets loaded to the run time stack, or only
the address
> >of the first element? Right now, I believe that only the address of
the first
> >element is pushed onto the stack, but I might be on crack!! Thanks
in advance!
> When you pass an array to a function, it decays into a pointer to its
> first element and only that pointer gets passed to the function, not
> the entire array.

> <pedantic>
> This pointer may or may not be passed by pushing it onto the stack.  I
> know of no implementation that doesn't do it this way, but the C
> language doesn't guarantee it (or, to the best of my knowledge, even
> guarantee that the code is running on a machine that _has_ a stack).
> </pedantic>

Actually, I am currently using an implementation that doesn't do it
this way.  Specifically, it's an IAR C compiler for the 8031 family of
8-bit microcontrollers.  The 8031 family has a small stack in its 256-
byte internal data area.  In this implementation, the first four bytes'
worth of arguments are passed in registers R4 to R7.  Pointers are
either 2 bytes (if non-ANSI keywords are used to give them types) or 3
bytes.  Any "overflow" arguments are passed in a "pseudo-stack", being
an area of internal or external (depending on the memory model) RAM
that's managed by the linker.  Variables also go in this area.

This sort of approach is not unusual in the lower end of the processor
range in the embedded world, but the compiler can still compile ANSI C
if required.  The code is more efficient - albeit less portable - if
the non-ANSI extension are used, but that's a tradeoff we have to make
occassionally (like once a day on average).

Just thought you'd like to know.

--
Geoff Field of XXXXia
Professional geek, amateur stage-levelling gauge

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 arrays and stack


Quote:


>>Hi,

>>Can someone please help me out with this?

>>If I make a call to a procedure

>>int foo(char bar[xxx]) { /* where xxx is some value */
>>....
>>}

>>Does the entire array bar gets loaded to the run time stack, or only the address
>>of the first element? Right now, I believe that only the address of the first
>>element is pushed onto the stack, but I might be on crack!! Thanks in advance!

> When you pass an array to a function, it decays into a pointer to its
> first element and only that pointer gets passed to the function, not
> the entire array.

> <pedantic>
> This pointer may or may not be passed by pushing it onto the stack.  I
> know of no implementation that doesn't do it this way,

The ARM C compiler passes the first four arguments to a function in registers.
Those arguments *may* get saved on a stack, once in the called function,
but they need not be.

--
Chris "RISC PC, with StrongARM" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Sat, 24 Aug 2002 03:00:00 GMT  
 arrays and stack


Quote:


> > Does the entire array bar gets loaded to the run time stack, or
> > only the address of the first element?

> Mostly its the address

why "mostly"?

--
"Perilous to us all are the devices of an art deeper than we possess
ourselves."
                Gandalf The Grey (discussing Windows NT)

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Declare N Sized Array on Stack?

2. ARRAYS AND STACKS

3. portable way to put arrays on the stack

4. how are arrays and functions stored in stack

5. how are arrays and functions stored in stack

6. QUestion regarding Stack based arrays....

7. Stack based variable sized arrays

8. Stack overflow witth a "large" array

9. increas the stack size for an array

10. Newbie question: Stack Overflow on Large Array

11. Two dimensional array on the stack

12. Array of pointers pointing to stacks

 

 
Powered by phpBB® Forum Software