Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restrict qualifier compilation error

I am using Code::Blocks 10.05, and mingw. It seems the compiler does not recognized restrict qualifier and return "error: expected ';', ',' or ')' before 'src'". Do I need to pass any compiler option in order to compile it correctly?

int inet_pton4 (const char *restrict src, unsigned char *restrict dst)

p/s: it seems mingw does not support inet_pton4, so i tried to integrate an open-source version into my code.

like image 406
twfx Avatar asked Oct 31 '11 05:10

twfx


People also ask

What is restrict type qualifier?

The restrict qualifier can be used in the declaration of a structure member. A compiler can assume, when an identifier is declared that provides a means of access to an object of that structure type, that the member provides the sole initial means of access to an object of the type specified in the member declaration.

When to use restrict in C?

In the C programming language, restrict is a keyword that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other pointer will be used to access the object to which it points.


1 Answers

If your compiler does not support the restrict keyword, just take that keyword out (a).

It's used to indicate to the compiler that you (the developer) promise that the pointers follow certain properties involving aliasing, and this, in turn, allows the compiler to perform certain optimisations that would otherwise not necessarily be safe.

If you leave off that keyword in a compiler that supports it, it prevents those optimisations (slight downside).

If you leave it off for compilers that don't support that keyword, the downside is nil (since they don't support those optimisations anyway) and the upside is considerable, as in "it will compile for you" :-)


(a) You may want to ensure you're compiling in C99 mode first. While it may be true that you're using an older gcc that doesn't understand restrict, it's equally possible that you're not compiling in C99 mode, such as with -std=c99 (gcc docs seem to indicate that restrict has been supported even back to version 3.0).

If, for some reason you cannot activate C99 mode, I think gcc has an extension that allows you to use __restrict.

like image 95
paxdiablo Avatar answered Oct 13 '22 04:10

paxdiablo