Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone. i have another mess.
i am sure it depends on wrong momory garbage deleting.
after having called funciton "Presentazione", when compiler reach marked line, it crash.
it's surely memory probem.

C++
<pre>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <ctime>
using namespace std;

string* mostraCarta1()
{ string* Carta1 = new string[7];
  Carta1[0]=" _____ ";
  Carta1[1]="|A    |";
  Carta1[2]="|  ^  |";
  Carta1[3]="| / \\ |";
  Carta1[4]="| \\ / |";
  Carta1[5]="|  V  |";
  Carta1[6]="|____V|";

	return Carta1;
//delete[] Carta1;	
}

string* mostraCarta2()
{	string* Carta2 = new string[7];
	Carta2[0]=" _____ "; 
	Carta2[1]="|A    |"; 
	Carta2[2]="|  ^  |"; 
	Carta2[3]="| /.\\ |";
	Carta2[4]="|(_._)|";
	Carta2[5]="|  |  |";
	Carta2[6]="|____V|";
	
	return Carta2;
//delete[] Carta2;	
}

string* mostraCarta3()
{	string* Carta3 = new string[7];
	Carta3[0]=" _____ "; 
	Carta3[1]="|A    |"; 
	Carta3[2]="|  ^  |"; 
	Carta3[3]="| ( ) |";
	Carta3[4]="|(_'_)|";
	Carta3[5]="|  |  |";
	Carta3[6]="|____V|";

	return Carta3;
//delete[] Carta3;	
}

string* mostraCarta4()
{	string* Carta4 = new string[7];
	Carta4[0]=" _____ "; 
	Carta4[1]="|A    |"; 
	Carta4[2]="| _ _ |"; 
	Carta4[3]="|( v )|";
	Carta4[4]="| \\ / |";
	Carta4[5]="|  V  |";
	Carta4[6]="|____V|";
	
	return Carta4;
//delete[] Carta4;
}

void Presentazione()
{   string Visualizza[7][5];
	string *Valori;
	Valori=mostraCarta1();
		for (int j = 0; j <7; j++)
		   {Visualizza[j][1]=*(Valori+j);
		   }	
	Valori=mostraCarta2();
		for (int j = 0; j <7; j++)
		   {Visualizza[j][2]=*(Valori+j);
		   }
	Valori=mostraCarta3();
		for (int j = 0; j <7; j++)
		   {Visualizza[j][3]=*(Valori+j);
		   }
	Valori=mostraCarta4();
		for (int j = 0; j <7; j++)
		   {Visualizza[j][4]=*(Valori+j);
		   }
	Valori=mostraCarta2();
		for (int j = 0; j <7; j++)
		   {Visualizza[j][5]=*(Valori+j);
		   }	

for (int j = 0; j <7; j++)
{cout<<Visualizza[j][1]<<endl;
 }
system("pause");
system("CLS");
for (int j = 0; j <7; j++)
{cout<<Visualizza[j][1]<<Visualizza[j][2]<<endl;
 }
system("pause");
system("CLS");
for (int j = 0; j <7; j++)
{cout<<Visualizza[j][1]<<Visualizza[j][2]<<Visualizza[j][3]<<endl;
 }
system("pause");
system("CLS");
//------------------------------CRASH!!!!!!!!!!!!!!!!!!!!
for (int j = 0; j <7; j++)
{cout<<Visualizza[j][1]<<Visualizza[j][2]<<Visualizza[j][3]<<Visualizza[j][4]<<endl;
}
system("pause");
system("CLS");
}


int main()
{Presentazione();

system("pause");
exit(0);
return 0;
}


What I have tried:

for (int j = 0; j <7; j++)
{cout<<Visualizza[j][1]<<endl;
delete Visualizza[j][1];// tried this in all four subsequent for int loop.  
}
Posted
Updated 22-Mar-20 23:18pm

1. You can only delete objects that you created on the heap. Visualizza is an array declared on the stack, so you may not delete it. Trying to do so will always result in a crash.

2. You have allocated arrays of strings in your functions mostraCarta*(), and you're storing them in the variable Valori. This is memory that should be released again, but instead you overwrite these pointers and create memory leaks.

3. To properly release the array stored in Valori, you have to use the array delete operator:
C++
delete [] Valori;
The brackets are important, because otherwise the compiler doesn't know whether the pointer is pointing to a single object or an entire array of elements.

4. Your for loops do not use the correct index values for the array Visualizza. But phil.o did already point that problem out in solution 1.

5. You should use std::vector instead of simple arrays. This class would take care of the memory, so you wouldn't need to manually realease it. it would also eliminate the need for the variable Valori, and the copying loops. E.g.:
C++
std::vector<std::string> mostraCarta3()
{	std::vector<std::string> Carta3(7); // vector with 7 elements
	Carta3[0]=" _____ "; 
	Carta3[1]="|A    |"; 
	Carta3[2]="|  ^  |"; 
	Carta3[3]="| ( ) |";
	Carta3[4]="|(_'_)|";
	Carta3[5]="|  |  |";
	Carta3[6]="|____V|";

	return Carta3;
}
...
void Presentazione()
{   std::vector<std::string> Visualizza[5];
    ...
    Visualizza[2]=mostraCarta3();
    // no need for loop; no need for variable Valori; no need to release memory!
    ...
}
 
Share this answer
 
C++
string Visualizza[7][5];

The Visualizza variable is defined as a bidimensional array, with 7 elements in the first dimension and 5 elements in the second.
This means that first dimension is indexed from 0 to 6, and second dimension is indexed from 0 to 4.

In your code, you seem to use a 1-based indexing for the second dimension. Try to subtract 1 from all second-dimension index accessors:
C++
Valori = mostraCarta1();
for (int j = 0; j < 7; j++)
{
   Visualizza[j][0] = *(Valori+j);
}	
Valori = mostraCarta2();
for (int j = 0; j < 7; j++)
{
   Visualizza[j][1] = *(Valori+j);
}
Valori = mostraCarta3();
for (int j = 0; j < 7; j++)
{
   Visualizza[j][2] = *(Valori+j);
}
Valori = mostraCarta4();
for (int j = 0; j < 7; j++)
{
   Visualizza[j][3] = *(Valori+j);
}
Valori = mostraCarta2();
for (int j = 0; j < 7; j++)
{
   Visualizza[j][4] = *(Valori+j);
}	
// Likewise for the rest of your code

There remains the issue that a new string array is created whenever you are calling one of the card-creating methods. Maybe you should begin by storing them in an array; this would allow to call these methods only once.
C++
string** mostraCartas = new string*[4];
mostraCartas[0] = mostraCarta1();
mostraCartas[1] = mostraCarta2();
mostraCartas[2] = mostraCarta3();
mostraCartas[3] = mostraCarta4();
 
Share this answer
 
Note your Carta objects are constants. You don't need to dynamically allocate memory, neither explicitely nor implicitely. For instance:
C++
#include <iostream>
#include <array>
using namespace std;




int main()
{
  using namespace std::literals::string_literals;
  const array < const array < const string, 7 >, 1> carta =
  {// here the array, for illustrative purpose, has just one item, fill it with all the required ones
    {
      " _____ "s,
      "|A    |"s,
      "|  ^  |"s,
      "| / \\ |"s,
      "| \\ / |"s,
      "|  V  |"s,
      "|____V|"s
    }
  };


  for ( const auto & c : carta )
    for ( const auto s : c )
      cout << s << "\n";

}
 
Share this answer
 
hi everyone;
your suggestions solved my mess up.
now i have another question, but is bit broad:
suppose i want to set a strings array as a result of different row vector toghether;
let s assume i want to gourp 3 strings vectors, 1) with 5 characters, 2) 8 characters 3) 2 characters.
the result would be and "assimetric " array. in maths this is overcome by changing the missings values of smaller vector respect the greater, with 0
example
V1 [5]= {a,a,a,a,a}
V2 [8]={a,a,a,a,a,a,a,a}
V3 [2]={a,a}
in maths i would have
Array[8][1]={a,a,a,a,a,0,0,0}
Array[8][2]={a,a,a,a,a,a,a,a,}
Array[8][3]={a,a,0,0,0,0,0,0}
will the compiler act in the same way?? thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900