Interesting implementation: atol, atof etc


 
Thread Tools Search this Thread
Top Forums Programming Interesting implementation: atol, atof etc
# 1  
Old 04-10-2008
Interesting implementation: atol, atof etc

Hi,
Check the following code:

#include <stdio.h>

int main()
{
char *s="123.33";

double d=0.0;

d = (double)atof(s);

printf("s is %2$s, d is %1$f\n", d, s);

d = (double)strtod(s, NULL);

printf("s is %3$s, d is %1$.*2$f\n", d, 2, s);

return 0;
}

Output:
s is 123.33, d is 133592.000000
s is 123.33, d is 133592.00

Quite Obvious: I have not included <stdlib.h>
But my code still compiles, because 'libc' is anyways referenced (eg: printf).

=> What are these values returned? This implies there is a 'default' atof which has resolved the symbol reference.

Something interesting now, is i can define atof as a local function: a global symbol and use it as i like.

Now my Question/Problem

But what i was very surprised to see is:
$: nm -A /usr/lib/libc.so.1 | grep atof
/usr/lib/libc.so.1: [3266] | 207888| 12|FUNC |GLOB |0 |9
|atof

'atof' is a symbol with Global visibility in libc.

My questions:
1. Standard C functions should be defined as WEAK symbols, so that a user can override them, like in the above case.
2. However, the resolution in above case has happened due to concept of "interposition" in shared libraries.

Am I correct in concluding this?
Please advice.
# 2  
Old 04-18-2008
C assumes (grrrrr, though a good compiler can warn you) that any function it does not see a prototype for prior to your calling it takes an int and returns an int. Since you did not include stdlib.h, it makes this very incorrect assumption for both atof and strtod. Your explicit cast to double even prevents the warning from popping out (explicit casts can be evil!).

It could be that on your platform, you are truncating/misaligning/generally messing up the return value for atof because C assumes that it is int. Due to this assumption, it is performing an (int) -> (double) conversion rather than the correct (no) conversion. Of course, going into the function it is also performing a (char *) -> (int) conversion, which is unusally (but not impossibly) a source of problems too.

In fact, I just recently had a guy come to me with an odd problem which turned out to be caused by a (void *) -> (int) conversion. He said he was passing a pointer to shared memory into a function and was getting "odd" results from this function. My immediate thought that was that pointers to shared memory on our 64 bit architecture machines likely have more resolution than an int (32 bit) can store (ie the upper half of the pointer value is not just zero'd out). I assumed he must not have had included the header and therefore all pointers passed into the function were being truncated to integer resolution. Hence, the real pointer value was never making it into the function and odd results were occuring. He included the header file, all worked well again, I saved the day, lol.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

atof problem

Hi I have a small code snippet. printf("sTaTuS |%s| , |%s| => %f , %f \n",before,after,strtod(before,0),strtod(after,0)); If the variables before and after has values 1000.2234 and 1000.0234 then i get the following output. sTaTuS |1000.2234| , |1000.0234| => 0.000000 , -0.000000 ... (1 Reply)
Discussion started by: kumaran_5555
1 Replies

2. Shell Programming and Scripting

Interesting problem

Hello, So I'm utilizing the bash brace expansion feature to checkout multiple folders from cvs with ease, while excluding certain subfolders within. So I do a command like this: cvs co trunk/{mod_a,mod_b,mod_c} \!trunk/{mod_a,mod_b,mod_c}/web to checkout modules trunk/mod_a , trunk/mod_b ,... (1 Reply)
Discussion started by: neked
1 Replies

3. Shell Programming and Scripting

Here is an interesting idea...

Does anyone know how to test if an ethernet interface is alive, or accepting connections? Here is the scenario - I have rsc and sc console interfaces on some Suns. There are some sporadic vulnerability scans that send them out to lunch when they run the scans. I have to login to the host and reset... (0 Replies)
Discussion started by: lm_admin_dh
0 Replies

4. UNIX for Dummies Questions & Answers

Interesting question :-D

first off, i LOVE unix to death, but i really only like the text/console side of it. im not big into the XFree86/Xwindows side of things.. so with that known... im in search of really old *nix's.. like way outdated, floppy OS's from way back in the day. im kinda in search of the pure'r UNIX's.... (0 Replies)
Discussion started by: 01000101
0 Replies
Login or Register to Ask a Question