18 December 2019, 10:08 | #1 |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
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); } |
18 December 2019, 10:57 | #2 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,634
|
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. |
18 December 2019, 11:25 | #3 |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
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 11:45. |
18 December 2019, 17:32 | #4 |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,521
|
|
18 December 2019, 18:13 | #5 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,046
|
Yeah, it's legal. It's the same as:
Code:
char* x = NULL; if (...) { x = "whatever"; } y(x); |
18 December 2019, 18:16 | #6 |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
|
18 December 2019, 18:27 | #7 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
Code:
kludge = (WORD []) { x, y }; |
|
18 December 2019, 18:50 | #8 | ||
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,521
|
Quote:
Deimos' example is some C99-thing. It doesn't work with C89. Quote:
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 (?). |
||
18 December 2019, 18:54 | #9 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,046
|
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); |
18 December 2019, 19:04 | #10 |
This cat is no more
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,333
|
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.
|
18 December 2019, 19:34 | #11 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,046
|
|
18 December 2019, 20:58 | #12 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
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. |
18 December 2019, 21:51 | #13 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,634
|
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). |
05 January 2020, 11:26 | #14 | |
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
Quote:
I consider this as wrong, since it's not const. If DoSomethingWith modifies a value subsequent calls will start with the modified values. |
|
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 13:13 |
Amiga legal emulation is possible? | Leandro Jardim | Retrogaming General Discussion | 2 | 21 August 2010 15:40 |
What the hell...legal or not? | cebulba | Amiga scene | 3 | 18 May 2005 21:04 |
Legal Games (Mostly Italian) | Frog | Amiga websites reviews | 2 | 17 December 2004 22:15 |
Hopefully this is legal to ask here. | JSemple3 | Amiga scene | 2 | 15 May 2004 17:02 |
|
|