pointers!

pointer to an array of pointers to objects

Page 1 of 1

8 Replies - 935 Views - Last Post: 29 April 2010 - 11:07 AM Rate Topic: -----

#1 prolink007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 10

pointers!

Posted 28 April 2010 - 06:45 PM

Here is what i need:

A vector of pointers to arrays of pointers to objects. All dynamic.

Here is basically what i have so far...

vector<Example **> exampleObjects;

Example ** exampleArray = new Example*[intvalue];

Example *exampleObjectPointer = new Example;

exampleObjectPointer->setSomething();

exampleArray[0] = exampleObjectPointer;

exampleObjects.push_back(exampleArray);


What i am trying to accomplish there is:

This is all in a for loop, where i am going through and creating objects and putting them into the array and then pushing that array onto the vector. At the moment, whenever i try to view contents of the objects that i pushed onto the vector i get the first of every vector and sometimes a bunch of pointer garbage. Also, whenever i debug this, i dont see it actually create an array of pointers to objects for my "exampleArray" i only see it create one object and point exampleArray to that one object.

Please assist me.

Thanks in advance.

Oh and i cant post specific code. If you can't help with what i have posted then just dont worry about it. No need to come in here and demand more. I will give you as much as i can.

Is This A Good Question/Topic? 0
  • +

Replies To: pointers!

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: pointers!

Posted 28 April 2010 - 08:01 PM

Is there supposed to be 1 vector or multiple vectors each containing multiple arrays?

Where/when is/are the vector(s) created?

Is all of that in a function that is being called from another function? Are you creating a new vector that is local to the function and going out of scope when the function returns?

With the info you provided, all I can do is guess. Maybe this will give you a clue.
Was This Post Helpful? 0
  • +
  • -

#3 prolink007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 10

Re: pointers!

Posted 28 April 2010 - 08:05 PM

Quote

Is there supposed to be 1 vector or multiple vectors each containing multiple arrays?


1 vector containing multiple arrays

Quote

Where/when is/are the vector(s) created?


the vector is created inside of a function of a class, only once.

Quote

Is all of that in a function that is being called from another function? Are you creating a new vector that is local to the function and going out of scope when the function returns?


the vector is a private member of a class and does not go out of scope. I am only manipulating it inside the class that it is a private member of.

Hope this helps a little better.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: pointers!

Posted 28 April 2010 - 08:17 PM

Hi, vectors cannot contain array as discussed here:-

Vector of Arrays in C++

You could use a vector of vectors.

Question about storing array in a std::vector in C++
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: pointers!

Posted 28 April 2010 - 09:27 PM

View Post#define, on 28 April 2010 - 11:17 PM, said:

Hi, vectors cannot contain array as discussed here:-


I see absolutely nothing wrong in theory with a vector of pointers, even if they are pointers to pointers.

For example:
#include <iostream>
#include <vector>
using namespace std;

struct s {
  int val;
  char str[5];
  
  s(int i) : val(i) {}
};

s** create_arry(int base) {
  s **asptr = new s*[5];
  
  for( int i = 0; i < 5; ++i ) {
    s *sptr = new s(i + base);
    asptr[i] = sptr;
  }
  return asptr;
}
    
  
int main() {
  vector< s** > pvec;
  for( int i = 0; i < 4; ++i ) {
    pvec.push_back( create_arry( i *  5 ) );
  }
  
  for( int i = 0; i < 4; ++i ) {
    for( int j = 0; j < 5; ++j ) {
      cout << (*(pvec[i]+j))->val << " ";
      delete *(pvec[i]+j);
    }
    delete[] pvec[i];
  }
  cout << endl;
  return 0;
}



The problem is that there is obviously something wrong with the op's implementation -- but I don't know how to debug code that I can't see.

edit: added deletes

This post has been edited by r.stiltskin: 28 April 2010 - 10:10 PM

Was This Post Helpful? 2
  • +
  • -

#6 Penzyak  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 22-April 10

Re: pointers!

Posted 28 April 2010 - 10:26 PM

@r.stillskin: You are confusing arrays with pointers. They are not the same thing in C++.

Vectors cannot have actual arrays inside of them, because vectors expect their elements to work with an assignment ('=') operator. Vectors of pointers (which will point to arrays located elsewhere) are fine.

By the way, I believe there is a workaround by putting arrays inside a struct and making a vector of those structs, since structs do work with '='. Try it :)
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: pointers!

Posted 28 April 2010 - 10:37 PM

@Penzyak: I'm not confusing anything. You and #define are answering a question that the OP didn't ask. The OP clearly has clearly created a series of arrays of pointers, and a vector of pointers to those pointers, but has an error someplace in his implementation. I posted a simplified working example of what he is trying to do, to show that there is nothing wrong with his concept.

This post has been edited by r.stiltskin: 28 April 2010 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 prolink007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-February 10

Re: pointers!

Posted 29 April 2010 - 06:22 AM

Thanks the both of you. I will try this asap r.s.

Thanks for the replies, i will let you know if it solves the problem.

This post has been edited by prolink007: 29 April 2010 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: pointers!

Posted 29 April 2010 - 11:07 AM

@r.stiltskin , @Penzyak : My bad, prolink007 says pointers to arrays. Thanks guys.


r.stiltskin said:

I posted a simplified working example of what he is trying to do, to show that there is nothing wrong with his concept.

Looks good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1