Can the heap (free store) be corrupted by passing a vector by valueto a function?

B

bobl0456

If you mistakenly passed a vector by value to a function and the function modified the vector, could such action cause the heap to become corrupted?

It would seem to me that the answer to my question is yes.

Thanks in advance,

Bob
 
A

Alf P. Steinbach

If you mistakenly passed a vector by value to a function and the function
modified the vector, could such action cause the heap to become corrupted?

It would seem to me that the answer to my question is yes.

The use of a std::vector is irrelevant, except to the degree that it
prevents such things by taking care of memory management.

But there are modifications (in your case of vector items, but it
doesn't matter that they're in a vector) that can corrupt the heap.

For more detailed discussion see the FAQ item about how to post a
question about code that doesn't work, and follow those guidelines.


Cheers & hth.,

- Alf
 
B

Barry Schwarz

If you mistakenly passed a vector by value to a function and the function modified the vector, could such action cause the heap to become corrupted?

It would seem to me that the answer to my question is yes.

Does your vector type use standard constructors and destructors? If
so, your conclusion seems unwarranted. The creation and destruction
of the parameters should be no different than the creation and
destruction of a vector local to the function.

Is this an academic question or do you think you have actual
corruption? If you do think that, tell us why. In most cases, heap
corruption occurs when you overrun allocated memory, usually with a
subscript that is out of range.
 
B

bobl0456

To answer Barry's question, it is an academic question.

The reason I thought the heap might be corrupted is that if the function passed the vector by value causes the local copy of the vector to grow past the capacity allocated to the vector, the storage would be moved to another location on the heap. But when control returns to the calling function, the original vector will still have a pointer that is pointing to the old location on the heap.

I apologize for my imprecise language.
 
Ö

Öö Tiib

To answer Barry's question, it is an academic question.

The reason I thought the heap might be corrupted is that if the
function passed the vector by value causes the local copy of the
vector to grow past the capacity allocated to the vector, the
storage would be moved to another location on the heap. But when
control returns to the calling function, the original vector will
still have a pointer that is pointing to the old location on the
heap.

If you pass 'std::vector' by value then deep copy is made of the
vector. The original remains what it was and its internal storage
is not accessed by the copy.
 
B

bobl0456

Thanks - that answers my question. I did not realize a deep copy would be made. Thinking about it, it makes sense that such would be the case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top