Pointers in a class, used in global functions.

Hi, Im currently trying to make a tool for reading txt files and extracting data from it into two different classes. One of them is a array of pointer to objects and the other one is just a normal array(for now..)

However, I have one small problem right now. I have a pointer that is made in the class for pointer to objects and it seems to reset the pointer when I go from a global function and over to a member function of the class. Here is a few code snippets:

class RaidString {
private:
string r_str;
int* tagp_start;

...

void sort_raid(RaidString rs, Raid *ra){
string buffer = "";
rs.find_tag( "RaidInfo" );
rs.find_tag( "key" );
buffer = rs.extract_data();
};

string RaidString::extract_data() {
string st;
int* strptr = tagp_start;

return st;
};

I cleaned up abit some of the parts the dont have anything to do with these issues. To explain abit further:

1. rs.find_tag("key"); returns the position on the keyword "key" in the r_str.
2. rs.extract_data() is then using the tagp_start as a starting point to where a small part of the string should be copied over to string st and then return it. Problem is: When I move into extract_data() tagp_start is suddenly a completely different place (-89375483 or something like that).

I guess there is some fundamental idea of pointers Im not getting here, but I was hoping someone here could help me understand why this isnt working.
Ive now made sort_raid() into a member of RaidString too, but it still wont work.

Why does the pointer reset even when I move from one member function to another? :( The pointer is saved in find_tag() and when debugging I can see that the pointer isnt set to crap until Im inside extract_data() :(
I'm still not 100% sure exactly what you're trying to do.

You say your pointer is not what you expect, but you don't set your pointer anywhere in the code you pasted.

I'm also not sure how an int* is supposed to help you get a position in a string. Perhaps you might just want an int to use as a character index.
Last edited on
This is only a small part of the code. I can paste it in here, but my code is probably as bad as u can get it :P

The pointer points to the correct place inside sort_raid() until I go into extract_data()

Here is the full code for the find_tag() member function(which sets the pointer), but the code is pretty messy, so dont say I didnt warn u! :P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
bool RaidString::find_tag( string tag_name ) {
	string str = r_str;
	string tag = "<", check;	// _start finds "<" , _end finds ">"
	int _start, _end, _offset;	// after _start. _offset = start of search
								
	for( _offset = 0; _offset < str.length(); _offset = _end )
	{
		_start = str.find("<", _offset);	// search for start of a tag
		_end   = str.find(">", _start);		// find the end of a tag
		check  = "";						// reset check
		int str_chk = 						// check if tag is a
		str.compare( _start + 1, 1, "/");	// end tag </xxx>

		if( str_chk != 0 )					// As long as str_chk isnt true
		{						// put body between _start & _end into check
		check.insert(0, str, _start+1, _end-_start-1);

		str_chk = check.compare(tag_name);	// compare with sent tag_name
		if(str_chk == 0)					// If they are the same make a 
			{								// pointer to it and return

				int tag_tmp1, tag_tmp2;
				
				// Sets tag_end, tagp_end(pointer to), tag_start,
				// tagp_start(pointer to) to proper settings and
				// updates them.
				tag.insert(1, check);		// add check to tag after 1
				tag.insert(tag.size(), ">");// add a > after check to tag
				tag_tmp1 = _start;			// make same address
				tagp_start = &tag_tmp1;		// set pointer properly
				tag_start = tag;
				tag_end  = tag;    
				tag_end.insert(1, "/");		// add a / after < 
				tag_tmp2 = str.find(tag_end,_offset); // search for </xx>
				tagp_end = &tag_tmp2;				  // set pointer </xx>

				int data = 0; 
				int p = *tagp_start + tag_start.length();
				for(p = p; p < *tagp_end; p++)
				{  data++;  }; 
				
				data_length = data;
				return true;				// Found a match!
			}
		else if( _end >= str.length() ){	// eof & no match: return false
			return false; 
			};
		};
	};
};
Last edited on
I tried to make my tagp_start / tagp_end pointers into string tags, but Im not as strong on pointers as I should be I guess, Ive tried a few times to do that function with string pointers, but I fail :(
You're pointing to a variable with local scope. tag_tmp1 and tag_tmp2 lose scope (and die) as soon as the if(str_chk == 0) block closes, which means your pointers no longer point to those variables at that point -- because the variables no longer exist (so there's nothing to point to). Here, your pointers become bad pointers.

Why can't you just use an int to record the position? Storing a pointer to a single int doesn't make a lot of sense in this situation.
Ah! Doh! ofc!!!

Youre saying that my tagp_start / tagp_end is pointing to the value of tag_tmp1 & tag_tmp2?

This was never the meaning! :P The whole point was to copy tag_tmp to tagp_start so that tagp_start points to what tag_tmp1 was pointing to in r_str. How would I do this?
1
2
3
4
5
// in your class
int tagp_start;

// when you want to set it:
tagp_start = tag_tmp1;


It sounds like you just want a character index to mark the position in a string. You do not need a pointer for this. Pointers point to other variables -- that's not what you need here.
Hmmm, I think I sorted it out :P made tagp_start / tagp_end into normal ints and did some changes to how find_tag() works. Looking at it in debug it seems like it works! thx! :D
Yeah, I really need to use pointers more I guess. Thx for the help :)
Topic archived. No new replies allowed.