Assigner char * à un attribut fait planter le programme
Bonjour,
Dans le cadre d'une classe résultant d'un Hash (SHA256, MD5, etc) possédant trois informations (Nom du hash, Hash, Longueur du hash), j'ai un problème quand j'assigne un pointeur à un de mes attribut.
Je n'ai jamais vraiment eu de problème avec ma classe, mais j'ai réparé le système qui hachait et depuis que lui fonctionne, la classe ne fonctionne plus.
Le plus étrange est que cela vient juste de l'affectation de "new char[taille]" à un attribut m_hashName, l'allocation du new char[taille] ou simplement le calcul de la taille ne cause pas le problème.
Par exemple je peux allouer une variable exactement comme pour l'allocation originale, et le programme crash si je l'affecte à l'attribut.
Exemples :
Code:
1 2
|
m_attribut = new char[taille]; // Plante |
Code:
1 2 3
|
char *test = new char[taille]; // Ok, pas de souci
m_attribut = test; // Plante |
Voici le code complet de la classe pour ceux qui veulent :
HashString.h:
Code:
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
|
#ifndef UU_HASHSTRING_H_INCLUDED
#define UU_HASHSTRING_H_INCLUDED
#include <cstring>
#include <iostream>
namespace Ungine
{
namespace Utils
{
class HashString
{
public:
HashString();
HashString(const char *hashName, const char *digest);
HashString(const HashString& hs);
~HashString();
bool IsValid() const;
const char *GetDigest() const;
const char *GetHashName() const;
unsigned int GetLength() const;
HashString& operator=(const HashString &hash);
bool operator==(const HashString &hash) const;
bool operator!=(const HashString &hash) const;
private:
char *m_digest;
char *m_hashName;
unsigned int m_length;
};
std::ostream& operator<<(std::ostream &out, HashString &string);
}
}
#endif // UU_HASHSTRING_H_INCLUDED |
HashString.cpp:
Code:
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
|
#include "HashString.h"
namespace Ungine
{
namespace Utils
{
HashString::HashString() :
m_digest(0),
m_hashName(0),
m_length(0)
{
}
HashString::HashString(const char *hashName, const char *digest)
{
m_hashName = new char[strlen(hashName)+1];
strcpy(m_hashName, hashName);
m_digest = new char[strlen(digest)+1];
strcpy(m_digest, digest);
m_length = strlen(digest);
}
HashString::HashString(const HashString& hs)
{
m_hashName = new char[strlen(hs.m_hashName)+1];
strcpy(m_hashName, hs.m_hashName);
m_digest = new char[strlen(hs.m_digest)+1];
strcpy(m_digest, hs.m_digest);
m_length = strlen(m_digest);
}
bool HashString::IsValid() const
{
return m_length > 0;
}
HashString::~HashString()
{
delete[] m_digest;
delete[] m_hashName;
}
const char *HashString::GetDigest() const
{
return m_digest;
}
const char *HashString::GetHashName() const
{
return m_hashName;
}
unsigned int HashString::GetLength() const
{
return m_length;
}
HashString& HashString::operator=(const HashString &hash)
{
delete[] m_hashName;
delete[] m_digest;
m_hashName = new char[strlen(hash.m_hashName)+1];
strcpy(m_hashName, hash.m_hashName);
m_digest = new char[strlen(hash.m_digest)+1];
strcpy(m_digest, hash.m_digest);
m_length = strlen(hash.m_digest);
return *this;
}
bool HashString::operator==(const HashString &hash) const
{
return strcmp(m_digest, hash.m_digest) && strcmp(m_hashName, hash.m_hashName);
}
bool HashString::operator!=(const HashString &hash) const
{
return !strcmp(m_digest, hash.m_digest) || !strcmp(m_hashName, hash.m_hashName);
}
}
}
std::ostream& operator<<(std::ostream& out, Ungine::Utils::HashString& string)
{
out << string.GetDigest();
return out;
} |
Qu'est-ce que j'ai fais de mal?
Je précise que l'erreur intervient dans le constructeur "HashString::HashString(const char *hashName, const char *digest)"
Merci d'avance