Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Variables allocated on stack

Reply
Thread Tools

Variables allocated on stack

 
 
Keith Thompson
Guest
Posts: n/a
 
      12-03-2011
Malcolm McLean <> writes:
> On Dec 3, 12:33Â*am, Quentin Pope <qp19...@hotmail.NOSPAM.com> wrote:
>> Sorry, but this is extremely misleading. C can and does run on
>> implementations with no stack.
>>

> I had such an implementation. int was eight bits, double 24 bits, of
> which 7 were exponent. Recursion was not supported. There was no
> malloc. Essentially every variable had an invisible "static"
> qualifier. It wasn't conforming, but it was clearly C.


I'd say such an implementation is C-like, but it's not C.

I think what Quentin is referring to is implementations on which
activation records for function calls are not necessarily allocated
at monotonically increasing or decreasing addresses.

Any conforming C implementation must have something that behaves in
a stack-like manner. It needn't be a contiguous region of memory.
And even if, as Quentin suggested, the implementation uses a fixed
length buffer for active function calls, it still has to behave in
a stack-like manner.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
David Thompson
Guest
Posts: n/a
 
      01-19-2012
On Sun, 27 Nov 2011 11:20:31 +0100, André Gillibert
<> wrote:

> Markus Wichmann <> wrote:
> > On 26.11.2011 12:07, BartC wrote:
> >
> > > Surely compiler writers can do what they like - within their own
> > > implementation? Calling conventions are only important when calling foreign
> > > and OS functions, and being called from outside.
> > >

> >
> > Yeah, that's right!
> >
> > Only... every function not defined "static" may be called "from the
> > outside". Every call to a function only declared (and not qualified
> > "static") but not defined may be to a foreign function.
> >

>
> Plus, when a "static" function pointer is passed around, either the
> function must get the standard calling convention or a stub must be
> written.
>

Or the function pointers are nonstandardly implementation-dependently
qualified (which I've seen). Or you use fat pointers that contain the
convention (which I've not seen, but it would be conforming).

> Or, an implementation that wants a non-standard calling convention may
> use a non-standard keyword to differentiate internal functions that
> must be called with the new calling convention from exported functions.


keyword or pragma or config setting, or possibly something else. Or
the compiler can guess -- for example, any 'static' function whose
address is never directly exported nor stored anywhere that could be
exported can safely be 'optimized'. That's a simple rule that suffices
for probably most useful cases.

 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      01-25-2012
Keith Thompson <kst-> writes:

> Markus Wichmann <> writes:
>> On 29.11.2011 01:17, Keith Thompson wrote:
>>> Markus Wichmann <> writes:
>>> [...]
>>>> $ uname -m -o
>>>> x86_64 GNU/Linux
>>>> $ cat test.c
>>>> #include <stdio.h>
>>>>
>>>> int main()
>>>> {
>>>> printf("%d\n", sizeof(long));
>>>> return 0;
>>>> }
>>>> $ gcc test.c && ./a.out
>>>> 8
>>>> $ gcc -m32 test.c && ./a.out
>>>> 4
>>> [...]
>>>
>>> That could have failed, since "%d" expects an int argument but you
>>> passed it a size_t.
>>>

>>
>> Oh, my bad. "%zd" it is, then.

>
> No, "%zu", since size_t is an unsigned type.
>
> [...]
>>> (And "int main()" should be "int main(void)".)

>>
>> OK, that's just a nitpick. The semantic difference between both of them
>> is that in the first case, the compiler doesn't complain if the caller
>> of main() passes additional arguments. In both cases the compiler
>> doesn't even get to see the caller, the only one who does that is the
>> linker.

>
> I've argued that a program using "int main()", as opposed to "int
> main(void)", has undefined behavior, since "int main()" is not one of
> the forms permitted by C99 5.1.2.2.1 (nor is it quite equivalent to
> either of them). [snip]


It is equivalent in the sense that the Standard uses the
term. Obviously so, since otherwise every single K&R-era
program that had 'int main(){ ... }' would have undefined
behavior under ANSI/ISO C; surely that point was considered
by the standard committee.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-25-2012
Tim Rentsch <> writes:
> Keith Thompson <kst-> writes:

[...]
>> I've argued that a program using "int main()", as opposed to "int
>> main(void)", has undefined behavior, since "int main()" is not one of
>> the forms permitted by C99 5.1.2.2.1 (nor is it quite equivalent to
>> either of them). [snip]

>
> It is equivalent in the sense that the Standard uses the
> term. Obviously so, since otherwise every single K&R-era
> program that had 'int main(){ ... }' would have undefined
> behavior under ANSI/ISO C; surely that point was considered
> by the standard committee.


I'm uncomfortable with conclusions reached by first assuming that the
standard doesn't break pre-ANSI code, and interpreting the wording based
on that assumption.

I can accept, based on your argument, that "int main()" is *intended* to
be valid. I don't agree that the current wording in the standard
actually *says* that it's valid. The only basis for assuming that
"equivalent" has the meaning you say it does is that Bad Things Happen
if it doesn't.

Someone reading the current ISO C standard with no knowledge of pre-ANSI
C would have no basis to assume that "int main()" is valid.

These two programs differ only in how they define main, and they clearly
are not equivalent; the first violates a constraint, while the second
does not.

int main(void) {
if (0) return main(42);
}

int main() {
if (0) return main(42);
}

I believe this was an oversight by the authors of the standard (one that
isn't corrected in C11, unless they did something after N1570).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      02-01-2012
Keith Thompson <kst-> writes:

> Tim Rentsch <> writes:
>> Keith Thompson <kst-> writes:

> [...]
>>> I've argued that a program using "int main()", as opposed to "int
>>> main(void)", has undefined behavior, since "int main()" is not one of
>>> the forms permitted by C99 5.1.2.2.1 (nor is it quite equivalent to
>>> either of them). [snip]

>>
>> It is equivalent in the sense that the Standard uses the
>> term. Obviously so, since otherwise every single K&R-era
>> program that had 'int main(){ ... }' would have undefined
>> behavior under ANSI/ISO C; surely that point was considered
>> by the standard committee.

>
> I'm uncomfortable with conclusions reached by first assuming that the
> standard doesn't break pre-ANSI code, and interpreting the wording based
> on that assumption.


For me it depends on what the motivation is for forming those
conclusions. For purposes of deciding what "the general public"
ought to think about C, I think it's perfectly okay to use that
kind of reasoning. For purposes of revising and improving wording
in the Standard, I'm more inclined to reason along the lines you
are advocating. But it depends on the circumstances, because those
situations are different in some important ways.

> I can accept, based on your argument, that "int main()" is *intended* to
> be valid. I don't agree that the current wording in the standard
> actually *says* that it's valid. The only basis for assuming that
> "equivalent" has the meaning you say it does is that Bad Things Happen
> if it doesn't.
>
> Someone reading the current ISO C standard with no knowledge of pre-ANSI
> C would have no basis to assume that "int main()" is valid.
>
> These two programs differ only in how they define main, and they clearly
> are not equivalent; the first violates a constraint, while the second
> does not.
>
> int main(void) {
> if (0) return main(42);
> }
>
> int main() {
> if (0) return main(42);
> }
>
> I believe this was an oversight by the authors of the standard (one that
> isn't corrected in C11, unless they did something after N1570).


I don't think we really have any significant disagreement. I
believe the intent was that the two forms be interchangeable as
far as invokability goes, and you can accept that. You think the
existing wording need clarifying, and I can go along with that
(and if it were up to me I would simply add a footnote with the
clarification). Beyond that, I don't think it's fruitful to be
concerned about what the Standard "really says", because the
current wording includes an English word which is ambiguous in
its meaning.
 
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
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
perl variables allocated in shared memory - feasible/possible/done already? Chris Perl Misc 1 02-03-2007 06:12 PM
variable allocated from stack/bss ?? onkar C Programming 148 12-18-2006 07:22 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57