Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualNightCreature83

Posted 15 February 2013 - 09:14 AM

Hello.

 

I'm working as java programmer and just now starting to learn c++ and irrlicht (want to create games as a hobby ;) ).

I am completely confused by stack vs heap issue.

What is the advantage of using heap? Isn't it MUCH faster and more efficient to create variales on stack and pass them by reference.

What I mean is:

 

Object obj = new Object();

 

VERSUS

 

Object obj;

method1(&obj)

 

I am also confused by memory managment in c++. When stack object is freed is there a memory "hall" in stack.  

 

Thanks 

Generally in C++ if you want by reference passing of variables you define this in the function header not in it's use.

 

 

void functionByReference(const Object& obj);
void functionByPointer(const Object* obj); //In compiled code(asm) there is no difference between this call and the one above it, the compiler does see this differently though.
void functionByValue(const Object obj);
 
//Both are called as follows
Object object;
functionByReference(object); //This call is faster then the one below it
functionByValue(object);
 
functionByPointer(&object); //You have to use the address operator here because the function is expecting a pointer to an object

 

Usually you only feed a function a "&<variable>" when you only have a stack value and the function wants a object that's passed by pointer. Even when the variable is an output parameter this is expressed in the function header and not in it's use.


#1NightCreature83

Posted 15 February 2013 - 09:13 AM

Hello.

 

I'm working as java programmer and just now starting to learn c++ and irrlicht (want to create games as a hobby ;) ).

I am completely confused by stack vs heap issue.

What is the advantage of using heap? Isn't it MUCH faster and more efficient to create variales on stack and pass them by reference.

What I mean is:

 

Object obj = new Object();

 

VERSUS

 

Object obj;

method1(&obj)

 

I am also confused by memory managment in c++. When stack object is freed is there a memory "hall" in stack.  

 

Thanks 

Generally in C++ if you want by reference passing of variables you define this in the function header not in it's use.

 

void functionByReference(const Object& obj);
void functionByPointer(const Object* obj); //In compiled code(asm) there is no difference between this call and the one above it, the compiler does see this differently though.
void functionByValue(const Object obj);
 
//Both are called as follows
Object object;
functionByReference(object); //This call is faster then the one below it
functionByValue(object);
 
functionByPointer(&obj); //You have to use the address operator here because the function is expecting a pointer to an object

 

Usually you only feed a function a "&<variable>" when you only have a stack value and the function wants a object that's passed by pointer. Even when the variable is an output parameter this is expressed in the function header and not in it's use.


PARTNERS