Author Topic: Pointer to an array of pointers :S  (Read 7882 times)

0 Members and 1 Guest are viewing this topic.

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Pointer to an array of pointers :S
« on: April 19, 2007 »
I'm trying to effectively make a dynamically resizable array of pointers. But I think i'm being silly.

My problem is i'm not sure how to write it out.

Code: [Select]
typedef struct
{
double x,y,z;
} Vertex3D;

typedef struct
{
Vertex3D *v0;
Vertex3D *v1;
Vertex3D *v2;

unsigned int color;
float alpha;
bool use_alpha;

} TTriangle;


class mesh
{

public:
mesh();
~mesh();
void addVertex( Vertex3D *Vertex );

private:


};

these are my basic structures. What I want to do is define an empty variable thats a pointer to an array of pointers. then, as I add verticies, I want to create a new array of pointers, copy the old pointers into the new array, add the new pointer to the end, delete the old array then update the array pointer to point to the new array of pointers.

The array of pointers would point to different verticies. So something along the lines of: -

int *VerticiesArrayPointer; // pointers to arrays are ints?

vertexCount++;
int *Verticies[vertexCount] = malloc(sizof(int)*vertexCount); // I dont know how to do this?

copy  &VerticiesArrayPointer to Verticies; // copy old array of pointers to new array of pointers
delete *VerticiesArrayPointer; // free old array of pointers memory
Verticies[vertexCount] = *newVertex; // add the new Vertex3D pointer to the end of the new array of pointers
*VerticiesArrayPointer = *Verticies; // set the pointer to the array to the new array

but I dont know how to do any of the above, or even what to search for on how to do it. I dont even know if thats just dumb :S

The reason I want to do it this way is because I dont know how big the array of verticies is going to be. Should I scrap this idea and just create a large array and not worry about memory usage?

Thnx for any help or direction.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #1 on: April 19, 2007 »
Maybe you're looking for pointer pointers, in allocating memory for an array of pointers you need a pointer to an array of pointers which would be:

vertex **Verticies[vertexCount] = (vertex**)malloc(sizof(vertex*)*vertexCount);

I think.

Fryer.

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #2 on: April 19, 2007 »
Thanks Fryer, I think it is something like that I need, but it doesnt like vertexCount not being a const, and apparently it cant convert vertex**[] to vertex**.


I'm also unsure how to define a pointer to an array i.e. Vertex3D **Verticies; in my class, can that be used as a pointer?

3DStuff.h
Code: [Select]
typedef struct
{
double x,y,z;
} Vertex3D;

typedef struct
{
Vertex3D *v0;
Vertex3D *v1;
Vertex3D *v2;

unsigned int color;
float alpha;
bool use_alpha;

} TTriangle;


class mesh
{

public:
mesh();
~mesh();
void addVertex( Vertex3D *Vertex );

private:

unsigned int vertexCount;
Vertex3D **Verticies;

};

3DStuff.cpp
Code: [Select]
#include <stdlib.h>
#include "3dStuff.h"

mesh::mesh()
{
vertexCount = 0;
}

mesh::~mesh()
{

}

void mesh::addVertex( Vertex3D *Vertex )
{
vertexCount++;

// It doesnt like this bit at all
Vertex3D **TempV[vertexCount] = (Vertex3D**)malloc( sizeof(Vertex3D*) * vertexCount );
}
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Pointer to an array of pointers :S
« Reply #3 on: April 19, 2007 »
Code: [Select]

// H file
typedef struct
{
double x,y,z;
} Vertex3D;

class mesh
{
public:
mesh();
~mesh();
void CreatVertex(int NumberOfVerticies)
void addVertex( Vertex3D *Vertex );
private:
unsigned int vertexCount;
Vertex3D*    Verticies;
};

// CPP file
void mesh::CreatVertex(int NumberOfVerticies)
{
     Verticies = new Vertex3D[NumberOfVerticies];
vertexCount = 0;
}

void mesh::addVertex( Vertex3D* Vertex )
{
Verticies[vertexCount++] = *(Vertex3D*)Vertex;
}

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #4 on: April 19, 2007 »
Thanks Emil, thats quite an interesting way of doing it and will probably work great for my purposes.

I've also had another look on the net and come up with something different. I'm not sure if this way will be problematic but I would appreciate some feedback on it.

cpp
Code: [Select]
#include <stdlib.h>
#include "3dStuff.h"

mesh::mesh()
{
vertexCount = 0;
Verticies = NULL;
}

mesh::~mesh()
{

}

void mesh::addVertex( Vertex3D *Vertex )
{
vertexCount++;

Vertex3D *TempV = NULL;
TempV = new Vertex3D[vertexCount];

if ( Verticies != NULL )
{
for ( int i = 0; i<vertexCount - 1; i++ )
{
TempV[i] = Verticies[i];
}
delete [] Verticies;
}

TempV[vertexCount - 1] = *Vertex;

Verticies = TempV;
}

h
Code: [Select]
typedef struct
{
double x,y,z;
} Vertex3D;

class mesh
{

public:
mesh();
~mesh();
void addVertex( Vertex3D *Vertex );

Vertex3D *Verticies;
private:

unsigned int vertexCount;


};

thanx
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Pointer to an array of pointers :S
« Reply #5 on: April 19, 2007 »
happy to help Tetra.  :)

Code: [Select]
// H file
typedef struct
{
double x,y,z;
} Vertex3D;

class mesh
{
public:
mesh();
~mesh();
void CreatVertex(int NumberOfVerticies)
void addVertex( Vertex3D *Vertex );
private:
    unsigned int MaxVerticies;
unsigned int vertexCount;
Vertex3D*    Verticies;
};

// CPP file
void mesh::CreatVertex(int NumberOfVerticies)
{
    Verticies = new Vertex3D[NumberOfVerticies];
vertexCount = 0;
    MaxVerticies = NumberOfVerticies;
}

void mesh::addVertex( Vertex3D* Vertex )
{
if(vertexCount > MaxVerticies)
{
       Vertex3D* NewVerticies = new Vertex3D[vertexCount+20]; // allocate eaxtra 20 Verticies per time
       memcpy(NewVerticies,Verticies,sizeof(Vertex3D)* vertexCount-1);
   delete [] Verticies;
   Verticies = NewVerticies;
}
Verticies[vertexCount++] = *(Vertex3D*)Vertex;
}


Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #6 on: April 19, 2007 »
Oh nice :) thanx.

I'd prefere to have not specified the number of verticies I want before adding the verts. although, I could initialize it with a number to begin with, taking out the CreateVertex function.

I'm not particularly interested in the speed of constructing the vertex list, as it would be part of the precalcs if I were to use it, but then, there must be a reason you do it like that? is allocating more memory bit by bit each time really slow / really bad?
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Pointer to an array of pointers :S
« Reply #7 on: April 19, 2007 »

Logically there is no different between allocate one be one vertex or allocate 20 Vertices at one.

But suppose you have a mesh that has 2000 Vertices , so you will use new and delete 2000 times , but if you allocate a 20 or more at one , then you have 2000/20=100 times , this will make it a bit faster when initializing the mesh.

I prefer that the user of my code (or I am when coding) to know every thing about what he does , so using a createVertex function will keep you know the amount of Vertices for each mesh.

Hope that help.

Ok I have to go to the bed now , see you tomorrow .

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #8 on: April 19, 2007 »
ah k,  :cheers: Emil thanx for the explination.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #9 on: April 19, 2007 »
I think you have your solution.  Using new and delete is much more intuitive than using malloc in this case

Fryer's line for malloc should have been
Code: [Select]
vertex **Verticies = (vertex**)malloc(sizof(vertex*)*vertexCount); Which would have eliminated the 'vertexCount not constant' error.  That error was telling you that you can't do something like
Code: [Select]
int i=5;
const int j=10;
int array1[i];
int array2[j];
Both of those will give an error because i and j are not constants (different from not 'const').

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #10 on: April 20, 2007 »
Ah sorry about that, Jim's right.

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #11 on: April 20, 2007 »
@Jim  :cheers: Jim for the explination.

@Fryer nps, it lead me to find the info I needed to make the other final method :)
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Pointer to an array of pointers :S
« Reply #12 on: April 20, 2007 »

Terta

I think that , these approach was done in Blitz3D basic language , and you want to do it in C/C++.

So my advice is , every language has it’s one way , I.E  Basic language is very simple and easy to use but is not so powerful or faster as C/C++.

If you want to use C/C++ I think you must read as many as C/C++ program to learn the how this language work.

For Example ,  I like BlitzMax very much , but I am using C++ , So  I have made some functions these compatible with BlitzMax  but with C++ Style , and used them with C++ style.

Hope you get my point here.

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Pointer to an array of pointers :S
« Reply #13 on: April 20, 2007 »
Yes I do understand what you are saying Emil, and I totally agree.

At the moment, i'm trying to get to grips with how c++ works. Over the years I have had a taste of many different languages but for me, it all started with M$ basic :) then delphi and many more after that.

Its true that I am trying to replicate Blitz3D, but atm, before I even consider optimizing or writing fast routines that are language specific, I think its important I get to grips with how the basics of the language work, complex that they are compared to what I'm use to.

The way I go about learning a language is to attempt to replicate something in it, then seeing how I can do the same thing in different ways for comparison. This also lets me relate one language to another and lets me integrate what I know already into the new language. I then use various examples and code to aid me in accoplishing my goal. This way I slowly build up my understanding of its structure methods and how it deals with variables.

The problem I find with relying too heavily on other code is that my own tends to end up looking the same, and I tend to be nobetter off not having understood fully everything going on. Also everyone has their own style and understaning, wich lets them take shortcuts or optimizations that you can only do if you have a full understanding of the language. Sometimes it tends to be incompatible with my way of thinking  ;D

This is why I am truly greatfull for this board and all it members. It lets me learn new stuff that I wouldnt normaly be able to. And with your good self and many others on this board willing to help me and others means I get onto a steap learning curve which is great :D

So in short :cheers: :buddies: and I hope to have something nice made for the compo in c++
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Pointer to an array of pointers :S
« Reply #14 on: April 20, 2007 »


ok , i see what you want. good luck and happy C++ coding.  :)