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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with pointers to objects

Status
Not open for further replies.

csripriya1

Programmer
May 13, 2003
35
US
hi all,
I have some problem with the pointers to objects.I have my program like this.
void chrom_manip(int max_fgs[],int len_chrom)
{
int i,dummy;
chromosome *chrs[6];
chromosome chrdummy;

ran.seedgen();
for(i=0;i<6;i++)
{
chrs=chrdummy.makechrom(max_fgs,len_chrom);
chrs->printchrom();
}
chrs[0]->printchrom();
}

The output I get is like this.
157 379 331 104 1430 163 210 372 67
173 397 270 120 1236 134 216 408 58
173 425 306 109 1240 136 230 396 63
147 453 291 113 1118 177 206 396 78
145 432 296 102 1324 131 242 462 69
178 415 274 100 1347 176 244 421 76
178 415 274 100 1347 176 244 421 76

This gives me an error message about assertion.
My another question is why does this chrs[0] takes the value of chrs[5].
Can anyone please help me with this.
thanks for the help and time.
 
The problem is that you can't assign pointers to variables that go out of scope.

chrs=chrdummy.makechrom(); points to the memory address returned from makechrom(), but the variable you want to point to is marked for deletion after the flow of execution leaves makechrom(). That, and you don't initialize the size of the array of character pointers (your program doesn't know what size should be preserved for each pointer).

If you are familiar with strings, it will be easier to manipulate an array of them.
 
to BoulderBum: there are no vars leaving their scopes in the code fragment. We don't know what assertion was fired.
ALL chrs array elements points to chrdummy (well, must be chrs = ..., not chrs = ...), if makechrom() returns pointer (very strange type?). That's why chrs[0]->printchrom() prints the LAST content of chrdummy (it's LAST value from the loop, chrs[5] points to chrdummy too).
Careless help request, but so evident error...
 
If you have a function like:

public char * chromosome::makechrom()
{
return &quot;some char *&quot;;
}


where the return value isn't static and you assign

chrs[6] = chrdummy.makechrom();

Then the return value will be out of scope by the time it is assigned to chrs[6]. The assigned (out of scope) variable may or may not show up when you need it, but it's space in memory is considered available for something else to overwrite it.

Beyond this, it looks like your chrs array elements are never initialized. E.G. chars[0] = new char[10];. Perhaps you should initialize the pointers, and consider passing the pointer to makechrom() and using strcpy() within the function as a less dangerous alternative to returning a char *.
 
hi ArkM and BoulderBum ,
Thanks for ur replies. As ArkM stated i had posted a careless help message. I understood where i went wrong. Once again thanks for your time and replies. It was helpful to figure out the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top