0

I'm because i have a problem on a c++ programm when i want to convert str to char*, when i debug it, i can see that the "this->nom" = France vs Espagne and it's what i want... i will show you the debug code at the end. The "nom" attribute is in the class Spectacle.h if you want it let me know.

here is my code( main first ) :

Match m("France", "Espagne", "Football", "Championnat du monde");
m.getTarif() = 110;
m.ajoutDate("Paris", "24/04/2017");
m.ajoutDate("Nantes", "26/08/2017");

std::cout << "Nom :" << m.getNom() << std::endl;

then the match.h :

#pragma once
#ifndef __Match_h__

#define __Match_h__
#include "Spectacle.h"
#include <string>

class Match : public Spectacle

{
public:
Match(char* equipe1, char* equipe2, char* sport, char* compet);
void ajoutDate(char * lieu, char * date);
char* getType();
char* getEquipe1();
char* getEquipe2();
char* getSport();
char* getTypeCompetition();
~Match();

private:
char* competition;
char* equipe1;
char* equipe2;
char* sport;
};

#endif //__Match_h__

then the match.cpp :

#include "Match.h"

Match::Match(char * equipe1, char * equipe2, char * sport, char * compet)
{
this->date = new char*[100];
this->lieu = new char*[100];
this->index = 0;
this->type = "Sport";

this->equipe1 = equipe1;
this->equipe2 = equipe2;
this->sport = sport;
this->competition = compet;
char* vs = " VS ";
std::string result;
result += equipe1;
result += vs;
result += equipe2;
char* res = (char*)result.c_str();
this->nom = res;
}

void Match::ajoutDate(char * lieu, char * date)
{
if (this->index < 1)
{
    this->date[this->index] = date;
    this->lieu[this->index] = lieu;
    this->index++;
}
else
{
    std::cout << "Il ne peut pas y avoir 2 dates pour ce concert.";
}
}

char * Match::getType()
{
return this->type;
}

char * Match::getEquipe1()
{
return this->equipe1;
}

char * Match::getEquipe2()
{
return this->equipe2;
}

char * Match::getSport()
{
return  this->sport;
}

char * Match::getTypeCompetition()
{
return this->competition;
}

Match::~Match()
{
}

Here is the spectacle.cpp :

#include "Spectacle.h"


Spectacle::Spectacle(char* nom)
{

this->date = new char*[100];
this->lieu = new char*[100];
this->nom = nom;
this->index = 0;
this->type = "indefinie";
}

Spectacle::Spectacle()
{
}


Spectacle::~Spectacle()
{
}

void Spectacle::ajoutDate(char * lieu, char * date)
{
if (this->index < 100)
{
    this->date[this->index] = date;
    this->lieu[this->index] = lieu;
    this->index++;
}
else
{
    std::cout << "Il y a déjà 100 dates pour ce concert.";
}
}

char * Spectacle::getLieu(char * date)
{
for (int i = 0; i < this->index; i++) {
    if(this->date[i] == date){
        return this->lieu[i];
    }
}
return "Rien trouver";
}

int & Spectacle::getNbDate()
{
return this->index;
}

float& Spectacle::getTarif()
{
return this->tarif;
}

void Spectacle::synthese()
{
std::cout << "Nom :" << this->nom << ", Type: " << this->type
    << ", Nb dates: " << this->getNbDate() << std::endl;
for (int i = 0; i < this->index; i++) {
    std::cout << this->date[i] << " " << this->lieu[i] << std::endl;
}
}


char* Spectacle::getNom()
{
return this->nom;
}

char * Spectacle::getType()
{
return *this->lieu;
}

Here is the debug : https://i.stack.imgur.com/mXKby.png

And here is what i have at the end : https://i.stack.imgur.com/1E31i.png

Thanks for the help guys, have a good day btw !

  • Really similar to http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope. This could be better narrowed down by you by putting a watch on the variable with the garbage value and seeing when it becomes garbage. – chris Mar 25 '17 at 17:05
  • @chris it becomes garbage when i just go out of the match constructor and sorry but i'm a begginer atm, didn't find the solution on the web and the output is really strange for me .. – Sebastien Napo Mar 25 '17 at 17:21
  • Right, so a more specific question is why it becomes garbage when the constructor is done. This lets you build a much more minimal example with the same behaviour. No classes necessary, no other variables necessary. In the end, your example code can look almost identical to the one in the question I linked, and that's a much easier example to reason about. – chris Mar 25 '17 at 17:52

2 Answers2

0

Can't find your data members.

this->date = new char*[100];
this->lieu = new char*[100];
this->index = 0;
this->type = "Sport";

You calling this but they are not part of Match class. fix your code errors first.

Adir Ratzon
  • 181
  • 3
  • 8
0

Sorry, the Spectacle files were not necessary because the problem is in Match constructor and precisely :

    std::string result;
    result += equipe1;
    result += vs;
    result += equipe2;
    char* res = (char*)result.c_str();
    this->nom = res;

result is a local variable, and your data member nom points to the object pointed by result.c_str(), so nom and result.c_str() shares the same object, but when the constructor method goes out of scope, the result variable is gone, and so the object result.c_str() is garbage and your data member nom points to garbage. And the reason why you could view the right value of nom while debugging is because you debugged inside the constructor scope so the result variable was still on the stack.

Solution : There's many solutions to this problem, for example you can use result as a class member so you can be sure it won't be gone until your Match object is gone.