Confuse with local char*

A

ambar.shome

i have a function as listed below:

char* ltoa(char* chr)
{
char* myChr=new char[100];
strcpy(myChr,chr);
return myChr;
}

in the above code i am returning a reference or pointer to a local
variable. Still how come i am getting the expected string in the caller
function of this function. It should print some junk characters. Now ,
I know memory for "myChr" has been allocated from heap. Now, if that is
the reason why I am getting the desired string then how can I allocate
some memory for this "myChr" in local stack so that I get junk chars
when I print the returned string in my caller function of ltoa()?
 
J

Jim Langston

----- Original Message -----
From: <[email protected]>
Newsgroups: comp.lang.c++
Sent: Saturday, July 02, 2005 2:26 PM
Subject: Confuse with local char*

i have a function as listed below:

char* ltoa(char* chr)
{
char* myChr=new char[100];
strcpy(myChr,chr);
return myChr;
}

in the above code i am returning a reference or pointer to a local
variable. Still how come i am getting the expected string in the caller
function of this function. It should print some junk characters. Now ,
I know memory for "myChr" has been allocated from heap. Now, if that is
the reason why I am getting the desired string then how can I allocate
some memory for this "myChr" in local stack so that I get junk chars
when I print the returned string in my caller function of ltoa()?

Actually, you never called delete on myChr. That's why it survives.
Basically the memory that myChr is pointing to is going to survive
until you call delete on it.

Now, if you did:
char myChr[100];
strcpy(myChr,chr);
return myChr;

the memory myChr is pointing to would be cleaned up as soon as
the function line ended.

That is (using my code, not yours):

std::cout << ltoa("Test") << std::endl;
would print Test.

char* temp = ltoa("Test");
std::cout << temp << std::endl;

would probably print junk.

But with your new allocation, either would print "Test".
 
A

Amadeus W. M.

i have a function as listed below:

char* ltoa(char* chr)
{
char* myChr=new char[100];
strcpy(myChr,chr);
return myChr;
}

in the above code i am returning a reference or pointer to a local
variable. Still how come i am getting the expected string in the caller
function of this function. It should print some junk characters. Now ,
I know memory for "myChr" has been allocated from heap. Now, if that is
the reason why I am getting the desired string then how can I allocate
some memory for this "myChr" in local stack so that I get junk chars
when I print the returned string in my caller function of ltoa()?

Pointers are variables that hold addresses. They know nothing else about
the memory they point to, which can live way longer (until deleted) than
the pointer itself. It's important that you pass that address from one
function to another, and you don't lose it.

char * myChr is the variable that holds the ADDRESS of the memory you
allocate with new. The address that myChr holds is passed to the calling
function via the return myChr statement. Then myChr can die quietly and
peacefully, happy to fulfill its destiny. Nothing happens to the actual
buffer allocated with new. It outlives the function that allocated it
(until explicitly deleted) and its address (previously held by myChar) is
now in the hands of the calling function, which can do useful work on that
buffer.
 
S

Serge Paccalin

Le samedi 2 juillet 2005 à 23:26:13, (e-mail address removed) a écrit dans
comp.lang.c++ :
i have a function as listed below:

char* ltoa(char* chr)
{
char* myChr=new char[100];
strcpy(myChr,chr);
return myChr;
}

in the above code i am returning a reference or pointer to a local
variable.

No, you're not. The only local variable is 'myChr' and you're not
returning a reference nor a pointer to 'myChr'; you're returning the
content of 'myChr', that is a pointer to some dynamically allocated
memory.

You'll need to call delete[] on that pointer at some point to avoid
memory leak:

char *p = ltoa("123");
// do something with p...
delete[] p;


--
___________ 03/07/2005 09:36:06
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top