Connecting Tech Pros Worldwide Help | Site Map

Confuse with local char*

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:49 AM
ambar.shome@gmail.com
Guest
 
Posts: n/a
Default 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()?


  #2  
Old July 23rd, 2005, 05:49 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: Confuse with local char*


----- Original Message -----
From: <ambar.shome@gmail.com>
Newsgroups: comp.lang.c++
Sent: Saturday, July 02, 2005 2:26 PM
Subject: Confuse with local char*

[color=blue]
>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()?
>[/color]

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


  #3  
Old July 23rd, 2005, 05:49 AM
Amadeus W. M.
Guest
 
Posts: n/a
Default Re: Confuse with local char*

On Sat, 02 Jul 2005 14:26:13 -0700, ambar.shome wrote:
[color=blue]
> 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()?[/color]

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.

  #4  
Old July 23rd, 2005, 05:49 AM
Serge Paccalin
Guest
 
Posts: n/a
Default Re: Confuse with local char*

Le samedi 2 juillet 2005 à 23:26:13, ambar.shome@gmail.com a écrit dans
comp.lang.c++*:
[color=blue]
> 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.[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.