Advertisement

STL vector

Started by May 15, 2001 04:53 PM
14 comments, last by TeTE 23 years, 8 months ago
In a program that I''m writing if I use vectors It crashes in a compiler internal function related to memory reallocation. But if I use lists it works correctly. I''m using VisualC++ 6.0 SP5. My doubt is: can you resize an element in a std::vector after inserting it?
I''ve never had any problems doing it. My current game uses a vector of vectors of lists without any problems.

Robert Warden
Lasershot Shooting Simulations

Bobby Ward - COM Guru in training
Advertisement
hmmm .mine works ..

where exactly does it crash ???

how are u resizing it ??

{ Stating the obvious never helped any situation !! }
What exactly was the error?
There are a lot of way to trash ram.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
You can resize them but the vector are likely to move around some elements since it want everything stored in a sequence. So if you use pointers to the elements somewhere in your program these will not be right anymore.

Snale
+--My humble and superior homepage
It will help if you give the compiler error message, and some relevant code that causes the error (although with STL compiler errors the actual error could be far from what the compiler reports).
Advertisement
Am I correct in reading "resize an element" as allocate more or less memory for what is actually stored at a location in the vector? That''s a no-no.

Vectors are a lot like static arrays, in that they are contiguously allocated memory. If you try to resize an element, bad things happen. Lists are separate locations strung together by pointers, so I don''t think they have the same problem.

What you want to do is use a vector of pointers to your objects, because the pointers have a fixed size. Then you can resize the object they point to with no problems, as long as you have properly allocated the memory.

Visit my web site.
"I came, I saw, I coded."
Visit my web site."I came, I saw, I coded."
I have no idea what "resize an element" means. The elements you add to a vector are fixed sizes. Either they contain an object, or a pointer, or whatever, but those are always the same size. You can realloc the memory the pointer points to all you want. You can realloc an internal pointer in an object that points to memory. You can resize the vector itself. I know of no ''resize element'' operation

Douglas Sutherland

"Brain and Brain! What is Brain!" - miniskirted alien chick from original StarTrek

Ditto what everyone else says: "resizing an element" makes no sense.

If I had to guess wildly, I''d say you''ve done one of two things:
1) You have a vector of base class objects and you''re trying to insert derived objects. This won''t work in STL. STL contains objects "in size", i.e. uses the sizeof your template type argument. If you want to have a vector of base class objects and want it to also hold derived class objects, you need to make it a vector of base class pointers. In code:
  vector<BaseClass> vec; // cannot hold a DerivedClassvector<BaseClass *> vec; // can hold a BaseClass * that points to a derived class  


2) You''re storing iterators and then either doing a resize or push_back. Although random iterators, such as vector supplies, can be stored and reused later, the iterators can become invalidated via the push_back, insert, resize and reserve commands (and possibly some others--the reference says which)
I''m desperated !!!!!!!!!!!!!

Each time I compile the project changing only the file to parse
(a Wavefront OBJ) the error changes !!! So I can''t debug it
correctly.

Sometimes the error is in a string::compare() that calls a _Grow function that produces the error.

Now it is in a vector::push_back() -> ... -> operator new()
-> ... -> __sbh_alloc_block() -> ERROR. (The sbh stands for
small-block heap.)

When the next time?

Well it seems that when I run the program from the IDE it makes
the string error. The push_back error is for the other case.

This topic is closed to new replies.

Advertisement