c99

How to define a function to be inline internal and external copy in C99

My library contains a function that is used both internal and external. The function is so small that I want the compiler to try to inline function when called internal. Because the function uses information of an incomplete type external calls cannot be inlined. So my module should also always contain a copy of the function with externa...

i=i-- -- does gcc document, which will be done

Hello Once again, our best loved "i=i--" -like issues. In C99 we have: 6.5 Expressions #2: Between the previous and next sequence point an object shall have its stored value modified at most once 70) This paragraph renders !!undefined!! statement expressions such as i = ++i + 1; But for undefinded behavior there ca...

Is this code undefined behavior?

Hi, after reading about sequence points, I learned that i = ++i is undefined. So how about this code: int i; int *p = &i; int *q = &i; *p = ++(*q); // that should also be undefined right? Let's say if initialization of p and q depends on some (complicated) condition. And they may be pointing to same object like in above ca...

Variadic macros with 0 arguments in C99

I have some debugging code that looks like the following: #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) #define AT __FILE__ ":" TOSTRING(__LINE__) void __my_error(const char*loc, const char *fmt, ...); #define my_error(fmt, ...) __my_error(AT, fmt, ##__VA_ARGS__) The last macro is used so I can insert the location into the ...

Does either ANSI C or ISO C specify what -5 % 10 should be?

I seem to remember that ANSI C didn't specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly? ...

Pointer mismatch for actual parameter?

I have a function which creates an array of pointers. The function which allocates the memory returns the new memory pointer through a parameter passed to the function. The simplest code which can reproduce the problem is as follows: void foo (void** new_mem, size_t bytes) { *new_mem = malloc(bytes); } int main (void) { int**...

Smart Pointer Implementation in C

Possible Duplicate: Smart pointers/safe memory management for C? I have an embedded application where I am allocating an object in dynamic memory and passing it around to other modules. I would like to create a smart pointer to this object. There are many examples in C++ for using and implementing smart pointers. I am l...

Is stdout line buffered, unbuffered or indeterminate by default?

Section 7.9.13/7 of c99 states that: At program start-up, three text streams are predefined and need not be opened explicitly - standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is ...

Memory allocation/reallocation question

I just finished working out a memory allocation problem with the current program I'm writing, but I am not happy with what I had to do to fix it. In my program, I was building up an array of structs, reallocating space for the array every time I wanted to add a struct to it. Here is a generic version of my struct and the function that ...

Token pasting in C

Hi all, After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) ...

Declaring an array of negative length

What happens in C when you create an array of negative length? For instance: int n = -35; int testArray[n]; for(int i = 0; i < 10; i++) testArray[i]=i+1; This code will compile (and brings up no warnings with -Wall enabled), and it seems you can assign to testArray[0] without issue. Assigning past that gives either a segfault ...

Var arg list in main

I want to use my program like this: ./program -I /usr/include/ /usr/bin/ /usr/local/include/ ... Where the switch can go on and on like in a var args list. How could I do that in C99? Preferably get a something like char **args_list or char *args_list[] that contains all of the things like /usr/include and /usr/bin/. ...

In C, if this ain't an address constant, what is it?

What, exactly, is numbers in the following declaration, if it is not an address constant? int main() { int numbers[3] = {1,2,3}; return 0; } Disassembling the program shows that 1, 2, and 3 are dynamically placed on the local stack space, rather than the whole array being treated as a constant. Hence, {1,2,3} does not have sta...

Restricted pointer questions

I'm a little confused about the rules regarding restricted pointers. Maybe someone out there can help me out. Is it legal to define nested restricted pointers as follows: int* restrict a; int* restrict b; a = malloc(sizeof(int)); // b = a; <-- assignment here is illegal, needs to happen in child block // *b = rand(); while(1) {...

Restricted pointer assignments

I have a question regarding restricted pointer assignments. See the comments in code for specific questions. Overall, I'm just wondering what's legal with restrict (I've read the standard, but still have questions :-( int* Q = malloc(sizeof(int)*100); { int* restrict R = Q; for(int j = 0; j < rand()%50; j++) { R[...

Why are no strict-aliasing warnings generated for this code?

I have the following code: struct A { short b; }; struct B { double a; }; void foo (struct B* src) { struct B* b = src; struct A* a = (struct A*)src; b->a = sin(rand()); if(a->b == rand()) { printf("Where are you strict aliasing warnings?\n"); } } I'm compiling the code with the following c...

How to check C source code against the current standard?

I'm continuing to learn C and would like to adhere to whatever is the current standard, but finding a good reference to that seems to be problem. From what I've found online (mostly through Google and Wikipedia) is that the current standard used today is C99, more formally the ISO/IEC 9899:1999 standard. When I'm writing C code, I ofte...

Can I define variadic C preprocessor macros with __VA_ARGS in the middle instead of the end?

GCC complains if i do this: #define M(obj,met, ..., contents) obj##_##met(const void * self, __VA_ARGS__) { \ contents \ } Giving me these 2 reasons: error: missing ')' in macro parameter list warning: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro Apparently, C99 - style variadic macros expect the clos...

Is this an invalid use of restrict pointers?

Suppose I have large array which I calculate an index into and pass to a second function. As a simple example, something like: void foo(float* array, float c, unsigned int n) { for (unsigned int i = 0; i < n; ++i) array[i] *= c; } void bar(float* restrict array, float* restrict array2, unsigned int m, unsigned int n) { ...

-std=c99 wtf on Linux

When I try to compile this on Linux with -std=c99 gcc complains about not knowing struct timespec. However if I compile this w/o -std=c99 everything works fine. #include <time.h> void blah(struct timespec asdf) {} int main() { struct timespec asdf; return 0; } Why is this and is there a way to still get it to work with -std=c99?...