address of a vector

How to pass the address of a vector in struct

Page 1 of 1

6 Replies - 2206 Views - Last Post: 10 June 2008 - 02:37 PM Rate Topic: -----

#1 Nima221  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-May 08

address of a vector

Post icon  Posted 10 June 2008 - 12:31 PM

All,

I have a structure like this


struct a_struc{

int b;

std::vector<b_struct>* bStructPtr;

};

where b_struct is defined

struct b_struct {
int i;
char str_arr[64];
}

now in one function I try to calculate/make some data and make a vector of "b_struct" , for this reason in this function I define:

std::vector< b_struct > myB_StructVec;

for each individual node of my b_struct, I allocate memory(size of b_struct) then fill data in it(i, str_arr) , then added the node to my vector(myB_StructVec) with push_back().
At the end of function when I dont have anymore data, I want to pass the address of "myB_StructVec" to a_struct. bStructPtr of caller function. Do you think this will be valid:

a_struc. bStructPtr = &myB_StructVec;

if the address of a vector is the address of its first element?

Thanx

Is This A Good Question/Topic? 0
  • +

Replies To: address of a vector

#2 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: address of a vector

Posted 10 June 2008 - 01:32 PM

You can't overload operator&, so yes, what you are doing is valid, and will continue to be valid as long as myB_StructVec is valid itself.

No, the address of a vector is not the address of it's first element. It's the address of the vector object itself. So you would use bStructPtr in the same way that you'd directly use myB_StructVec. a_struc.bStructPtr->size() does the same thing as myB_StructVec.size().
Was This Post Helpful? 0
  • +
  • -

#3 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: address of a vector

Posted 10 June 2008 - 01:32 PM

The vector class I have does not have the operator overloaded for = It is possible to write your own class that does, but I'm not sure you would want to. I would just pass the vector itself
Was This Post Helpful? 0
  • +
  • -

#4 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: address of a vector

Posted 10 June 2008 - 01:41 PM

Passing a pointer won't require an operator= or a copy constructor. Passing a vector by reference won't require either as well.

Oh... and is b_struct supposed to represent a string? Is "i" to be the string length? If so, you can use the std::string class, which stores the size within.
Was This Post Helpful? 0
  • +
  • -

#5 Nima221  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-May 08

Re: address of a vector

Posted 10 June 2008 - 02:16 PM

[quote name='perfectly.insane' date='10 Jun, 2008 - 01:32 PM' post='367520']
You can't overload operator&, so yes, what you are doing is valid, and will continue to be valid as long as myB_StructVec is valid itself.

Then how can I have a valid pointer to vector once I return to caller of that function? at that time of return ,the defined vector is not valid and as you say the pointer in not valid either.
Basically I want to fill the vector in a subroutine and caller of that subroutine will pass the "a_struc" to that subroutine and it expects to receive a valid pointer in "a_struc. bStructPtr" but the whole vector could be not in stack at that time (cause of defining of vector object locally in subroutine)
Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: address of a vector

Posted 10 June 2008 - 02:22 PM

In that case... there are multiple options....

1.) Use new (try to avoid this one).

a_struc.bStructPtr = new vector<b_struct>();
// Manipulate the vector...
*(a_struc.bStrucPtr) = otherVector;
a_struc.bStrucPtr->push_back(xxx);

2.) Make the caller provide the vector.

void myFunc(vector<b_struct>& vec)
{
vec.push_back(xxxx);
}

// Or

void myFunc(vector<b_struct>* vec)
{
vec->push_back(xxxx);
}

3.) Make the vector a non-pointer type in a_struct and pass the a_struct by reference.

void myFunc(a_struct& s)
{
s.bVec.push_back(xxxx);
}
Was This Post Helpful? 0
  • +
  • -

#7 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: address of a vector

Posted 10 June 2008 - 02:37 PM

Oh, yes... and if the vector implementation that you're using doesn't have operator=, then using std::copy might help...

#include <algorithm>
#include <iterator>
#include <vector>

.......

vector<T> v1;
vector<T> v2;

// Populate v1.....
......

std::copy(v1.begin(), v1.end(), std::back_inserter(v2));

// Or even.... more efficiently
v2.resize(v1.size());
std::copy(v1.begin(), v1.end(), v2.begin());


This post has been edited by perfectly.insane: 10 June 2008 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1