+ 1

What is the difference between declaring a variable x with x=5 and assigning int *ptr= new int; *ptr=5?

related to pointers

26th Aug 2017, 1:47 PM
Ironrobot
Ironrobot - avatar
4 Answers
+ 5
The heap is not faster than the stack, its the vice versa that is true. (Sorry for my mistake). The stack is faster, as in the stack, the memory is arranged in an ordered manner. So data access is easier. In a heap, the memory is unordered, and thus separate address tables must be maintained to get the positions of the elements in the heap. But, a stack can store a limited amount of memory, as it is an ordered list and thus the OS initially allocates a limited memory for storage. And in a heap, there is no set limit in which one can store data. (as long as the RAM is not filled ).
27th Aug 2017, 7:49 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
There is just one difference. The first statement stores the variable in the stack, or the original default static memory location. it is in continuous use by the OS. The second statement(s) stores the variable in the heap, a dynamic memory. The new operator helps achieve this task.
26th Aug 2017, 3:11 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
how is it faster?
26th Aug 2017, 4:03 PM
Ironrobot
Ironrobot - avatar
+ 2
thanks
27th Aug 2017, 1:25 PM
Ironrobot
Ironrobot - avatar