[!!Correction made to the second code!!]
vector<int> a;
vector<int>*p = &a;
and
vector<int>*b = new vector<int>();
I know that in first sce开发者_C百科nario, a is on stack and in second b is on heap. But, are there any other differences? Like memory consumed etc.
Yes vector b is allocated on the heap and vector a is on the stack (assuming the code is in the scope of a method) along with a 4 byte pointer also on the stack. Other differences in memory consumed would depend on the memory manager and how it allocates blocks and any internal bookkeeping required for the heap.
I know that in first scenario, a is on stack and in second b is on heap.
Both parts of that statement are wrong. Mostly because the terms stack/heap are useless in describing C++ objects.
vector<int> a;
This is an automatic storage duration object
. More commonly referred to as an automatic
object.
It is created on first use and destroyed when it goes out of scope. The definition of scope depends on context. If you are in a function it is placed on the stack and destroyed when the function exists. If it is a member of an object then it is created with the object and destroyed with the object (in this case the object could be on the heap or stack).
Conversely:
vector<int>* p = &a;
This is a pointer to an object. What it points to depends. In this case you are making it point at an automatic object (which as described above could be on the stack or heap).
Finally:
vector<int>* q = new vector<int>();
This is a pointer to an object of dynamic storage duration
. This means it is created with new and must be manually destroyed (Which is also why you never create RAW pointers, they are always wrapped in smart pointers (please read a book)). If this object is on the stack or heap depends on a lot of things as the language allows you override the default behavior (in a simple naive way you can think of it as being on the heap (but it is best to just forget the concept of heap and stack as they don't apply to C++).
It is best to think of object belonging to one of four categories:
- Static storage duration objects
- Global variables (and a few other things)
- You can think of these (until you know more) as created before main destroyed after main
- Thread Storage duration objects
- Globals associated with a thread.
- You can think of these as created with the thread destroyed after the thread
- Automatic Storage duration objects
- Nearly all other objects
- These are created when first encountered.
- Destroyed when they go out of scope.
- Scope depends on context.
- Dynamic storage duration objects
- Objects allocated with new and de-allocated with delete.
- Objects that should be contained in smart pointers or containers
Both vectors will allocate the memory for their elements on the heap. The main difference is the lifetime of your vector object. In the first case, it's on the stack and will be destroyed at the end of its scope.
In the second case, the vector will remain in memory until you call delete q.
Original question:
You are asking, what is the difference between
vector<int> a;
vector<int>*p = &a
and
vector<int> b;
vector<int>* q=&b;
In both cases you have a vector, and you declare a pointer which is initialized to point to the vector. That extra indirection is usually not a good idea.
In the declaration of p
you forgot the final semicolon, while in the declaration of q
you didn't forget that.
That's all.
Second variant of question:
You are asking, what is the difference between
vector<int> a;
vector<int>*p = &a
and
vector<int>* q= new vector<int>();
In the first cases you have a vector, and you declare a pointer which is initialized to point to the vector. That extra indirection is usually not a good idea.
In the second case you declare a pointer to a vector and initialize it to point to a zero-size vector allocated via new
. That's not a good idea ever. A vector does the memory management for you, which is much of the point: just Say No to that alluring new
.
That's all.
Cheers & hth.,
p and q are just pointers to the vector, which is initialized on the stack.
It should hold only 4 bytes on most platforms.
Err, both p and q are pointers to vectors, both being pointed to the address of a and b respectively. They are in essence the same, but I'd probably be mad at someone formatting like the second example. ;)
It's just a slight tweak in syntax spacing (and a missing semi-colon on the second line of the first vector pair.).
q is a pointer to object b which is on the stack. A pointer object is just an address( a number) while b is a vector instance
精彩评论