attractivechaos / klib

A standalone and lightweight C library
http://attractivechaos.github.io/klib/
MIT License
4.18k stars 556 forks source link

kvec : kv_a causes a compilation error #100

Open Pascal-Ortiz opened 6 years ago

Pascal-Ortiz commented 6 years ago

The example provided in the header file itself:

kv_a(int, array, 20) = 5;

doesn't compile under a C compiler, leading to the following message:

lvalue required as left operand of assignment

I think there is a bug in kv_a function in kvec.h. Lines 84-88 are currently:

#define kv_a(type, v, i) (((v).m <= (size_t)(i)? \
                          ((v).m = (v).n = (i) + 1, kv_roundup32((v).m), \
                           (v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \
                          : (v).n <= (size_t)(i)? (v).n = (i) + 1 \
                          : 0), (v).a[(i)])

#endif

hence kv_a(type, v, i) is a comma expression so cannot stand as a left value. I guess you have to remove the outer parenthesis.

EDIT In standard C, a comma expression is not a lvalue contrary to C++. So the test file kvec_test.ccprovided in the test folder compiles correctly. So the problem is a C-bug only.

jvburnes commented 4 years ago

I found the same exact error and fixed it by pulling the last comma expression out of the list and put it and the beginning of a new statement after a semicolon. will this be patched? maybe I have an old version of the code.

a-p-jo commented 2 years ago

Strange... this is a C project, so it (and examples) must have been tested with a C compiler ?

So the problem is a C-bug only.

It's not a "C bug". This is C code, so this is bug in the code. Were it a C++ project, not compiling with C would be a moot point.