Why is a new pointer deleting information from anotherone?

Pages: 12
I apologize for the words in spanish, but basicly what im trying to do in this funtion is to create a new set. But when i create a double pointer one of the parameters I pass to the function is being modified, I really dont give any idea of whats wrong. Please any help or comment.

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
void CAnalizador::Set(char* IDConjunto, char* elementos, int elem)
{
	int comas = 1;
	for(int i = 0; i < elem; i++)
	{
		if(*elementos == ',')
			comas++;
		elementos++;
	}
	elementos -= elem;
	char** conjunTemp;
	conjunTemp = new char*[comas+1];
	for(int j = 0; j < (comas+1); j++)
	{
		conjunTemp[j] = new char[WORD_LENGHT];
		for(int k = 0; k < WORD_LENGHT; k++)
			conjunTemp[j][k] = 0;
	}
	conjunTemp[0][0] = ((char)(comas+1));
	for(int i = 1; i < (comas+1); i++)
	{
		int x = 0;
		for(; *elementos != ',' && *elementos != 0; x++)
		{
			conjunTemp[i][x] = *elementos;
			elementos ++;
			cout<<conjunTemp[i][x];
		}
		conjunTemp[i][x] = 0;
		elementos ++;
		cout<<"una palabra"<< endl;
	}
	Conjunto.insert(pair<char*,char**>(IDConjunto,conjunTemp));
}
Hmm, shouldn't you reset elementos to it's original position before the loop on line 20?
Im doing it on line 10. i go back excatly the same positions i advance.
Dont know if i was clear or if you need to see something else??
Are you speaking about parameter char* elementos?

If you are not going to change it in the function then declare it as
const char* elementos
the problem is that parametr is writen for the user so im not sure if i can declare it as const char* elementos.
The fact is that this function works when i use independetly but when i try to use from another function it doesnt work, the line 12 modifies elementos

this is the other function

1
2
3
4
5
6
7
8
9
10
11
12
void CAnalizador::SetUnion(char* Nuevo, char* A, char* B)
{
	char* p = Union(A,B);
	int num = 0;
	while(*p != 0)
	{
		p++;
		num++;
	}
	p-=num;
	Set(Nuevo,p,num);
}
Is it the line 12

conjunTemp = new char*[comas+1];

???

It can change nothing.
it really does, i imagine that somewhere or somehow it is takin memory from elementos because when that line is executed elementos have diferent value
You are wrong. This statement can not change elementos.
it does, want me to put all the code here??
im also sorprise it change it thats why im asking for the reason
Don't spend the time in vain. Insert printing of variables you suspect in changing in all places in your program where you want.
i have debugged it all step by step and is when i call CAnalizador::Set from CAnalizador::SetUnion on the conjunTemp = new char*[comas+1]; that elementos is changed.
I have seen how above that line it has the value that i need and executing it, change its value.
are u ok with this two functions or wanna see the program??
It is simply impossible. Show me the value of conjunTemp after that statement and the address of elementos.
in the debugger before Line 10(of the first code)
elementos: address is 0x004ff43f
value is "a,b,c,1,2,3"
conjunTemp: doesnt exist

Line 10

elementos: address is 0x004ff43f
value is ""
conjunTemp: doesnt exist

Line 11
elementos: address is 0x004ff434
value is "a,b,c,1,2,3"
conjunTemp: declared

Line 12 same

Line 13
elementos: address is 0x004ff434
value is "a,b,c,(trash)"
conjunTemp: address is 0x003ff343
it have trash

It is impossible that at the address 0x004ff43f there will be simultaneosly "a,b,c,1,2,3" as you wrote

in the debugger before Line 10(of the first code)
elementos: address is 0x004ff43f
value is "a,b,c,1,2,3"
conjunTemp: doesnt exist


and "" as you wrote below

elementos: address is 0x004ff43f
value is ""
conjunTemp: doesnt exist


So I can not trust what you are saying.
Last edited on
char* p = Union(A,B);

What does Union return? My suspicion is that it returns the address of an object that is out of scope when it is assigned to p.
vlad: the reason that the second value of elementos is "" is tha i move myself to its last position but i think that it still have de "a,b,c,1,2,3".
im as inpresed as you i just dont know what it is.

Cire: Union returns a char* that is created inside the same function

Well, obviously it returns a char*. But does it return a char* which contains an address of something which ceases to exist when Union() returns? If it did, it would fit the symptoms perfectly.

Perhaps posting the definition of Union would be in order.
ok here is the definition of Union WORD_LENGHT and MAX_LENGHT are defines of 30 and 512

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
char* CAnalizador::Union(char* A, char* B, char* REspuesta)
{
	char Union[MAX_LENGTH];
	for(int i = 0; i < MAX_LENGTH; i++)
	{
		Union[i] = 0;
	}
	auto itA = Conjunto.find(A);
	int u = 0;
	int a = ((int)(*itA).second[0][0]);
	for(int i = 1; i < a;i++)
	{
		for(int j = 0; j < WORD_LENGHT && u < MAX_LENGTH; j++)
		{
			if(((char)(*itA).second[i][j]) != 0)
			{
				Union[u] = (*itA).second[i][j];
				u++;
			}
		}
		Union[u] = ',';
		u++;
	}
	auto itB = Conjunto.find(B);
	char* palabraB = new char[WORD_LENGHT];
	int PaB = 0;
	bool existe = false;
	bool listo = false;
	char* palabraU = new char[WORD_LENGHT];
	int b = ((int)(*itB).second[0][0]);
	for(int i = 1; i < b;i++)
	{
		int j = 0;
		for(; j < WORD_LENGHT && u < MAX_LENGTH;j++ )
		{
			if((*itB).second[i][j] == 0)
				break;
			if(((char)(*itB).second[i][j]) != 0)
			{
				palabraB[j] = (*itB).second[i][j];
				PaB++;
			}
		}
		palabraB[j] = 0;
		int k = 0;
		for(; k < MAX_LENGTH;)
		{
			if(listo == true)
			{
				break;
			}
			int z = 0;
			for(;z < WORD_LENGHT && Union[k] != ',' && Union[k] != 0; z ++)
			{
				palabraU[z] = Union[k];
				k++;
			}
			palabraU[z] = 0;
			if(Union[k] == 0 && existe == false)
			{
				for(int x = 0; x < PaB; x++)
				{
					Union[k] = palabraB[x];
					k++;
					u++;
					existe = true;
					listo = true;
				}
				Union[u] = ',';
				u++;
			}
			if(strcmp(palabraU,palabraB) == 0)
			{
				existe = true;
			}
			if(Union[k] == ',')
			{
				k++;
			}
		}
		PaB = 0;
		existe = false;
		listo = false;
	}
	Union[(u-1)] = 0;
	int c = 0;
	if(REspuesta != NULL)
	{
		for(;c<MAX_LENGTH; c++)
		{
			REspuesta[c] = Union[c];
		}
		REspuesta[c] = 0;
	}
	return Union;
}
Yeah, that is what is wrong. Union goes out of scope when the function returns and the memory pointed to by the address returned is no longer valid. When the stack is modified in the later function it modifies the stuff that elementos points to (because elementos points to memory on the stack that has been recycled.)

Using containers instead of arrays is probably the simplest way to address the issue (ie return a vector or set instead of a pointer.)
Last edited on
cire is right. You return pointer to the local array which will be destroyed after exiting the function.

The statement

char Union[MAX_LENGTH];

defines local array. It will be destroyed after exiting the function.
And you return this array (as pointer to its first element) from the function.

So your code is invalid.
Last edited on
Pages: 12