473,401 Members | 2,127 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,401 software developers and data experts.

Error on reallocation of a vector

Hi, all... I have a problem when trying to reallocate a vector for a second time inside a loop.
The order is somelike:

for (n=k; n<L; n++)
{
vector.resize(n, (long) 0);
...

vector.clear(); //[Or alternativelly vector.resize(1);]
}

The loop first loop is executed appropriatelly, but after first increment of n, i get a crash of the heap... (when trying to resize the vector again... with n-zeros)

Has anyone any idea?

Thanks
Aug 14 '07 #1
2 1961
I don't see why this shouldn't work, assuming of course that your vector is an std::vector<T>, where T is a built-in type (like int or long).

However, if your T is a class-type, the program may crash because the destructor is called on an uninitialized object.
Aug 14 '07 #2
However, if your T is a class-type, the program may crash because the destructor is called on an uninitialized object.
Slight correction: If your T is a class-type the program may crash because the destructor is called on an object that was not properly copy-constructed.

If your T contains pointers (and a dtor), chances are you also need to make a copy ctor (and assignment operator) that properly creates a deep copy of your object, because vector uses the copy ctor internally.

Otherwise your T's dtor's will end freeing the same memory twice, which may easily cause the error you describe.
Aug 14 '07 #3

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

Similar topics

9
by: newbiecpp | last post by:
When you insert an element into a vector, if the internal memory is already used, how does the vector allocate additional memory? It just allocates one unit to satisfy the insert, or allocates a...
6
by: Lieven | last post by:
Consider: std::vector<int> vec(100); vector<int>::iterator it(vec.begin()); for(int i(0); i < 10000000; i++){ vec.push_back(rand()); }
2
by: Dmitri Shvetsov | last post by:
Hi All, Does somebody know how to work with this class directly? I know that we could redefine some internal predefined variables in C++ to force some methods to run much faster. For example. If...
4
by: alex323 | last post by:
Hey. I must have an array that can be resized dynamically. I have coded an implementation of it using malloc/realloc, but I am getting a runtime error as seen below in GDB: *** glibc detected...
2
by: pretear_yuki | last post by:
I'm a 14 year old student in Singapore. I am doing a programming for my Research Studies in school, I got the source codes of my programme from my mentor in the National University of Singapore...
5
by: Mark | last post by:
Sorry for creating such a newbish topic, but I just can't seem to figure out what the problem is here. // main.cpp #include <cstdlib> #include <iostream> #include "Vector.h" using namespace...
4
by: Jon Rea | last post by:
I have the following code that works on Visual C++ 2005 and on GCC. bool VectorContains( const std::vector<int>& vect, int value ) { std::vector<int>::const_iterator iter; for( iter =...
5
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
2
by: brasewel | last post by:
Hi guys, I'm getting an error i cannot figure. Here is my code Header file ------------------------------------------------------------------------------------------- #ifndef Clock_H #define...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.