pointer problems :(

Started by
15 comments, last by dyerseve 18 years, 5 months ago
i have a <void *> that points to an array of std::string's. i want to then have an array of pointers that are GetWordsPtr( <thestring> ); so i create a new array of pointer that point to structs which (in them) contain the string. ( dont worry about the function i have made this ). how do i make an array of pointers? ( not fixed size (array size) ) i have to use <void *> because it points to a struct of the type that it is :/ eg. typedef struct { std::string r_word; bool start; bool end; int wordsbefore; void * bwords; //ptr to array of word's int wordsafter; void * awords; //ptr to array of word's } word;
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Advertisement
Why are you using void * in the first place?

Say, you want an array of your word struct:

int length = 100; // Dynamic lengthword *array = new word[length]; // Allocate the arrayarray[0].r_word = "blah blah";// Clean up the arraydelete [] array; // Make note of the []. It tells the compiler you're deleting an array


Toolmaker

thanks alot but i dont think you understand my question. I also dont think you realise how well i know C++.

anyway i worked it out. its really messy and looks like this
if( words[curword].wordsbefore > 0 ) {    i = 0; strptr = (string *) words[curword].bwords; word *tempword;    word * tptr = new word [ words[curword].wordsbefore ];    while( i < words[curword].wordsbefore ) {        theword = strptr;        tempword = (word*) GetWordsPtr( theword );				        tptr = *tempword;				        i++;				    }    delete [] words[curword].bwords;    words[curword].bwords = tptr;}
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Quote:Original post by ErUs

typedef struct {
std::string r_word;
bool start;
bool end;
int wordsbefore;
void * bwords; //ptr to array of word's
int wordsafter;
void * awords; //ptr to array of word's
} word;


why not declare bwords and awords to be string *'s anyway?

if that is what they always are, why bother with void* s and casting?
at first they are strings, while i am loading the whole database. Then i make them pointers to the other words ( because the other words have to be in memory first :) )
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
i just realised something silly :(

-
word * tptr = new word [ words[curword].wordsbefore ];
-

i dont want to be allocating new memory :( i want to point to the words allready in the memory :(

what is the defualt type for a pointer? UINT?
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Quote:Original post by ErUs
i just realised something silly :(

-
word * tptr = new word [ words[curword].wordsbefore ];
-

i dont want to be allocating new memory :( i want to point to the words allready in the memory :(

what is the defualt type for a pointer? UINT?


That made no sense what-so-ever. A pointer can be of any type, why UINT? UINT is an unsigned int, which isnt a pointer unless you make it UINT*.
so how do i create an array of pointers ( not create memory for the things they point to ) and then change what they point to later?

so i only want to create memory for the size of the POINTER's. :S
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Quote:Original post by ErUs
so how do i create an array of pointers ( not create memory for the things they point to ) and then change what they point to later?

so i only want to create memory for the size of the POINTER. :S


I beleive that made no sense what-so-ever either. I dont believe a pointer takes space, does it? W/e, the point is, I dont know about arrays. I would use a 'vector'. Like this(in my SDL engine):

...
std::vector m_Images;
...

...
m_Images.reserve(AMOUNT_OF_POINTERS_YOU_WISH_TO_RESERVE, NULL);
...

I beleive that will do the trick.
I messed up. I meant:

std::vector m_Images;

This topic is closed to new replies.

Advertisement