TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: BOGDAN SHEPTUNOV
from: PETER HAYWOOD
date: 1998-01-24 01:43:00
subject: Re: how should I return pointer to strin01:43:3201/24/98

Groovy hepcat Bogdan Sheptunov jived with All on 21 Dec 97  05:43:08!
how should I return pointer to string?'s a cool scene. Dig it!
 BS> char*
 BS> IniFile::readLine(void)
 BS> {
 BS> char buffer[NAME_MAX_LENGTH];
 BS> char *_value;
     ...
 BS> return _value;
 BS> }
 BS> A problem is here: in readline(void) _value is OK. I mean it points to
 BS> the correct line. But when "return" is called "buffer" is being
 BS> destructed, and the result of the call
  Of course that's what happens. buffer is automatic. There are
several ways to go here. You could declare buffer static (probably the
simplest way). You could allocate memory for the array dynamically
(and have the calling function free/delete it - not recomended). Or,
the one I would recomend, you could allocate an array before the call,
either statically or dynamically, and pass a pointer to that - or even
a reference - to IniFile::readLine(). Perhaps another good idea, make
buffer a class member instead of just a datum inside a function.
                                                  Wolvaen
... Since I've used up all my sick time, I'm calling in dead, Jim.
--- Blue Wave/RA v2.20
---------------
* Origin: The Gate, Melbourne Australia, +61-3-9809-5097 33.6k (3:633/159)

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