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

  • SEARCH
  • Home
  • 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 8643463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:02:50+00:00 2026-06-12T12:02:50+00:00

Possible Duplicate: Can a local variable’s memory be accessed outside its scope? I’ve declared

  • 0

Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?

I’ve declared a C style array using pointer, and assign it a value returned by a function.

1.const char* str = chArr->readString();

Right after above, I want to cout the str as follow:

2.cout << "pointer to char is = " << str <<endl;

and the readString is:

char* CharArray::readString()
{
    std::cout << "Insert a string of max 19 length:" <<std::endl;
    char string[20];
    std::cin.getline(string,20,'\n');
    return string;
}

When I put a break point on line 2, I can see the correct result as value of str.
But the console window shows nothing, and after passing step 2, when I look at the str value, it shows something like “P÷7” or “äû:“,..

Maybe worth saying that for the str I strings with length of 4,5. Not 19 despite the length of str.

  • 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-12T12:02:54+00:00Added an answer on June 12, 2026 at 12:02 pm

    You are returning a pointer to a local variable. The memory address of this variable is made available again by the system after the function ends. So it is possible that some other code overwrites it with some random data and that’s why you are seeing gibberish.

    In order to safely do this, use:

    char* string = new char[20];

    Remember to delete [] it afterwards.

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

Sidebar

Related Questions

Possible Duplicate: Can a local variable's memory be accessed outside its scope? today i
Possible Duplicate: Can a local variable's memory be accessed outside its scope? What's the
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I saw
Possible Duplicate: Can a local variable's memory be accessed outside its scope? input: #include
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I recently
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I'm trying
Possible Duplicate: Can a local variable's memory be accessed outside its scope? Is there
Possible Duplicate: Can a local variable's memory be accessed outside its scope? #include <iostream>
Possible Duplicate: Can a local variable's memory be accessed outside its scope? I thought
Possible Duplicate: Can a local variable's memory be accessed outside its scope? When Automatic

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.