-1

I was doing my Huffman homework and I got stumble on a tiny thing that I cant understand why it happens. So I created a structure that has an int array a char and an int that holds the size of the array.

struct kodlar{
    char karakter;
    int* code;
    int codesize;
};
typedef struct kodlar kodlar;

kodlar* yenikod(char karakter, int* code,int codesize){
    kodlar* yenikod = (kodlar*)malloc(sizeof(kodlar));  
    if(yenikod){
        yenikod->karakter = karakter;
        yenikod->code = code;
        yenikod->codesize = codesize;
    }
    return yenikod;
}

Then inside of my main, I created an array that holds these structures:

kodlar* K[taille];

taille is the number of char that it is going to store. In order to put the characters and codes correspondence, I created the function

printCodes(HuffTree,arr,top,&p,K);

and it works like this:

void printCodes(node* root, int arr[], int top,int* i,kodlar** K)
{  
        if (root->left) { 
            arr[top] = 0; 
            printCodes(root->left, arr, top + 1,i,K);
            //printf("%c\n",'l'); 
        }
        if (root->right) {
            arr[top] = 1; 
            printCodes(root->right, arr, top + 1,i,K); 
            //printf("%c\n",'r');
        }
        if (isLeaf(root)) { 
            printArr(arr,top);          
            K[*i]=yenikod((root->lettre),arr,top);
            *i = *i + 1;
            //printArr(K[*i]->code,K[*i]->codesize);
            //printf("%i en son if te i \n",*i );
        }
}

But it seems like I cant store arrays inside of my array of kodlar structure. if I commented out the parties //printArr(K[*i]->code,K[*i]->codesize); it gives me a segmentation fault and if I try to print like this:

for (int i = 0; i < taille; ++i){
        printf("%c :", K[i]->karakter);
        printf(" ");
        printArr(K[i]->code,K[i]->codesize);
        printf("\n");
}

it gives me codes but only with 1's. I got stuck on this it has been 2 days I would appreciate it if somebody can help me.

T.K
  • 41
  • 7
  • arrays are not pointers and pointers are not arrays. Why are you not using `std::string` ? – 463035818_is_not_an_ai Dec 09 '20 at 17:00
  • 1
    Everybody will tell you that you need to provide a [mcve]. – Costantino Grana Dec 09 '20 at 17:01
  • 1
    You are only performing a "shallow ptr copy" here: `yenikod->code = code;` The actual array data is not being copied at all. You need to perform a "deep array copy" by allocating space for the new array and then copying all the data inside of it. – wcochran Dec 09 '20 at 17:04
  • 1
    Better yet, use `std::vector` then you get deep copy by default. – wcochran Dec 09 '20 at 17:05
  • 1
    BTW, since you tagged as C++, you don't need to use `typedef struct` or use the `struct` keyword when declaring variables. – Thomas Matthews Dec 09 '20 at 17:12
  • 1
    Since you tagged as C++, prefer to use `new` instead of `malloc`. The `malloc` function does not call object constructors. – Thomas Matthews Dec 09 '20 at 17:13
  • Thanks to the wrong tag, half of the comments are wrong... – underscore_d Dec 09 '20 at 17:30
  • @wcochran is it going to be something like this ` kodlar* yenikod(char karakter, int* code,int codesize){ kodlar* yenikod = (kodlar*)malloc(sizeof(kodlar)); if(yenikod){ yenikod->karakter = karakter; yenikod->codesize = codesize; code = (int*)malloc(sizeof(int)*codesize); for (int i = 0; i < codesize; ++i) { yenikod->code[i] = code[i]; } } return yenikod; } ` – T.K Dec 09 '20 at 18:06
  • @T.K Yes that is a deep copy (although I'd probably use `memcpy` instead of a loop). I am confused on your semantics though ... `printCodes` should not be mutating any of your structures if the goal is just to regurgitate the Huffman codes -- or is it trying to actually generate codes somehow? Also, I think you are going to have a tough time avoiding memory leaks when your are done with tree since you need to chase down all those allocations to release memory. – wcochran Dec 09 '20 at 18:16
  • @wcochran I want to make an array of kodlar which contains 1-character 2-code of that character 3- size of that character code and to do it so I created `printCodes` so it will be some kind of library which contains the codes and the character of that code. It generates the code when it comes to the leaf and stores it in the array – T.K Dec 09 '20 at 18:22
  • @wcochran I did what u said and used ` code = (int*)malloc(sizeof(int)*codesize); memcpy(code,yenikod->code,codesize); ` but this time it gives me random numbers like 1772182991-2147476736001772576205-21474764800 – T.K Dec 09 '20 at 18:48

1 Answers1

0
struct kodlar{
char karakter;
int codesize;
int code[50];
};
typedef struct kodlar kodlar;

kodlar* yenikod(char karakter, int* code,int codesize){
    kodlar* yenikod = (kodlar*)malloc(sizeof(kodlar));
    if(yenikod){
        yenikod->karakter = karakter;
        yenikod->codesize = codesize;
        for (int i = 0; i < codesize; ++i)
        {
            yenikod->code[i] = code[i];
        }
    }
    return yenikod;
}

So thanks to @wcochran I understood that the problem was in my struct but memcpy did not work on my code and I was already giving an array that has been already allocated before entering to yenikod. And I gave a size to my code array in the kodlar struct and my problem was solved.

T.K
  • 41
  • 7