473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to delete memory for std::vector<unsigned char>

1
Hi All,

I am using one library which returns allocated memory for std::vector<unsigned char> myVar. something like std::vector<unsigned char> myVar = getData();

Now my problem is how to delete memory for std::vector<unsigned char> myVar.
Feb 4 '13 #1
1 2866
Banfa
9,065 Expert Mod 8TB
C++11 introduces std::vector::shrink_to_fit that reduces the vectors memory usage to its current size; so to release all memory you could resize the vector to 0 and then call this method.

Obviously if you are still using C++03 (like me) you can't do this, the standard way to release the unused memory in a vector is to swap it with vector containing only the required data.

Expand|Select|Wrap|Line Numbers
  1. std::vector<unsigned char> myVar = /* Some data */;
  2.  
  3. // resize myVar to a smaller size
  4.  
  5. std::vector<unsigned char> temp = myVar; // only used items are copied to temp
  6.  
  7. std::swap(temp, myVar);  // myVar contains what was in temp and temp contains what was in my var
  8.  
  9. // At this point you can let temp go out of scope and the excess data it is using is released.
  10.  
if you just want to get rid of all the data in myVar then create temp as an empty vector std::vector<unsigned char> temp;

It's a bit of a kludge which is why the shrink_to_fit method was introduced.
Feb 5 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chris E. Yoon | last post by:
I just want to hear people's opinions on this subject. My application has lots and lots of short-lived objects that use dynamic allocation/deallocation. After implementing its functionality, I...
7
by: A | last post by:
Hi, consider this: char* aClass::printHello() { char* ptr = new char; ptr = "Hello"; return ptr;
5
by: Scott Brady Drummonds | last post by:
Hi, everyone, A coworker and I have been pondering a memory allocation problem that we're having with a very large process. Our joint research has led us to the conclusion that we may have to...
19
by: Xavier Décoret | last post by:
Hi, I long thought one could not delete a const pointer but the following code compiles under g++ and visual net: class Dummy { public: Dummy() {} ~Dummy() {}
27
by: fermisoft | last post by:
I have a strange problem in my project. I have a class like this...... class CMsg { public: void *m_body; ~CMsg()
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
4
by: singhraghvendra | last post by:
See the program below. I am trying to copy 20 bytes of data in a 10 bytes of buffer. The copy succeeds and also it displays the result correctly. But the delete a1 fails and gives an error. Can...
6
by: AS | last post by:
Hello, consider the following statements, int *pBuf = new int; //this will allocate 10*sizeof(int) memory delete pBuf // this will delete all the memory allocated to pBuf Question: How...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
4
by: Hong Chen | last post by:
I developed a large program and found the program does not really release memory after the delete operation is taken, since, according to "performance monitor", the virtual bytes used by this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.