English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 18 December 2019, 11:08   #1
deimos
Registered User

 
Join Date: Jul 2018
Location: France
Posts: 537
Is this legal C?

Is this correct / legal C99, and will it do what I expect?

Code:
void AFunction(void) {
    WORD * kludge = NULL;

    // ...

    if (topLeft) {
        kludge = (WORD []) { 0, 1 };
    }

    // ....

    DoSomethingWith(kludge);
}
i.e. is the WORD [] containing the values 0 and 1 available outside of the scope of the if statement that it is declared in? Is it really allocated in the stack frame for the function, or does it end disappearing when the if statement completes, leaving kludge pointing to potential garbage?
deimos is offline  
Old 18 December 2019, 11:57   #2
hooverphonique
ex. demoscener "Bigmama"

 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,042
Normally the compiler will reserve space on the stack for the array when you enter the function, then initialize it in the if-block, but I'm not sure if it could repurpose the stack space between the if-block and DoSomethingWith if you put some code there that requires space.

You could use a statically allocated array and assign its address to kludge instead.
hooverphonique is offline  
Old 18 December 2019, 12:25   #3
deimos
Registered User

 
Join Date: Jul 2018
Location: France
Posts: 537
Quote:
Originally Posted by hooverphonique View Post
it could repurpose the stack space
Ok, thanks, I won't risk it.

Edit:

Decided to use a struct instead:

Code:
typedef struct {
    BOOL required;
    WORD start;
    WORD end;
} Kludge;

void AFunction(void) {
    Kludge kludge = { .required = FALSE };

    // ...

    if (topLeft) {
        kludge = (Kludge) { TRUE, 0, 1 };
    }

    // ....

    DoSomethingWith(kludge);
}

Last edited by deimos; 18 December 2019 at 12:45.
deimos is offline  
Old 18 December 2019, 18:32   #4
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,566
Quote:
Originally Posted by hooverphonique View Post
but I'm not sure if it could repurpose the stack space between the if-block and DoSomethingWith if you put some code there that requires space.
No. That shouldn't happen.
phx is offline  
Old 18 December 2019, 19:13   #5
a/b
Registered User

 
Join Date: Jun 2016
Location: europe
Posts: 174
Yeah, it's legal. It's the same as:
Code:
char* x = NULL;
if (...) {
  x = "whatever";
}
y(x);
The value you assign is anonymous static const, it's fully known at compile time, and it's not placed on stack. It's either placed in the object's data segment and then merged with others by linker (subject to segment manipulation directives), or embedded into code e.g if it fits in 32 bits and some optimizations take place. More or less, but in any case it's safe, in c99 or any other version.
a/b is offline  
Old 18 December 2019, 19:16   #6
deimos
Registered User

 
Join Date: Jul 2018
Location: France
Posts: 537
Quote:
Originally Posted by phx View Post
No. That shouldn't happen.
What should happen to the { 0, 1 }? Where is it? In the functions stack frame? Or is it created within the if statement's block?
deimos is offline  
Old 18 December 2019, 19:27   #7
deimos
Registered User

 
Join Date: Jul 2018
Location: France
Posts: 537
Quote:
Originally Posted by a/b View Post
Yeah, it's legal. It's the same as:
Code:
char* x = NULL;
if (...) {
  x = "whatever";
}
y(x);
The value you assign is anonymous static const, it's fully known at compile time, and it's not placed on stack. It's either placed in the object's data segment and then merged with others by linker (subject to segment manipulation directives), or embedded into code e.g if it fits in 32 bits and some optimizations take place. More or less, but in any case it's safe, in c99 or any other version.
Right. That makes sense, I recognise those words, but it only works if it's really a constant:

Code:
    kludge = (WORD []) { x, y };
Isn't legal, right?
deimos is offline  
Old 18 December 2019, 19:50   #8
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,566
Quote:
Originally Posted by a/b View Post
Yeah, it's legal. It's the same as:
Code:
char* x = NULL;
if (...) {
  x = "whatever";
}
y(x);
Not really. Here you are just assigning x with a constant string pointer. This string is usually not temporarily allocated on the stack.
Deimos' example is some C99-thing. It doesn't work with C89.

Quote:
Originally Posted by deimos View Post
What should happen to the { 0, 1 }? Where is it? In the functions stack frame? Or is it created within the if statement's block?
It's in the stack frame, allocated on function entry.

Quote:
Originally Posted by deimos View Post
Code:
    kludge = (WORD []) { x, y };
Isn't legal, right?
Didn't test it, but I would guess it works. The structure is initialized during the if-statement, so its contents might also be variable (?).
phx is offline  
Old 18 December 2019, 19:54   #9
a/b
Registered User

 
Join Date: Jun 2016
Location: europe
Posts: 174
It's still safe. In this case a temporary object will be constructed on stack but its scope is *not* limited to if's {} (it's limited to current function).
Code:
WORD* p_array = NULL;
WORD  x = 1, y = 2;
if (...) {
  p_array = { x, y };  // OK
}
if (...) {
  WORD a = 1, b = 2;
  p_array = { a, b };  // bad, a and b are limited to if's {}
}
foobar(p_array);
The second case will still work though, because a temporary object on stack is valid and containts valid data because data type is WORD and is entirely copied into the object, and not something more complex (that includes pointers or references) that would cause dangling pointers/references. But it's bad and would be called out by compiler (but if you can get past that it should work).
a/b is offline  
Old 18 December 2019, 20:04   #10
jotd
This cat is no more
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 48
Posts: 3,530
it's safe only when you're assigning a literal to your pointer. Else, if the area pointed to goes out of scope, anything can happen.
jotd is offline  
Old 18 December 2019, 20:34   #11
a/b
Registered User

 
Join Date: Jun 2016
Location: europe
Posts: 174
Quote:
Originally Posted by phx View Post
Not really...
Hmm, you're right. Such arrays (and structs) are constructed in run-time on stack.
I'm sure I knew that at some point, spoiled by not bothering much about such details in c++, and doing whatever I want in asm ;P.
a/b is offline  
Old 18 December 2019, 21:58   #12
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 49
Posts: 511
It's on stack (variable) on gcc-6.5
https://franke.ms/cex/z/A1khyA

On gcc-2.9.5 it's on data segment.
alkis is offline  
Old 18 December 2019, 22:51   #13
hooverphonique
ex. demoscener "Bigmama"

 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,042
String literals are a special case, and they become heap-allocated objects.

You could use compiler explorer (e.g. godbolt.org) to check this kind of thing with different compilers and options.

Edit: or the link alkis provided, which has an m68k version of gcc, if you prefer that (strange that the array initialization is missing when selecting gcc 10, btw).
hooverphonique is offline  
Old 05 January 2020, 12:26   #14
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 447
Quote:
Originally Posted by alkis View Post
It's on stack (variable) on gcc-6.5
https://franke.ms/cex/z/A1khyA

On gcc-2.9.5 it's on data segment.

I consider this as wrong, since it's not const. If DoSomethingWith modifies a value subsequent calls will start with the modified values.


bebbo is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Legal: Amiga schematics michaelz support.Other 25 15 March 2017 14:13
Amiga legal emulation is possible? Leandro Jardim Retrogaming General Discussion 2 21 August 2010 16:40
What the hell...legal or not? cebulba Amiga scene 3 18 May 2005 22:04
Legal Games (Mostly Italian) Frog Amiga websites reviews 2 17 December 2004 23:15
Hopefully this is legal to ask here. JSemple3 Amiga scene 2 15 May 2004 18:02

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

Forum Jump


All times are GMT +2. The time now is 02:44.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Page generated in 0.08356 seconds with 15 queries