Re: O3 versus O2 weirdness

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Bartlomiej,

> ..
>   char c[2]="";
> ..
>   c[0]=konfig[i];
>   l=atoi(c);
> ..

Make sure you put in
 c[0]=konfig[i];
 c[1] = '\0';
 l=atoi(c);

(Otherwise, there's no guarantee that c[1] is the nul character as desired.)

> I know it is my programming error (atoi manpage states it takes a string
> as input), but isnt there really a way of detecting such ?

Maybe.  Did you have all the warnings turned on?

But even with warnings turned on, C gives you plenty of rope to hang
yourself.  (C++ gives you even more rope.  So you can hang yourself, all
your friends, family, co-workers, and still have enough rope left over to do
the rigging of a small schooner.)

For foo(const char*), the compiler probably cannot distinguish between:
  const char c = 'x';
  const char s[] = "x";
  const char* p = "x";
  foo(&c);
  foo(s);
  foo(p);

Which one is "wrong" depends on whether foo expects a pointer to a character
(in which case they're all "right"), or foo expects a pointer to a character
array that is terminated with a nul character (in which case the first one
is incorrect in that it violates the API contract... but the compiler does
not know that).

> This was particularly easy sample but chasing such bugs in a big code
> can be a really painful task...

I agree.  The sole consolation is "job security".  :-P

In the inimitable words of Dr. Smith, "Oh, the pain... the pain of it all."

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux