Questions tagged [gcc-extensions]

GCC provides extensions not found in ISO standard C or C++.

45 questions
3
votes
0 answers

Using code blocks as rvalues - inside if(), for(), while()?

I've noticed that gcc accepts code like: if ( ({ int ret; /* code here */; ret }) == some_value) ...; for ( i = ({ int ret; /* code here */; ret }); i < top; i++) ...; and so on. One can create an anonymous codeblock and treat it as rvalue…
FrankH.
  • 17,675
  • 3
  • 44
  • 63
2
votes
2 answers

Why sizeof (array A[n]) without n defined in C++is fixed?

When I try to find the sizeof(A) where A is of type int with size as 'n', n is an int that is not defined. I get an output of 496 and when I give a value to n and then check it, sizeof(A) gives me the same values of 496. I know Array is a static…
Itzz_CJ
  • 70
  • 5
2
votes
0 answers

Should I always use __builtin_extract_return_addr after __builtin_return_address

The gcc documentation: Additional post-processing of the returned value may be needed, see __builtin_extract_return_addr. The stored representation of the return address in memory may be different from the address returned by…
anton_rh
  • 8,226
  • 7
  • 45
  • 73
2
votes
1 answer

GCC get build date and time in local timezone

GCC provides the macros __DATE__ and __TIME__ that give string constants with the build date and time. However they seem to be giving the time in UTC. Is there some macro to get the build time in local time zone?
Ajoy
  • 525
  • 1
  • 9
  • 20
2
votes
2 answers

variable declaration in function return type specifier c++type

I was trying out codefights.com and noticed someones answer to a question which involved giving all the longest strings in a vector do this: std::vector r, allLongestStrings(std::vector a) { int b=0; for (s:a) if…
2
votes
3 answers

How to return VLA with size varying on each function instance?

I'm using a nice GCC extensions which allow us to declare VLAs inside structures. For now I found a way to pass VLAs to functions (by value) this way. I also find a way to return one but in a very limited context. The function code of this example…
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
2
votes
2 answers

Are code blocks inside parenthesis in C/C++ legal and can MSCL compile it?

I have the following code: int x; x = ({ 1; 2; 3; }); printf("%d\n", x); // should be 3 (If you're curious why I would ever write disgusting code like that. The answer is I'm not. I'm writing a generator that outputs C code, and having such a…
Yifan
  • 4,867
  • 5
  • 25
  • 24
2
votes
2 answers

is it safe to relegate __builtin_expect to an inline function?

I'm workon on some C++ code which defines #define LIKELY(x) (__builtin_expect((x), 1)) and I was wondering - why not an inline function? i.e. why not template inline T likely(T x) { return __builtin_expect((x), 1); } (or…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
0 answers

GCC's statement-expressions - what's their history w.r.t. C standard adoption?

With gcc, the following is a valid C statement: foo ({bar(1); baz = 2;}) Which is equivalent to bar(1); baz = 2; foo(2); Edited: Here are my (hopefully more informative) questions: Was the ISO C standard committee/body petitioned to make these…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
6 answers

How to pass a VLA to a function template?

I have the following code which could not be complied. using namespace std; void f(int); template void array_ini_1d(T1 (&x)[N]) { for (int i = 0; i < N; i++) { x[i] = 0; } } What is the proper way to pass the array…
2
votes
1 answer

G++ may_alias with Member Functions

How can I get the below code to compile on g++ 4.7? It will compile if I place the body of foo inline, but I don't want it inline (because the real code is a lot more complicated). struct A { void foo(); } __attribute__((__may_alias__)); void…
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
1
vote
2 answers

Test for GNU extension

I want to make a macro to return the real part of a complex number (which will work with double, float and long double types). The GNU C extension __real__ seems to fit the bill (although it is not portable unfortunately). I am trying the…
vibe
  • 333
  • 1
  • 9
1
vote
1 answer

Difference between gcc __attribute__ placement

While playing around with gcc __attribute__ for functions, I noticed that there is a difference in code generation depending on where I place the attribute. In below examples, I want the compiler not to optimize away my call to use(). Compiler:…
1
vote
2 answers

GCC extension __attribute__ ((unused)) for variable attributes

following is a sample code for GCC variable attribute extension, #include int main(void){ int sam __attribute__((unused))= 10; int p = sam+1; printf("\n%d" , p); } for the assembly code of above program generated…
Sameeran Joshi
  • 59
  • 1
  • 10
1
vote
1 answer

Can we mix __extension__ and -std=c99?

The standard doesn't allow to convert between pointers to void * and pointers to functions: 6.3.2.3:8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94