Autor Tema: Deep C  (Leído 3112 veces)

panreyes

  • Administrator
  • *****
  • Mensajes: 2060
  • Karma: 65
    • panreyes.com
Deep C
« en: Marzo 06, 2013, 02:33:38 pm »

I've just learnt something :|http://www.pvv.org/~oma/DeepC_Scandev_Mar2013.pdf


It made me understand of all those bugs in my C practices.

handsource-dyko

  • Hero Member
  • *****
  • Mensajes: 568
  • Karma: 32
  • I'm the eye in the sky.
    • dyko designs
Re:Deep C
« Respuesta #1 en: Marzo 06, 2013, 03:00:29 pm »
He, that's a pretty interessting book. I knww C is a dangerous language, this book explains things clearly in a funny way.  No I understand why so many C programs are cryptic.... ;D

SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12536
  • Karma: 370
Re:Deep C
« Respuesta #2 en: Marzo 06, 2013, 03:32:40 pm »
I can explain you lot of this behavoir, but not in english... if you want  I can do it in spanish...

for example, is stupid said, that C reuse a var between several functions, only is the space for non-static vars...

this give you 42, too.
Código: [Seleccionar]
#include <stdio.h>
void foo(void)
{
        int a;
        printf("%d\n", a);
}
void bar(void)
{
        int b = 42;
}
int main(void)
{
        bar();
        foo();
}

the first example, is curious, but is logic... depend on stack state machine based implementation or direct interpreter...

well, who did this document?
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12536
  • Karma: 370
Re:Deep C
« Respuesta #3 en: Marzo 06, 2013, 03:45:57 pm »
I don't understand why the document said that examples, like this fail... in my linux gcc they works right

Código: [Seleccionar]
#include <stdio.h>
int main(void) {
int v[6] = {4,6,2,9};
int i = 2;
int j = i * 3 + v[i++];
printf("%d\n", j);
}

this give 8... is ok


Código: [Seleccionar]
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main()
{
        // implementation-defined
         int i = ~0;
         i >>= 1;
         printf("%d\n", i);
         // unspecified
         printf("4") + printf("2");
         printf("\n");
         // undefined
         int k = INT_MAX;
         k += 1;
         printf("%d\n", k);
         }

this give us...

-1
42
-2147483648

values are ok.

-1 is because you use int (then rotation is signed rotation)... if you define "i" as unsigned int, the values are differents.
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

panreyes

  • Administrator
  • *****
  • Mensajes: 2060
  • Karma: 65
    • panreyes.com
Re:Deep C
« Respuesta #4 en: Marzo 06, 2013, 03:57:31 pm »
If you read it thoroughly it says it may work with some compilers, but you should be aware of that possible problem for troubleshooting.


Citar
int j = i * 3 + v[i++];[/size][/color]



Because of some compiler optimizations, it's not recommended to read and change the same variable in the same expression or sentence.

SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12536
  • Karma: 370
Re:Deep C
« Respuesta #5 en: Marzo 06, 2013, 04:03:16 pm »
optimization bug of specific compiler... in gcc works fine...

and is recommended initialize vars on declaration or before his use.
Download Lastest BennuGD Release: http://www.bennugd.org/node/2