04-10-2008
19,
0
Join Date: Nov 2007
Last Activity: 13 December 2009, 10:44 AM EST
Posts: 19
Thanks Given: 0
Thanked 0 Times in 0 Posts
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.