[wplug] Linux vs OS X version of GCC

Jonathan S. Billings billings at negate.org
Thu Apr 20 08:04:01 EDT 2006


Logan wrote:
> For class, I've been using my OS X laptop to do a bunch of assignments, 
> but a recent one had a Segmentation Fault when the professor ran it on 
> the Linux server. We had to modify that lab, so I found the error, 
> here's the offending line:
> 
> args[arg_count][strlen(args[arg_count++])] = 0;//null terminate (with a 
> self-referential strlen())
>     /* ok, here was the bug in Mac vs linux
>     *  I had "args[arg_count++][strlen(args[arg_count])] = 0;"
>     *  You can see the change above, apparently Linux increments my 
> arg_count before getting that second []
>     *  and mac gets both [][] before incrementing arg_count.
>     *  Either that, or it's gcc version 4.0.0 vs version 3.2.3.
>     *  Thus Linux segfaults when we try to get strlen(args[invalid]).
>     *  I thought C was supposed to be portable. Who's correct here?
>     */

For what it's worth, I wrote some test code on my mac and linux systems. 
  The results:

% ./test-gcc3.2.linux
Initial array:
[0][0] == a
[0][1] == aa
[1][0] == aaa
[1][1] == aaaa
Testing increment when getting the value:
[t++][strlen(test[t])] == aaaa (t starts off as 0, ends up as 1)
[t][strlen(test[t++])] == aaaa (t starts off as 0, ends up as 1)

% ./test-gcc4.0.1-macosx
Initial array:
[0][0] == a
[0][1] == aa
[1][0] == aaa
[1][1] == aaaa
Testing increment when getting the value:
[t++][strlen(test[t])] == a (t starts off as 0, ends up as 1)
[t][strlen(test[t++])] == a (t starts off as 0, ends up as 1)


-- 
Jonathan Billings <billings at negate.org>


More information about the wplug mailing list