TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTIAN S. PEDERSEN
from: KEVIN YOCHUM
date: 1997-07-26 15:35:00
subject: Conversion of pointer.

 So sayeth the Book of S. Pedersen, "Conversion of pointer.."
 CSP> Behold, a function which returns a pointer:
 CSP> int* test()
 CSP> {
 CSP> int i=5;
 CSP> return &i;
 CSP> }
 CSP> Why do I get a "Suspicious pointer conversion"-warning ? The functions
 CSP> using this concept seems to work OK. Should I make them differently.
 
 int i was defined locally to the test() function.  When you return &i, 
 you're returning a memory location that has been deallocated because 
 the memory taken by i is released back to the OS when the return is 
 called.  It seems to work OK because nothing has yet over-written the
 memory location, but you're not *guaranteed* that something won't over
 write it when you try to use i.
 A better, safer, and guaranteed  way to do what you're trying to do
 follows:
 void test( int i )
 {
    i=5;
    return;
 }
 You're not getting a pointer back, but you could use &i (or whatever
 the variable name is in the calling function ).
 -=Kevin=-
 Fidonet: 1:363/309   TAGnet : 21:320/0     IntraTec: 191:670/0
 JAMNet : 75:82/1     BegNet : 44:244/702   SinNet  : 18:28/1
 Homepage:  http://www.commercialweb.com
 Internet:  kyochum@worldramp.net
 Internet:  kevin.yochum%309@satlink.oau.org
... "Forget the Joneses, I keep up with the Simpsons."
--- Blue Wave/386 v2.20
---------------
* Origin: Forethought BBS -=- Orlando, FL -=- 407-679-6561 (1:363/309)

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™.