Should I free 'buf' after assigning a dynamic char vector to it?
I cannot decide:
Code:vector<char> vec(dynamic size,'1'); char * buf = &vec.at(0); free (buf);
This is a discussion on Freeing memory within the C++ Programming forums, part of the General Programming Boards category; Should I free 'buf' after assigning a dynamic char vector to it? I cannot decide: Code: vector<char> vec(dynamic size,'1'); char ...
Should I free 'buf' after assigning a dynamic char vector to it?
I cannot decide:
Code:vector<char> vec(dynamic size,'1'); char * buf = &vec.at(0); free (buf);
Compiler MSVC++ 2010 with Code::Blocks.
No. The memory you're pointing your buf pointer at was allocated by the std::vector object. std::vector was written to do its own memory management and thus will clean up after itself. Besides, in C++ if you have to allocate memory from the free store, you generally want to use new / new [] and delete / delete [] instead of malloc and free.
Great, thank you!
Compiler MSVC++ 2010 with Code::Blocks.