TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTIAN S. PEDERSEN
from: AARE TALI
date: 1997-07-28 00:00:00
subject: Conversion of pointer.

 > Behold, a function which returns a pointer:
 > int* test()
 > {
 >     int i=5;
 >     return &i;
 > }
 > Why do I get a "Suspicious pointer conversion"-warning
 > ? The functions using this concept seems to work OK.
 > Should I make them differently.
   The error message itself makes me suspicious about the quality of the 
compiler. The correct warning (or preferably error) would be 'returning 
address of the local variable' or something like that. The address itself 
would be usable, but data that is stored at that location will most probably 
be invalid sooner or later without any visible cause. Try this (should be 
valid code):
void main(void)
{
    int * ip;
    ip = test();    // ip points to the location where 5
                    // is (was) stored
    if (*ip == 5)   // can be true at this point
        printf("There is %d stored in the middle"
            " of nowhere\n", *ip);
}
   Most probably you won't get message saying that 5 is still there. printf() 
uses pretty much space in stack, enough to recycle the storage test() points 
to.
   If yoy declare i as static int in test() it would make sense because i 
will exist after test() returns address to it.
---
---------------
* Origin: A point in the middle of nowhere (2:490/31.3100)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.