PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

[FIXED] Where is local variable of member function created if object is created via new?

 October 20, 2022     c++, class, datamember     No comments   

Issue

Class A
{
    int a;
    int get_a()
    {
       int d;
       return a;
    }
};

A* obj_a_ptr = new A;
int c = A->get_a();

Where is int d memory allocated , in heap or stack ?


Solution

Member functions are not that different from free functions, they only implicitly get a this pointer as first parameter. So your member function is more or less equivalent to (lets forget about the fact that nothing in your A is actually accesible, because it is all private)

int get_a(A* obj)
{
   int d;
   return obj->a;
}

I hope this already answers your question. Whether obj was allocated via new or not makes no difference for d being on the stack.



Answered By - 463035818_is_not_a_number
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing