I seem to be having a few problems with pointers at the moment. At the moment I have a char pointer as a parameter in one class. The function is being used in another class and is being used to get an input file.

ClassA* CAptr = new ClassA;
char* p = "test.txt";

CAptr->Load(p);

delete p, CAptr;

the function itself

int ClassA::Load(char *FName)
{
	ifstream ipf;
	ipf.open(FName);
	char c;
	int FSize = 0;

	if(!ipf)
		return 0;
	else 
	{
		//blah blah blah
	}
}

when debugging it shows "bad ptr" and "0xcdcdcdcd" when the function is called.

i'm not really used to using char pointers but i figured i'd give them a try.

any help would be appreciated.

You should ONLY call delete to the things you call new for.

thanks for pointing that out. Thats easy enough to fix but it doesn't have an effect on the function

After changing it to this :

ClassA* CAptr = new ClassA();
char* p = "test.txt";
CAptr->Load(p);
delete CAptr;

does it still give you a runtime error ?

After changing it to this :

ClassA* CAptr = new ClassA();
char* p = "test.txt";
CAptr->Load(p);
delete CAptr;

does it still give you a runtime error ?

it hasn't made a difference

Test to see if "new" is returning a null. That is try this :

ClassA* ptr = new(nothrow) ClassA();
 if(ptr) ptr->Load("test.txt");
 else printError();

Edited 6 Years Ago by firstPerson: n/a

i think the problem is to do with pointing to the filename rather than with the pointer to another class

Can you tell me which line it fails at? Is it when you do CAptr->Load()
or is it inside the load function somewhere? I'm guessing it fails inside your load function
for some reason, I cannot say because you haven't provided me with enough code.

Edited 6 Years Ago by firstPerson: n/a

Can you tell me which line it fails at? Is it when you do CAptr->Load()
or is it inside the load function somewhere? I'm guessing it fails inside your load function
for some reason, I cannot say because you haven't provided me with enough code.

thanks the initial problems fixed now

This article has been dead for over six months. Start a new discussion instead.