Objects members in Stack vs. Heap

I know that the allocation and deallocation of the objects in stack are faster than the heap. I am not sure about the changing or getting the object's members. Which one is faster? For example:

class myClass{
public:
myClass(int a, int b){
var1 = a;
var2 = b;
}
int var1;
int var2;
};

int main(void){
myClass *objectInHeap = new myClass(10,15);
myClass objectInStack(20,25);

objectInHeap->var1 = 100; //Line3
objectInStack.var1 = 100; //Line4

return 0;
}

I have two objects of same class in this program. One is in the stack and the other one is in the heap. I want to change the member variables of two objects in Line3 and Line4. Which line is executed faster?
[770 byte] By [thekemal] at [2007-11-20 11:33:30]
# 1 Re: Objects members in Stack vs. Heap
As far as I am aware, they both take the same time. I guess the reason why heap allocations/deallocations are slower is because of the runtime dynamic memory allocation/deallocation overhead. Once the objects are created though, the access time should be the same for both stack and heap objects.
PredicateNormative at 2007-11-9 1:25:47 >
# 2 Re: Objects members in Stack vs. Heap
Thank you PredicateNormative. I have additional question. What about the allocating, creating and changing the local variables in the stack? If I change the code as follows:
class myClass{
public:
myClass(int a, int b){
var1 = a;
var2 = b;
}
int var1;
int var2;
};

int main(void){
myClass *objectInHeap = new myClass(10,15); //Line1
myClass objectInStack(20,25); //Line2
int localVariable; //Line3

objectInHeap->var1 = 100; //Line4
objectInStack.var1 = 100; //Line5
localVariable = 1; //Line6

return 0;
}

What are the execution speed of Line1 through Line6? What is the order?

Stack allocation and deallocation is a supplement of the hardware. By using stack pointer in the assembly code we can directly communicate with the hardware to allocate new memory spaces, so the stack memory management is faster. (correct?)
I have not seen any heap manager supply of the hardware, no any assembly instructions about it. If we think heap manager as an interface between user program and the memory, who implements this heap manager, the OS or the programming language itself?

Thank you.
thekemal at 2007-11-9 1:26:47 >
# 3 Re: Objects members in Stack vs. Heap
What are the execution speed of Line1 through Line6? What is the order?

What are you asking, how long each assignment will take, or what's faster?

Actual execution times, even in terms of machine clock ticks, are guesses, dependent too many variations.

However, the assignment off an object pointer to a dynamically allocated object's member is similar to to the stack allocated version, with occasional nods to what might be cache operation, which is also too variable to predict.

The integer assignment will be faster simply because the machine doesn't have to dereference into an object.

What are you doing that focuses on such performance questions, though? These are general inquiries about speed that are not usually at issue in application development, unless you're working on interior iterations that require optimization. Choosing stack allocations as a preference for speed might not even be an option depending on design requirements. Certainly if an object's use is local, stack is preferable, if that's what you're looking for.

The stack is usually a hardware supported notion (some rare CPU's don't have a formal stack, but you're probably not using one of them). The stack allocation is a simplification associated with the frame of a function call. There's no complicated allocation strategy - the various local objects are allocated from a contiguous block of RAM associated with the stack and that's it. Naturally that's fast.

Allocation from the heap is first 'contacted' from the CRT, but that's a wrapper for a call to the OS, eventually, which is what decides how the memory request is satisfied (or fails it). The nature of heap allocation is that one must select an appropriate contiguous block from what's available to satisfy the request ASYNCHRONOUSLY with respect to deallocations back to the heap. This is a far different puzzle than stack allocations, and its related to the fact that heap allocations are expected to survive the function call mechanism.

Another point in threaded development is the fact that the stack is thread specific, so allocations don't have a synchronization problem, they can just proceed immediately (as long as the thread is being serviced). Heap allocations, on the other hand, draw RAM from a single common resource, so all threads are serialized against each other. This has performance implications when high volumes of allocations occur in threaded systems. I have a few posts on that point and a solution.
JVene at 2007-11-9 1:27:49 >
# 4 Re: Objects members in Stack vs. Heap
Quote:
What are the execution speed of Line1 through Line6? What is the order?
What are you asking, how long each assignment will take, or what's faster?
Actual execution times, even in terms of machine clock ticks, are guesses, dependent too many variations.
-- I am asking about the comparison of group1 statements (Line1,Line2 and Line3) And seperately group2(Line4,Line5 and Line6) comparisons. Actually for group1 which one is faster? Allocation of a object from the stack, from the heap or a local variable from the stack?
My sense is that Line3<Line2<Line1.
Group2 is about going to the memory space of the object's members,and the local variable then assigning new values to them.
My answer for this group is Line6<Line5<Line4. Namely, reaching a stack local variable is faster than all. Reaching a stack object's member variable is faster than the reaching the heap object's member variable.

I was trying to understand the c++ memory management about the objects and how manage the object or local variables in the stack and heap. I could not find the enough resource about it, so I go through the some compiler lessons and assembly language to understand stack management and heap management. However, again I was not be able to get any assembly code of the heap management. I think that the best way is to see the assembly conversion of these concepts. Still I am not sure about my answers.

Everybody says heap allocation is slower than stack. Just memorizing the sentences are not enough to be a good programmer. In some cases, deep knowledge is necessary to understand what is going on in your program. That's why, I am discussing the memory management of local variables, objects, stacks and heap.
How the memory addresses of the object's member variables are calculated by compiler?
How a space is allocated in the heap, who manages this? How is the deallocation?
These are the questions that I am trying to find answers. I am thankful for any help.

Thank you.
thekemal at 2007-11-9 1:28:49 >