'AVR / C (C++) I created a class with a pointer on a char array and I have a segmentation fault sometimes

This is my class Cipher.hpp:

#include <string.h>

class Cipher
{

private:
    mutable char* m_matrice;

public:
    /**
    * @brief Uniqement pour debug. A supprimer en prod
    * 
    */
    mutable int m_xor = 0;

    /**
    * @brief Construct a new Cipher object
    * @param matrice Chaine de char entre 33'!' et 126'~'
    */
    Cipher(char *matrice);

    /**
     * @brief 
     * 
     * @param matrice Chaine de char entre 33'!' et 126'~'
     */
    void setMatrix(char *matrice);

    /**
     * @brief 
     * 
     */
    char *getMatrix();

    /**
    * @brief 
    * @param message tableau de char entre 32' ' et 126'~' 
    */
    void Encode(char *message) const;

    /**
    * @brief 
    * @param message tableau de char entre 32' ' et 126'~' 
    */
    void Decode(char *message) const;
};

This is the code of my class : Cipher.cpp

#include "Cipher.hpp"
#include <stdio.h>
#include <cstdlib>

using namespace std;

/**
 * @brief Construct a new Cipher:: Cipher object
 * @param matrice Clé de chiffrage
 */
Cipher::Cipher(char *matrice)
{
    strcpy(m_matrice, matrice);   //Sometimes Triggers "Segmentation fault"
    m_xor = 0;
}

char *Cipher::getMatrix()
{
    return m_matrice;
}

/**
 * @brief définit la Clé de chiffrage
 * 
 * @param matrice Clé de chiffrage
 */
void Cipher::setMatrix(char *matrice)
{
    strcpy(m_matrice, matrice);
    m_xor = 0;
}

This is a part of the main.cpp

int main()
{
    char matrix[] = "0123456789";

// Constructor
    Cipher m_Cipher(matrix);

    int  Total = 0;
    cout << " Total égal : ";

// This cout // sometimes triggers a "segmentation fault"
    cout << Total;

// ...
}

This program works well, but throw segmentation faults depending on the length of char matrix[]:

char matrix[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX"; // Works well

char matrix[] = "123456789"; // triggers a "segmentation fault"

cout << Total;  // sometimes triggers a "segmentation fault"

I'm pretty sure the problem is with the pointer to the char[] but I need a dynamic char array and I don't know of any other ways to do this.

Could you help me to fix this please? Note : This program is for an atmel AVR, then I must use the avr string library (https://www.nongnu.org/avr-libc/user-manual/group__avr__string.html), not the C++ standard (https://docs.microsoft.com/en-us/cpp/standard-library/string?view=msvc-170)



Solution 1:[1]

your problem is this

 mutable char* m_matrice;

with this

Cipher::Cipher(char *matrice)
{
    strcpy(m_matrice, matrice);   //Sometimes Triggers "Segmentation fault"
    m_xor = 0;
}

that pointer m_matrice points nowhere, or to some random location.

do

   Cipher::Cipher(char *matrice)
   {
     m_matrice = new char(strlen(matrice + 1));
    strcpy(m_matrice, matrice);   //Sometimes Triggers "Segmentation fault"
    m_xor = 0;
}

you need to delete [] m_matrice in your destructor

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pm100