C89, C99, C11: All the specifics that I know

before anything.. sizeof is an operand! 

sizeof is an operand!

sizeof is an operand!

重要なことは三回にしませんね!

int *ptr;

sizeof *ptr; = sizeof (int);

--------------the following are the original text------------------

compile in some standard by: -ansi,-std=c99,-std=c11,-std=gnu90,-std=gnu99,-std=gnu11. 

with -pedantic follows the standard strictly.

C89 is called ANSI C..

Starting from C99, we have designated initialization for struct, union and arrarys.. by struct foo foo = {.a=1}; and int a[] = {[2]=3};

Starting from C11, we have the anonymous unions(structs..)

Starting from C99, we have bool(actually _Bool) type, which is not typedefd from char like that in C89, which might causing type conversion problem: (bool) 0x100000000 = false in C89(true in C99?).

Starting from C89, void* can be implicitly converted to any pointer. So in C programming, malloc are conventionally without forced conversion. But in C++, malloc without forced conversion will prompt for warning or even error..

-----updates at 2018.5.8------ after reading torvalds/linux/Documentation/memory-barriers.txt

c11 specifies "memory location": 

A bit-field and an adjacent non-bit-field member are in separate memory locations. The same applies to two bit-fields, if one is declared inside a nested structure declaration and the other is not, or if the two are separated by a zero-length bit-field declaration, or if they are separated by a non-bit-field member declaration. It is not safe to concurrently update two non-atomic bit-fields in the same structure if all members declared between them are also (non-zero-length) bit-fields, no matter what the sizes of those intervening bit-fields happen to be.

It is about avoid race conditions while reading-modify-write of bit-fields. Examples clarified at here.

To note that the zero-length bit-field can be defined as:

struct { 
    char a;
    int b:5, 
    c:2,
    d:11, 
    :0, 
    e:8; 
    struct {int ff:8;} f; 
} 

In the example above, the struct has 4 memory locations: 

1. a

2. b,c,d

3. e

4. f

read-modify-write any one of the memory-location concurrently in threads should apply one and only one mutex.

----------------c++/c function definition difference---

in c, pass by reference can only be the form as:

int func(int *a, struct STR_SOME *p) { *a=1;...; }
int a = 0;
struct STR_SOME ss;
func(&a, &ss);

However, in cpp, you can do the following:

int func(int &a, struct STR_SOME *p) { a=1;...; }
int a = 0;
struct STR_SOME ss;
func(a, &ss); //Now a equals 1 as former

--------------------------------------

posted on 2016-10-28 11:53  三叁  阅读(258)  评论(0)    收藏  举报

导航