Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to a member of a class changing

Status
Not open for further replies.

Weber1

Programmer
Sep 9, 2000
20
US
I have a problem with a class function which deletes and re-creates some members within the class (C_mystring* name and C_mystring* designation); these are just pointers to instances of my own string class (a char* and an int for the length); However, once the function exits, the values of the pointers seem to be changed as I have debugging code which prints out the addresses of the pointers as the function exits, and again after it exits. (For each one, name and designation, the pointer values are 240 greater after exitting the function.) Partial code shown (debugging code is at end of CreateUnitFromFile and at end of main both shown below):
-------------------------------------------
C_unit::CreateUnitFromFile(C_mystring* inputfilename)
delete designation;
delete name;
// ******* More?
C_mystring* filedata = new C_mystring; // string to hold file data
filedata = ReadTextFile(inputfilename->Getstring()); // Get file data

C_mystring* tempstring = new C_mystring; // temporary string to hold
// parts of file data
tempstring->CreateString(filedata->Getstring()); // copy file data to temp string

C_mystring* designation = new C_mystring;
C_mystring* name = new C_mystring;
designation->CreateString(tempstring->Search_and_Pick_Line("Designation:")); //Get designation
tempstring->CreateString(filedata->Getstring()); // copy file data to temp string
name->CreateString(tempstring->Search_and_Pick_Line("Name:")); // Get name

delete tempstring; // free memory of temporary strings
delete filedata; // free memory of temporary strings

//DEBUG
printf("\nname is: ");
printf(name->Getstring());
printf("length of name string: %i \n", name->Getlength());
printf("\ndesignation is: ");
printf(designation->Getstring());
printf("length of designation string: %i \n", designation->Getlength());
printf("address of name object is: ");
printf("%i \n", name);
printf("address of designation object is: ");
printf("%i \n", designation);

}

----------------------------------------------
(within main program which calls this function)

C_mystring* filepath = new C_mystring;
filepath->CreateString("c:\\Game Development\\Unit Data\\Generic Tank.txt");

C_unit* testunit = new C_unit;
testunit->CreateUnitFromFile(filepath);

printf("address of name object is: ");
printf("%i \n", testunit->name); //Here the value is 240 greater
printf("address of designation object is: ");
printf("%i \n", testunit->designation); //Here the value is 240 greater

------------------------------
and ideas greatly appreciated!

 
Your Code:
C_unit::CreateUnitFromFile(C_mystring* inputfilename)
delete designation;
delete name;
/* My: What is here deleting? Have You created it before with "new"? If not, Windows does not like it - it tries to delete an nonexisting object.*/
// ******* More?
To say more, I must see Your constructor for C_mystring.

 
//------------------------------------------------------------------------
// Mystring constructor
//------------------------------------------------------------------------
C_mystring::C_mystring()
{
stringdata = new char;
length = 1; // Note that a null string has length = 1 due to EOstring char
}

 
I would do it so:
C_mystring::C_mystring()
{
stringdata = new char[1];
// stringdata = new char; - it is right too!
length = 1; // Note that a null string has length = 1 due to EOstring char
}
C_mystring::C_mystring(int le)
{
if(le >= 0)
length = le+1;
else
length = 1;
stringdata = new char[length];
}

C_mystring::~C_mystring()
{
if( length > 0) //Check, if You deleted it earlier
delete [] stringdata;
length = 0;
}
A class don't like to delete own members. And so You delete object, not a member!
 
Thanks for help, but not sure that the deletions/constructions are the problem (though I do not know exactly what the machine language is doing when I exit the function CreateUnitFromFile.) I DO know that the values of the pointers are correct directly before the function exits (I print out pointer values as last statement of function.) After function exits, the pointers are changed. (Next instruction after function ends is to print out the value of pointers.) I have messed around with constructors/destructors, but no difference.

So far, what I have done is basically "patch" the code by created a couple of ints as part of the unit class to hold the pointer values, then I just update the pointers to the values of the ints when I want to get them. This has solved the problem but is not very tidy. Any ideas greatly appreciated. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top