The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
VietPad 2.0 (.NET implementation branch) iBot Software Releases - RSS News 0 04-04-2008 09:00 AM
Piping and redirection implementation mobile01 High Level Programming 7 12-20-2006 07:52 PM
need help with my implementation of cat in bash sanchopansa Shell Programming and Scripting 4 10-18-2006 03:28 AM
Socket implementation differences charlcy HP-UX 3 05-07-2005 02:54 PM
Shell Implementation clickonline1 UNIX for Dummies Questions & Answers 3 10-02-2001 01:52 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Nov 2007
Posts: 17
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-18-2008
Registered User
 

Join Date: Oct 2003
Posts: 69
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 02:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102