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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:40:57+00:00 2026-05-27T21:40:57+00:00

Say I have the following two functions: 1 int * foo() { int b=8;

  • 0

Say I have the following two functions:

1

int * foo()
{
  int b=8;
  int * temp=&b;
  return temp;
}

2

int * foo()
{
   int b=8;
   return &b;
}

I don’t get any warning for the first one (e.g. function returns address of a local variable) but I know this is illegal since b disappears from the stack and we are left with a pointer to undefined memory.

So when do I need to be careful about returning the address of a temporary value?

  • 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-05-27T21:40:58+00:00Added an answer on May 27, 2026 at 9:40 pm

    The reason you are not getting a warning in the first snippet is because you aren’t (from the compiler’s perspective) returning an address to a local variable.

    You are returning the value of int * temp. Even though this variable might be (and in this example is) containing a value which is an address of a local variable, the compiler will not go up the code execution stack to see whether this is the case.

    Note: Both of the snippets are equally bad, even though your compiler doesn’t warn you about the former. Do not use this approach.


    You should always be careful when returning addresses to local variables; as a rule, you could say that you never should.

    static variables are a whole different case though, which is being discussed in this thread.

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

Sidebar

Related Questions

Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
Say you have two functions with the following signatures: addClass( class ) addClass( class,
Lets say I have the following two PHP arrays that contain integers: $foo =
Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
Say for example, I have the following two tables: TableA { _id } TableB
If I have two domain names: altcognito.com and say I've got the other following
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
Let's say I have a variadic function foo(int tmp, ...) , when calling foo

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.