Jump to content
  • Advertisement
Sign in to follow this  
ErUs

pointer problems :(

This topic is 4626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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;

tempword = (word*) GetWordsPtr( theword );
tptr = *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
I messed up. I meant:

std::vector m_Images;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!