473,396 Members | 1,864 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,396 software developers and data experts.

Vectors deleting memory without loosing data

hi,

This function is used to store some data into the stl vector, after store it using push_back, and i will delete the allocated memory using delete [], but did it also delete the data i have store previously ? This function will run several time to store all the data .

void store_specsetvalue(char *d)
{
char *value ;

value= new char[strlen(d) + 1];

if (!value)
{
printf (" Memory Allocation Failure.\n");
exit (1);
}
strcpy (value, d);

specsetvalue.push_back(value);

if(value != NULL)
delete []value;
}

if i did not delete the value that i have create, it will lead to memory leak, so how i delete the memory allocate without loosing the data i have store in the vector ?

Thanks.. a lot .
Aug 20 '06 #1
2 2145
D_C
293 100+
Vectors have capacity. Even though you remove elements, it still keeps the memory and capacity. Even when you clear the vector, it keeps the memory. To completely empty a vector, swap it with an empty vector, and then it will have 0 capacity.
Aug 21 '06 #2
Banfa
9,065 Expert Mod 8TB
I suspect you have a vector of char * vector<char *>. This means that all the vector stores is a pointer so if you delete value after the push_back then you delete the memory that the vector points to.

A memory leak only occurs if you remove all references to a piece of allocated memory without deallocating it. You will not have a memory leak if you don't free value, but you will need to be careful about deleting entries from the vector. You will need to get the value for the entry you wish to remove from the vector, delete it to remove the allocated memory and then remove the entry from the vector.


D_C I thought vector had a method you could call that reduced allocated memory to the minimum required or that calling empty did that for you.
Aug 26 '06 #3

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

Similar topics

12
by: Fred Ma | last post by:
Hello, I was looking at Meyers's "Effective STL", item 23 about choosing between vectors and maps (at least that the choice for me). In many cases, using sorted vectors is faster for lookups. ...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
8
by: slurper | last post by:
if i have a vector and assign another vector to a vector variable like this: vector<int> c, k; c.push_back(1), c.push_back(2); k.push_back(3), k.push_back(4); c = k; does this work?? if...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
1
by: Alvin Bruney | last post by:
Interesting problem here. Asp.net does not immediately give back its memory. How can i force it to give back this memory? I thought of having a scheduled low priority thread running in the...
4
by: mp | last post by:
I am doing pairwise comparisons between 2 vectors of chars and permuting one vector and storing the resulting calculations in a vector<float> then I find a p-value among other stats. I have to do...
11
by: jagguy | last post by:
Could someone please tell me a use for vectors in C++. I know they are popular today but I am struggling to come up for a use. Since we now have database programs to connect to in the code, why...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
5
by: Jim | last post by:
Hi, Just wondering which is better vector<record *r; r.push_back(new record(x,y)); or vector<recordr; r.push_back(record(x,y));
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.