C on Windows vs Linux - differences -
this code works on windows (visual studio), on linux gcc, produces incorrect results. can spot issues? work differently on linux vs windows?
is there way of compiling on linux? rather working gcc, if can me spot issues in code act differently on linux, great. -
returning this: char output[8]; char *x = output; return x;
for one, have plenty of code returns pointers local arrays, not allowed , cause undefined behavior when pointers de-referenced later. if code works in windows, you're being lucky since code not valid.
the 2 typical solutions are:
- have caller pass in pointer (and
size_t
length value prevent overwrite, of course!) string space called function allowed work. - dynamically allocate new memory string inside function , return that. becomes caller's responsibility
free()
memory when it's no longer needed.
Comments
Post a Comment