Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9141515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:39:18+00:00 2026-06-17T09:39:18+00:00

Below are 3 functions. main() prints out as expected. Now, in mycharstack() the string

  • 0

Below are 3 functions. main() prints out as expected.
Now, in mycharstack() the string is stored on stack I guess, so as “ch” goes out of scope, it should not be able to return the string. How does it work correctly?
I guess the string stored in mychar() is also on stack. Is it supposed to work correctly?
I guess there are other errors in the code and memory leaks, please let me know if any. I could do these cleaner & easier with std::string. But I want to understand what’s going on with char*.

#include <iostream>
using namespace std;

char* mychar()
{
    return "Hello";
}

char* mycharstack()
{
    char* ch = "Hello Stack";
    return ch;
}

char* mycharheap()
{
    char* ch = new char;
    ch = "Hello Heap";
    return ch;
}

int main()
{
    cout << "mychar() = " << mychar() << endl;
    cout << "mycharstack() = " << mycharstack() << endl;
    cout << "mycharheap() = " << mycharheap() << endl;

    system("PAUSE");
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:39:19+00:00Added an answer on June 17, 2026 at 9:39 am

    In C++, the string handling is different from, for example, pascal.

    char* mycharheap()
    {
        char* ch = new char;
        ch = "Hello Heap";
        return ch;
    }
    

    This does following:

    1. char* ch = new char; creates memory for ONE character, and assigns it to variable ch
    2. ch = "Hello Heap"; assigns to variable ch pointer to readonly memory, which contains bytes "Hello Heap\0". Also, the original content of variable ch is lost, resulting in memory leak.
    3. return ch; returns the pointer stored to variable ch.

    What you probably wanted is

    char* mycharheap()
    {
        char* ch = new char[11] /* 11 = len of Hello Heap + 1 char for \0*/;
        strcpy(ch, "Hello Heap");
        return ch;
    }
    

    Note the strcpy -> you’ve got memory in ch, that has space for 11 chars, and you are filling it by string from read-only portion of memory.

    There will be a leak in this case. You will need to delete the memory after writing, like:

    char* tempFromHeap = mycharheap();
    cout << "mycharheap() = " << tempFromHeap << endl;
    delete[] tempFromHeap;
    

    However, I highly don’t recommend doing this (allocating memory in callee and deleting in caller). For this situations, there are, for example, STL std::string, another common and more reasonable approach is allocating in caller, passing to callee, which ‘fills’ the memory with result, and deallocating in caller again.

    What will result in undefined behavior is following:

    char* mycharstack()
    {
        char[] ch = "Hello Heap"; /* this is a shortcut for char[11] ch; ch[0] = 'H', ch[1] = 'e', ...... */
        return ch;
    }
    

    This will create array on stack with bytes "Hello Heap\0", and then tries to return pointer to first byte of that array (which can, in calling function, point to anything)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the code below I have 3 functions (including main ). The receive_loop function
My code below takes a URL inputted by the user and prints out the
Of the below three functions: getc getchar & scanf which is the best one
consider the two template functions below: template <typename T, typename A> T* create(A i)
Below program contains two show() functions in parent and child classes, but first show()
The script below resides in my theme's functions.php file. It is designed to show
some times i see that functions are defined as below: read_dir(dir) char *dir; {
I have two jQuery functions. The first one below I want to run first,
Am interoping a c++ dll and am attempting to access it's functions. Below is
The code below is from the well-known article Smashing The Stack For Fun And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.