Linked Questions

0 votes
3 answers
225 views

What is the Difference - Memory Allocation Wise - between Object Mat(args); and Object *Mat = new Mat(args); in C++? [duplicate]

Possible Duplicate: Proper stack and heap usage in C++? Heap vs Stack allocation I'm trying to understand why a Library I ported from Java to C++ (long and arduous editing work after using a ...
Cypher_CS's user avatar
-4 votes
1 answer
222 views

Why does the C++ compiler create an object heap instead of pushing the class object in stack? [duplicate]

On creating a class object with 'new' keyword,why is an object heap created instead of stack?Would stack not be a better option since it is faster,performs contiguous allocation and does not need ...
Ankit Peshin's user avatar
89 votes
7 answers
44k views

When is it best to use the stack instead of the heap and vice versa?

In C++, when is it best to use the stack? When is it best to use the heap?
Anthony Glyadchenko's user avatar
56 votes
6 answers
15k views

Why are the terms "automatic" and "dynamic" preferred over the terms "stack" and "heap" in C++ memory management?

Related to a lot of questions and answers on SO, I've learned that it's better to refer to objects whose lifetime is managed as residing in automatic storage rather than the stack. Also, dynamically ...
Luchian Grigore's user avatar
18 votes
7 answers
27k views

C++ Pointer Objects vs. Non Pointer Objects [duplicate]

Possible Duplicate: Why would you ever want to allocate memory on the heap rather than the stack? Test2 *t2 = new Test2(); t2->test(); Test2 t3; t3.test(); Why would I want to create a ...
user avatar
8 votes
8 answers
2k views

Why should I use malloc() when "char bigchar[ 1u << 31 - 1 ];" works just fine?

What's the advantage of using malloc (besides the NULL return on failure) over static arrays? The following program will eat up all my ram and start filling swap only if the loops are uncommented. It ...
Samuel Danielson's user avatar
8 votes
8 answers
10k views

Address of Stack and Heap in C++

Correction: I messed up with the concept of pointer address and the address the pointer points to, so the following code has been modified. And now it prints out what I want, variable a, c, i, j, k, ...
Zhongxia Zhou's user avatar
7 votes
4 answers
6k views

When should I allocate on the heap? (C++)

I don't really understand when I should be allocating memory on the heap and when I should allocate on the stack. All I really know is that allocating on the stack is faster, but since the stack is ...
amc's user avatar
  • 349
3 votes
4 answers
16k views

C++ creating and collecting structs in a loop

I want to create a struct from data harvested by line from a file. Each line necessitates a new struct, and the lines are accessed in a while loop. In C# I did this by creating anonymous structs and ...
Nona Urbiz's user avatar
  • 4,943
8 votes
5 answers
1k views

Double Linked Lists in C++

I have an assignment that requires us to implement a doubly linked list class. For some reason they defined the node struct as follows: struct node { node *next; node *prev; T *o; }; ...
Ian Burris's user avatar
  • 6,415
9 votes
1 answer
4k views

When to use unique_ptr instead of local object?

I'm wondering when we should use unique_ptr (or boost::scope_ptr) instead of local object. There are two possible cases I can think of: The object is large that exceed stack size of a thread. But in ...
Deqing's user avatar
  • 14.4k
0 votes
1 answer
8k views

C++ Classes - dot notation vs pointer [duplicate]

Possible Duplicate: What is the difference between the dot (.) operator and -> in C++? What's the difference between using dot notation and the pointer way? Instantiating an object with or ...
RoR's user avatar
  • 16.2k
0 votes
4 answers
925 views

How can I overload the new operator to allocate on the stack? [closed]

How can I overload the new operator for a class type so that it allocates the memory on the stack instead of heap (basically so that the user doesn't have to call delete afterwards). What about ...
user965369's user avatar
  • 5,573
4 votes
6 answers
717 views

Why would you ever want to have an array on the heap?

Why would you ever want to have an array on the heap? My professor gave us two reasons: To pass the array to functions, instead of passing a copy So that the array outlives the scope Can't these ...
dfg's user avatar
  • 787
0 votes
4 answers
1k views

why this error happens when I release memory for map through 'delete' statement?

In the program below I try to release memory for a std::map object. #include <map> #include <iostream> #include <unistd.h> int main() { // std::map<int, int> *mp = new std:...
beanmoon's user avatar

15 30 50 per page