Program spadne, když se volá destructor – C / C++ – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Program spadne, když se volá destructor – C / C++ – Fórum – Programujte.comProgram spadne, když se volá destructor – C / C++ – Fórum – Programujte.com

 

MiCizek0
Stálý člen
29. 3. 2013   #1
-
0
-

Ahoj, program spadne, když se volá destructor ~Image () , konkrétně řádek "delete [] pPoint;"

#pragma once

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Image
{
public:
	Image ();
	void CreatePoints (unsigned short int x, unsigned short int y);
	~Image ();
	bool LoadFile (string nameFile);
	bool SaveFile (string nameFile);
	unsigned short int GetValueFromThisPoint (unsigned short int itsPoint) const {return pPoint[itsPoint] + itsPoint;}
	void ConstructPoint ();
	void SetValueOfThisPoint (unsigned short int itsPoint, unsigned short int itsValue) {pPoint[itsPoint] = itsValue;}
	void SetPosition(unsigned short int x, unsigned short int y);
private:
	unsigned short int *pPoint;
	unsigned short int *pHeight, *pWidth;
};

class PlanetaryGeneration
{
public:
	PlanetaryGeneration ();
	PlanetaryGeneration (unsigned short int x, unsigned short int y);
	~PlanetaryGeneration ();
	bool SaveFiles ();
	bool LoadFiles ();
	string GetName(signed short int itsX, signed short int itsY);
	void SetCoordinate (signed short int itsX, signed short int itsY);
	void SetName ();
private:
	Image *pImage;
	signed short int *pX;
	signed short int *pY;
	string *pName;
};

PlanetaryGeneration::PlanetaryGeneration ()
{
	pX = new signed short int;
	pY = new signed short int;
	pName = new string;
	*pX = 0;
	*pY = 0;
	*pName = "Earth";
	pImage = new Image[9];
	for (int i = 0; i < 9; i++)
	{
	}
}

PlanetaryGeneration::PlanetaryGeneration (unsigned short int x, unsigned short int y)
{
	pX = new signed short int;
	pY = new signed short int;
	pName = new string;
	*pX = 0;
	*pY = 0;
	*pName = "Earth";
	pImage = new Image[9];
	for (int i = 0; i < 9; ++i)
	{
		pImage[i].CreatePoints(x,y);
	}
}

PlanetaryGeneration::~PlanetaryGeneration ()
{
	delete [] pImage;
	delete pX;
	delete pY;
	delete pName;
}

bool PlanetaryGeneration::SaveFiles ()
{
	int k = 0;
	string itsName;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
			itsName = GetName(i,j);
			if (itsName != "error")
			{
				if (pImage[k].SaveFile(itsName))
				{
				}
				else
				{
					return false;
				}
			}
			++k;
		}
	}
	return true;
}

bool PlanetaryGeneration::LoadFiles ()
{
	int k = 0;
	string itsName;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
			itsName = GetName(i,j);
			if (itsName != "error")
			{
				if (pImage[k].LoadFile(itsName))
				{
				}
				else
				{
					return false;
				}
			}
			++k;
		}
	}
	return true;
}

string PlanetaryGeneration::GetName(signed short int itsX, signed short int itsY) 
{
	if (itsX > 1 || itsX < -1 || itsY > 1 || itsY < -1)
	{
		cout << " Input method GetName() must be in range -1 to 1!" << endl;
	}
	else
	{
		signed short int resultX = *pX + itsX;
		signed short int resultY = *pY + itsY;
		stringstream streamX;
		stringstream streamY;
		streamX << resultX;
		streamY << resultY;
		if (resultX < 0 || resultY < 0)
		{}
		else
		{
			string wholeName = *pName + streamX.str() + "_" + streamY.str() + ".raw";
			return wholeName;
		}
	}
	return "error";
}

void PlanetaryGeneration::SetCoordinate (signed short int itsX, signed short int itsY)
{
	*pX = itsX;
	*pY = itsY;
}
void PlanetaryGeneration::SetName ()
{
	cout << "Write name of file." << endl;
	cin >> *pName;
	//cout << endl << *pName << endl;
}

//class Planet;


Image::Image ()
{
	pHeight = new unsigned short int;
	pWidth = new unsigned short int;
	*pHeight = 0;
	*pWidth = 0;
	pPoint = 0;
}

void Image::CreatePoints (unsigned short x, unsigned short int y)
{
	if (x >= 256)
	{
		if (x <= 4096)
		{
			*pHeight = x;
		}
		else
		{
			*pHeight = 4096;
		}
	}
	else
	{
		*pHeight = 256;
	}
	if (y >= 256)
	{
		if (y <= 4096)
		{
			*pWidth = y;
		}
		else
		{
			*pWidth = 4096;
		}
	}
	else
	{
		*pWidth = 256;
	}
	pPoint = new unsigned short int[*pHeight * *pWidth];
	ConstructPoint ();
}
Image::~Image ()
{
	delete pHeight;
	delete pWidth;
	delete [] pPoint;
}

bool Image::LoadFile (string nameFile)
{
	cout << endl <<"Begin Load File " << "\"" << nameFile << "\"" << "." << endl;
	unsigned long int *la = new unsigned long int;
	unsigned long int *lb = new unsigned long int;
	unsigned long int numberPoint;
	unsigned short int number;
	*lb = 0;
	ifstream inImageFile(nameFile,ios::binary);
	if(!inImageFile)
	{
		cout << endl <<"Unable to open." << endl;
		return false;
	}
	for (unsigned short int i = 0; i < *pHeight; i++)
	{
		for (unsigned short int j = 0; j < *pWidth; j++)
		{
			numberPoint = *pHeight*j+ *pWidth;
			inImageFile.read((char*) &number,sizeof number);
			pPoint[numberPoint] = number;
			*la = 100*i/ *pHeight;
			if (*la != *lb)
			{
				cout << "Load File: "<< *la << " %"<< " " << number << endl;
				*lb = *la;
			}
		}
	}
	inImageFile.close();
	cout << endl << "Complete Load File: "<< 100 << " %"<< endl;
	return true;
}

bool Image::SaveFile (string nameFile)
{
	cout << endl <<"Begin Save File " << "\"" << nameFile << "\"" << "." << endl;
	unsigned long int *la = new unsigned long int;
	unsigned long int *lb = new unsigned long int;
	unsigned long int numberPoint;
	unsigned short int number;
	*lb = 0;
	ofstream outImageFile(nameFile,ios::binary);
	if(!outImageFile)
	{
		cout << endl <<"Unable to open." << endl;
		return false;
	}
	for (unsigned short int i = 0; i < *pHeight; i++)
	{
		for (unsigned short int j = 0; j < *pWidth; j++)
		{
			numberPoint = *pHeight*j+ *pWidth;
			number = pPoint[numberPoint];
			outImageFile.write((char*) &number,sizeof number);
			*la = 100*i/ *pHeight;
			if (*la != *lb)
			{
				cout << "Save File: "<< *la << " %"<< endl;
				*lb = *la;
			}
		}
	}
	outImageFile.close();
	cout << endl << "Complete Save File: "<< 100 << " %"<< endl;
	return true;
}

void Image::ConstructPoint ()
{
	unsigned long int *la = new unsigned long int;
	unsigned long int *lb = new unsigned long int;
	*lb = 0;
	unsigned long int numberPoint;
	cout << "Create new image." << endl << endl;
	for (unsigned short int i = 0; i < *pHeight; i++)
	{
		for (unsigned short int j = 0; j < *pWidth; j++)
		{
			numberPoint = *pHeight*j+ *pWidth;
			pPoint[numberPoint] = i;
			*la = 100*i/ *pHeight;
			if (*la != *lb)
			{
				cout << "Create: "<< *la << " %"<< " " << pPoint[numberPoint] <<endl;
				*lb = *la;
			}
		}
	}
	delete la;
	delete lb;
	cout << "Complete: "<< 100 << " %"<< endl;
}
Nahlásit jako SPAM
IP: 89.24.203.–
vitamin+8
Grafoman
29. 3. 2013   #2
-
0
-

Normalne som sa zlakol ked som zbadal ten kod   

Preco skoro vsade pouzivas dynamicku alokaciu premennych? Dokonca aj pomocnych intov...

signed short int = short

typedef unsigned short int ushort; //nech je to prehladnejsie
Nahlásit jako SPAM
IP: 95.105.157.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
MiCizek0
Stálý člen
29. 3. 2013   #3
-
0
-

no mě celkem baví vypisovat unsigned short int. :-D :-P

Nahlásit jako SPAM
IP: 89.24.203.–
vitamin+8
Grafoman
29. 3. 2013   #4
-
0
-

#3 MiCizek
Neboj sa, casom ta to prejde :)

Nahlásit jako SPAM
IP: 95.105.157.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
MiCizek0
Stálý člen
29. 3. 2013   #5
-
0
-

Ok ale to nic nemění na tom, ze program spadne, kdy delokuju paměť. Jinak, co se týkám používání typedef a dynamické alokaci, vše se dá zatím snadno změnit. ;-)

Nahlásit jako SPAM
IP: 89.24.203.–
MiCizek0
Stálý člen
30. 3. 2013   #6
-
0
-

trochu jsem to zkrášlil a hned to funguje. :-D

#pragma once

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

typedef unsigned short int unshort;
typedef unsigned long int unlong;

class Image
{
public:
	Image ();
	void CreatePoints (unshort x, unshort y);
	~Image ();
	bool LoadFile (string nameFile);
	bool SaveFile (string nameFile);
	unshort GetValueFromThisPoint (unshort itsPoint) const {return pPoint[itsPoint] + itsPoint;}
	void ConstructPoint ();
	void SetValueOfThisPoint (unshort itsPoint, unshort itsValue) {pPoint[itsPoint] = itsValue;}
	void SetPosition(unshort x, unshort y);
private:
	unshort *pPoint;
	unshort *pHeight, *pWidth;
};

class PlanetaryGeneration
{
public:
	PlanetaryGeneration ();
	PlanetaryGeneration (unshort x, unshort y);
	~PlanetaryGeneration ();
	bool SaveFiles ();
	bool LoadFiles ();
	string GetName(short itsX, short itsY);
	void SetCoordinate (short itsX, short itsY);
	void SetName ();
private:
	Image *pImage;
	short *pX;
	short *pY;
	string *pName;
};

PlanetaryGeneration::PlanetaryGeneration ()
{
	pX = new short;
	pY = new short;
	pName = new string;
	*pX = 0;
	*pY = 0;
	*pName = "Earth";
	pImage = new Image[9];
	for (int i = 0; i < 9; i++)
	{
	}
}

PlanetaryGeneration::PlanetaryGeneration (unshort x, unshort y)
{
	pX = new short;
	pY = new short;
	pName = new string;
	*pX = 0;
	*pY = 0;
	*pName = "Earth";
	pImage = new Image[9];
	for (int i = 0; i < 9; ++i)
	{
		pImage[i].CreatePoints(x,y);
	}
}

PlanetaryGeneration::~PlanetaryGeneration ()
{
	delete [] pImage;
	delete pX;
	delete pY;
	delete pName;
}

bool PlanetaryGeneration::SaveFiles ()
{
	int k = 0;
	string itsName;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
			itsName = GetName(i,j);
			if (itsName != "error")
			{
				if (pImage[k].SaveFile(itsName))
				{
				}
				else
				{
					return false;
				}
			}
			++k;
		}
	}
	return true;
}

bool PlanetaryGeneration::LoadFiles ()
{
	int k = 0;
	string itsName;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
			itsName = GetName(i,j);
			if (itsName != "error")
			{
				if (pImage[k].LoadFile(itsName))
				{
				}
				else
				{
					return false;
				}
			}
			++k;
		}
	}
	return true;
}

string PlanetaryGeneration::GetName(short itsX, short itsY) 
{
	if (itsX > 1 || itsX < -1 || itsY > 1 || itsY < -1)
	{
		cout << " Input method GetName() must be in range -1 to 1!" << endl;
	}
	else
	{
		short resultX = *pX + itsX;
		short resultY = *pY + itsY;
		stringstream streamX;
		stringstream streamY;
		streamX << resultX;
		streamY << resultY;
		if (resultX < 0 || resultY < 0)
		{}
		else
		{
			string wholeName = *pName + streamX.str() + "_" + streamY.str() + ".raw";
			return wholeName;
		}
	}
	return "error";
}

void PlanetaryGeneration::SetCoordinate (short itsX, short itsY)
{
	*pX = itsX;
	*pY = itsY;
}
void PlanetaryGeneration::SetName ()
{
	cout << "Write name of file." << endl;
	cin >> *pName;
	//cout << endl << *pName << endl;
}

//class Planet;


Image::Image ()
{
	pHeight = new unshort;
	pWidth = new unshort;
	*pHeight = 0;
	*pWidth = 0;
	pPoint = 0;
}

void Image::CreatePoints (unshort x, unshort y)
{
	if (x >= 256)
	{
		if (x <= 4096)
		{
			*pHeight = x;
		}
		else
		{
			*pHeight = 4096;
		}
	}
	else
	{
		*pHeight = 256;
	}
	if (y >= 256)
	{
		if (y <= 4096)
		{
			*pWidth = y;
		}
		else
		{
			*pWidth = 4096;
		}
	}
	else
	{
		*pWidth = 256;
	}
	pPoint = new unshort[*pHeight * *pWidth];
	ConstructPoint ();
}
Image::~Image ()
{
	delete pHeight;
	delete pWidth;
	delete [] pPoint;
}

bool Image::LoadFile (string nameFile)
{
	cout << endl <<"Begin Load File " << "\"" << nameFile << "\"" << "." << endl;
	unlong la = 0;
	unlong lb = 0;;
	unlong numberPoint;
	unshort number;
	lb = 0;
	ifstream inImageFile(nameFile,ios::binary);
	if(!inImageFile)
	{
		cout << endl <<"Unable to open." << endl;
		return false;
	}
	for (unshort i = 0; i < *pHeight; i++)
	{
		for (unshort j = 0; j < *pWidth; j++)
		{
			numberPoint = *pHeight*j+ *pWidth;
			inImageFile.read((char*) &number,sizeof number);
			pPoint[numberPoint] = number;
			la = 100*i/ *pHeight;
			if (la != lb)
			{
				cout << "Load File: "<< la << " %"<< " " << number << endl;
				lb = la;
			}
		}
	}
	inImageFile.close();
	cout << endl << "Complete Load File: "<< 100 << " %"<< endl;
	return true;
}

bool Image::SaveFile (string nameFile)
{
	cout << endl <<"Begin Save File " << "\"" << nameFile << "\"" << "." << endl;
	unlong la = 0;
	unlong lb = 0;
	unlong numberPoint;
	unshort number;
	ofstream outImageFile(nameFile,ios::binary);
	if(!outImageFile)
	{
		cout << endl <<"Unable to open." << endl;
		return false;
	}
	for (unshort i = 0; i < *pHeight; i++)
	{
		for (unshort j = 0; j < *pWidth; j++)
		{
			numberPoint = *pHeight*j+ *pWidth;
			number = pPoint[numberPoint];
			outImageFile.write((char*) &number,sizeof number);
			la = 100*i/ *pHeight;
			if (la != lb)
			{
				cout << "Save File: "<< la << " %"<< endl;
				lb = la;
			}
		}
	}
	outImageFile.close();
	cout << endl << "Complete Save File: "<< 100 << " %"<< endl;
	return true;
}

void Image::ConstructPoint ()
{
	unlong la = 0;
	unlong lb = 0;
	unlong numberPoint;
	cout << "Create new image." << endl << endl;
	for (unshort i = 0; i < *pHeight; i++)
	{
		for (unshort j = 0; j < *pWidth; j++)
		{
			numberPoint = *pHeight*j+ *pWidth;
			pPoint[numberPoint] = i;
			la = 100*i/ *pHeight;
			if (la != lb)
			{
				cout << "Create: "<< la << " %"<< " " << pPoint[numberPoint] <<endl;
				lb = la;
			}
		}
	}
	cout << "Complete: "<< 100 << " %"<< endl;
}
Nahlásit jako SPAM
IP: 89.24.203.–
MiCizek0
Stálý člen
30. 3. 2013   #7
-
0
-

Ahoj, může to být  takhle? Nebo je nějaký jednoduší způsob?

/* Je to jenom priklad, napadlo me to a myslim, ze se mi to muze hodit*/

	unshort value;
	unshort firstInt;
	unshort secondaryInt;
	cin >> value;
	firstInt = value;
	secondaryInt = value;
	firstInt <<= 8;
	firstInt >>= 8;
	secondaryInt >>= 8;
	cout << (char)firstInt << " " << (char)secondaryInt << endl;
	value = firstInt + 256 * secondaryInt;
	char firstChar;
	char secondaryChar;
	cin >> firstChar;
	cout << endl;
	cin	>> secondaryChar;
	cout << endl;
	value = (unshort)secondaryChar;
	value <<= 8;
	value = value + (unshort)firstChar;
	firstInt = value;
	secondaryInt = value;
	firstInt <<= 8;
	firstInt >>= 8;
	secondaryInt >>= 8;
	cout << (char)firstInt << " " << (char)secondaryInt << endl;
	value = firstInt + 256 * secondaryInt;
	cout << value << endl;
Nahlásit jako SPAM
IP: 89.24.203.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 28 hostů

Podobná vlákna

Spadne, offline mini chyba — založil _Daffy_

Metoda volá metodu — založil delicacyy

Moderátoři diskuze

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý