Prev: Basic, basic, C question!
Next: Bad pointer on CString
From: zagreb on 17 Sep 2009 04:44 This program: #include <stdio.h> /*#include <math.h>*/ main() { int a=4; a=pow(a,4) ; printf("%d\n", a); } Prints wired result. Also you can write: a=pow(4,1,1,2,3,4,5,6,7,8,9); What exactly is this code calling? It is not standard pow function in math library.
From: Ulrich Eckhardt on 17 Sep 2009 05:16 zagreb wrote: > This program: > #include <stdio.h> > /*#include <math.h>*/ > main() { > int a=4; > a=pow(a,4) ; > printf("%d\n", a); > } You are using pow() without a declaration in sight. This tells the compiler to just guess the declaration. In this case, it will assume "int pow(int, int);". Note that the return-type is always 'int', it is not determined from the assignment. > Prints wired result. Also you can write: > a=pow(4,1,1,2,3,4,5,6,7,8,9); > > What exactly is this code calling? It is not standard pow function in > math library. Yes it is. However, that function takes two double values and returns a double. Now imagine being passed some bytes that actually are an int but interpreting them as if they were a double. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: zagreb on 17 Sep 2009 06:38 On 17 ruj, 11:16, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote: > zagreb wrote: > > This program: > > #include <stdio.h> > > /*#include <math.h>*/ > > main() { > > int a=4; > > a=pow(a,4) ; > > printf("%d\n", a); > > } > > You are using pow() without a declaration in sight. This tells the compiler > to just guess the declaration. In this case, it will assume "int pow(int, > int);". Note that the return-type is always 'int', it is not determined > from the assignment. > > > Prints wired result. Also you can write: > > a=pow(4,1,1,2,3,4,5,6,7,8,9); > > > What exactly is this code calling? It is not standard pow function in > > math library. > > Yes it is. However, that function takes two double values and returns a > double. Now imagine being passed some bytes that actually are an int but > interpreting them as if they were a double. > > Uli > > -- > C++ FAQ:http://parashift.com/c++-faq-lite > > Sator Laser GmbH > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 I understand that (about prototype). But why is vc linking math library without my request (this can not be compiled without linker command on cc platform). I tried to debug both cases (w and w/o math.h) and with math.c vc shows exact lib where code for pow is, and w/o math.h I can't see stack (i.e. I can't see where is pow function). Regards
From: Ulrich Eckhardt on 17 Sep 2009 07:22 zagreb wrote: > I understand that (about prototype). So why did you even mention that you are receiving strange results? > But why is vc linking math library without my request (this can > not be compiled without linker command on cc platform). This is part of the standard C API, so why shouldn't it? Note that a separate math library is a relict from some old platforms. A modern GCC doesn't require any additional libs. Actually, concerning VC, I believe you can tell the compiler/linker to not link the C runtime, but by default it will be linked. > I tried to debug both cases (w and w/o math.h) and with math.c vc > shows exact lib where code for pow is, and w/o math.h I can't see > stack (i.e.I can't see where is pow function). Well, you broke it and now it doesn't work. I guess that the stack is just not set up for the debugger to digest. In particular there is a thing called frame-pointer which might get clobbered by your abuse. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: zagreb on 17 Sep 2009 08:59
On 17 ruj, 13:22, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote: > zagreb wrote: > > I understand that (about prototype). > > So why did you even mention that you are receiving strange results? > > > But why is vc linking math library without my request (this can > > not be compiled without linker command on cc platform). > > This is part of the standard C API, so why shouldn't it? Note that a > separate math library is a relict from some old platforms. A modern GCC > doesn't require any additional libs. Actually, concerning VC, I believe you > can tell the compiler/linker to not link the C runtime, but by default it > will be linked. > > > I tried to debug both cases (w and w/o math.h) and with math.c vc > > shows exact lib where code for pow is, and w/o math.h I can't see > > stack (i.e.I can't see where is pow function). > > Well, you broke it and now it doesn't work. I guess that the stack is just > not set up for the debugger to digest. In particular there is a thing > called frame-pointer which might get clobbered by your abuse. > > Uli > > -- > C++ FAQ:http://parashift.com/c++-faq-lite > > Sator Laser GmbH > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 Please excuse my ignorance and define "modern GCC" TIA |