473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validity of pointers to vectors

Hi all,

I have vectors that holds pointers to other vectors, like so:

vector<whatever > x;
vector<whatever *> z;

z=&x;

Now I add something to x

x.push_back(som ething);

Will all the pointers in z still be valid? Of course I have problems with
something like this and I think not, but I didn't find anything that says
if, on using push_back, the vector allocates new memory for the whole
vector or if it just allocates new memory for the stuff that is added.
Also, I read that if you have vectors in vectors like so:

class x
{
vector<whatever > z;
}

and then
vector<x> vec;

That this often causes problems with keeping track of memory therewith
causing memory leaks? If so, is there a better way to do it?

Any help is greatly appreciated,

Jens
Jul 23 '05 #1
4 2016
Dr. J.K. Becker wrote:
Hi all,

I have vectors that holds pointers to other vectors, like so:

vector<whatever > x;
vector<whatever *> z;
Do you mean 'whatever' is a vector type?
z=&x;
That doesn't make sense.
z is a vector of pointers, not a pointer-to-vector.
Now I add something to x

x.push_back(som ething);

Will all the pointers in z still be valid?
That depends. If the push_back operation causes x to resize (and
reallocate storage), then probably no.

Of course I have problems with something like this and I think not, but I didn't find anything that says
if, on using push_back, the vector allocates new memory for the whole
vector or if it just allocates new memory for the stuff that is added.
It allocates new memory as soon as its initial capacity is too small to
hold the new object.
Also, I read that if you have vectors in vectors like so:

class x
{
vector<whatever > z;
}

and then
vector<x> vec;

This is not a vector of vector but a vector of x, which has has a vector
field. That's quite a difference.
That this often causes problems with keeping track of memory therewith
causing memory leaks? If so, is there a better way to do it?
Since no pointers and dynamic resource allocation are used in your
example, the memory won't leak.
Any help is greatly appreciated,

Jens

--
Matthias Kaeppler
Jul 23 '05 #2

"Dr. J.K. Becker" <be****@jkbecke r.de> wrote in message
news:d0******** *****@news.t-online.com...
Hi all,

I have vectors that holds pointers to other vectors, like so:

vector<whatever > x;
vector<whatever *> z;

z=&x;
I think you are confusing a pointer and a vector containing pointers.

what would make sense is something like this...
x.push_back(som ething);
and for z it would be..

int *p = new int(10);
z.push_back(p);
or even
z.push_back(&x[0]); or something like that.

Now I add something to x

x.push_back(som ething);

Will all the pointers in z still be valid? Of course I have problems with
something like this and I think not, but I didn't find anything that says
if, on using push_back, the vector allocates new memory for the whole
vector or if it just allocates new memory for the stuff that is added.
Also, I read that if you have vectors in vectors like so:
Adding anything to X, might not be related to anything in Z.
if you want something in Z, you need to add the pointer to the appropriate
vector element in X.
class x
{
vector<whatever > z;
}

and then
vector<x> vec;

That this often causes problems with keeping track of memory therewith
causing memory leaks? If so, is there a better way to do it?

Any help is greatly appreciated,

Jens

Jul 23 '05 #3
On 3/4/2005 12:20 PM, Dr. J.K. Becker wrote:
Hi all,

I have vectors that holds pointers to other vectors, like so:

vector<whatever > x;
vector<whatever *> z;

z=&x;
I think you might have made a typo in there. I'm going to assume that z
is of type vector<whatever > *.
Now I add something to x

x.push_back(som ething);

Will all the pointers in z still be valid? Of course I have problems with
something like this and I think not, but I didn't find anything that says
if, on using push_back, the vector allocates new memory for the whole
vector or if it just allocates new memory for the stuff that is added.
If x was resized as a result of the insertion, then z is now invalid. I
don't think the standard specifies exactly how the reallocation takes
place, and frankly I don't think it matters. To be safe, assume that
every insertion invalidates iterators, pointers, etc.
Also, I read that if you have vectors in vectors like so:

class x
{
vector<whatever > z;
}

and then
vector<x> vec;

That this often causes problems with keeping track of memory therewith
causing memory leaks? If so, is there a better way to do it?


This is only a problem if whatever is a pointer type. The default
destructor for class x will destruct z, which will in turn destruct each
of its elements. If whatever is a pointer, the pointer will be
destructed but the object it points to will not, thereby leaking
resources. To get around this, write your own destructor for class x to
explicitly destruct each element of z. You could also look into
boost::shared_p tr if you're able to use the Boost library.

Of course, this whole discussion is moot if whatever is not a pointer
type. STL objects clean up after themselves just fine. You can safely
create vectors of vectors of vectors ad nauseam.

Kristo
Jul 23 '05 #4
"Dr. J.K. Becker" <be****@jkbecke r.de> writes:
Hi all,

I have vectors that holds pointers to other vectors, like so:

vector<whatever > x;
vector<whatever *> z;

z=&x;
This is a compiler error.

Now I add something to x

x.push_back(som ething);

Will all the pointers in z still be valid?
Iff push_back() causes a reallocation then all pointers, references,
and iterators which refer to an element of the container are
invalidated. A reallocation occurs if size() == capacity() at the
time of the push_back(). You can use reserve() reserve enough
capacity to do a number of push_pack()s.

Example:

#include<vector >

int main()
{
std::vector<int > v;
//reserve(2) guarantees at least enough space for 2 elements to be
//added to v before reallocation.
v.reserve(2);
v.push_back(1);
int* foo= &v[0];
v.push_back(2);
//at this point, foo is still valid.

if (v.capacity() == v.size())
{
v.push_back(3);
//The 3rd push_back exceeded capacity, so
//it invalidated foo.
*foo; //undefined behavior.
}
else
{
v.push_back(3);
//In this case, v.capacity() is at least 3, so push_back does
//not force a reallocation, and foo is still valid.
*foo; //defined behavior.
}
}

Note that reserve() itself will trigger a reallocation iff the
requested size is greater than the current capacity.

My example uses pointers, but for vector, the same rules apply to
iterators and references as well.

Of course I have problems with
something like this and I think not, but I didn't find anything that says
if, on using push_back, the vector allocates new memory for the whole
vector or if it just allocates new memory for the stuff that is added.
Also, I read that if you have vectors in vectors like so:

class x
{
vector<whatever > z;
}

and then
vector<x> vec;
If this (common) usage results in memory leaks, you have a badly
broken implementation. Where I work, we use 3 different versions
of gcc, MSVC++ 2003, and hp aCC, and none of them have trouble
with constructs like this.
That this often causes problems with keeping track of memory therewith
causing memory leaks? If so, is there a better way to do it?

[snip]

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3712
by: Rex_chaos | last post by:
Hi there, I have a question about using expression template. We know that the final calculation in expression template will end up with a series of element-by-element operations. The concept can be explained with W = X o Y o Z; (here o denotes any operator) W.operator = LOR(LOR<X, o, Y>, o, Z); and in W.operator=, we have
2
2650
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:
18
3054
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary containers and sort those (only pointers have to be copied around then). However, that also means if I...
16
3378
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data of the program till the last byte. Here we find static tables, global context pointers, etc. This are the global variables of the program.
8
3613
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all we get is this and it doesn't work vector<int*> ones; ones->push_back(7); //this failed to *ones.push_back(7) ones->push_back(8);
33
4549
by: a | last post by:
Hi, I have a pointer that points to an unknown heap memory block, is it possible to check the pointer + 3 is valid or not? If it is impossible, how can I do the check? Thanks
1
2454
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors.. Sample code: //x.cpp void read(vector <int> a,vector <int> b,vector < vector <int> > c)
6
11880
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with so far: However I have found that it does not check it correctly. I just need to check the 9x9 array, which I am passing to this function against the classic sudoku rules and then return true for
160
5651
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.