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 8352613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:00:42+00:00 2026-06-09T09:00:42+00:00

Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside

  • 0

Possible Duplicate:
Pointer to local variable
Can a local variable's memory be accessed outside its scope?

I have an interesting problem. I have a read function that returns a pointer:

char * myReadFunc() {   
    char r [10];
    //some code that reads data into r.
    return r;
}

Now, I call this function to assign info to some variables I have:

char * s;
//Some code to specify where to read from.
s = myReadFunc();

This produces results as I intended.

However, when I do this:

char * s1;
char * s2;
//Some code to specify where to read from.
s1 = myReadFunc();
//Some code to change the read location.
s2 = myReadFunc();

I get some odd results. The data is the same for both, and it is ALWAYS from the second specified read location.

So I tried some alternate code:

char * s1;
char * s2;
//Some code to specify where to read from.
char r [10];
//some code that reads data into r. IDENTICAL to myReadFunc().
s1 = r;
//Some code to change the read location.
s2 = myReadFunc();

This code produces results as I intended (s1 has data from one location, and s2 has data from another).

So, my question is, why did the latter code work, but the code above it did not?
I my guess is that somehow my function was aliased to both variables, and since it was pointing to both, it reassigned both every single time it was called. Does anyone understand the full reason for this behavior?

  • 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-09T09:00:44+00:00Added an answer on June 9, 2026 at 9:00 am

    Your readFunc function doesn’t work as you expect.

    You’re returning a pointer to an array that is only in scope in the body of your function. When the function exits, the array goes out of scope, and later attempts to access that memory invoke undefined behavior. It may appear to work under certain circumstances but is incorrect.

    Instead, in readFunc, allocate the array on the heap using new or malloc:

    // it is the responsibility of the caller to delete[] the
    //    returned buffer, but prefer to use e.g. shared_ptr
    char *myReadFunc()
    {
        char *r = new char[BUFFER_SIZE];
        //some code that reads data into r.
        return r;
    }
    
    • 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? 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? return reference
Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside
Possible Duplicate: Pointer to local variable I've read a lot of other topics on
Possible Duplicate: checking if pointer points within an array If I have an array
Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I
Possible Duplicate: Difference between pointer variable and reference variable in C++ I am reading
Possible Duplicate: What are the differences between pointer variable and reference variable in C++?

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.