Sign in to follow this  
ErUs

pointer problems :(

Recommended Posts

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;

Share this post


Link to post
Share on other sites
Why are you using void * in the first place?

Say, you want an array of your word struct:


int length = 100; // Dynamic length
word *array = new word[length]; // Allocate the array

array[0].r_word = "blah blah";

// Clean up the array
delete [] array; // Make note of the []. It tells the compiler you're deleting an array


Toolmaker

Share this post


Link to post
Share on other sites
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[i];

tempword = (word*) GetWordsPtr( theword );
tptr[i] = *tempword;

i++;

}

delete [] words[curword].bwords;

words[curword].bwords = tptr;

}



Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :) )

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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*.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry for triple posting, but my arrows wont show:

std::vector(ARROR_TO_THE_LEFT)Image*(ARROR_TO_THE_RIGHT) m_Images;

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
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*.


on intel32 machines a pointer is usually 32 bits and so are ints...

but it doesnt make much sense to say
int i;
i = (int)&someObject;
doSomeThingWithObectPtr( (Object*)i );

just declare a pointer

word *wordptr;

and set it to the address you want

wordptr = &some_word; wordptr = name_of_word_array;

Share this post


Link to post
Share on other sites
Could you explain exactly what it is you want to do? Do you want to work with a list of phrases, or something different? Currently, your code looks like you're making some kind of tree. Is this the effect you want? A little more elaborate description wold help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Do you mean something like this:

TYPE** p = new TYPE* [NUMBER_OF_ELEMENTS];

p[x] = &a;
...
delete[] p;

?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this