0

This is the code:

I do know what is the problem, I tried for hours to fix it, but was not successful

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void input(char ***array1,int * sizeWords,int size)
{
    for (int i = 0; i < size; i++)
    {
        char word[81] = "";
        char descrip[201] = "";
        int numAgdarot = 0;
        //how mach agdarot
        printf("how mach of agdarrot you want? ");
        scanf("%d", &numAgdarot);
        //save the count of agdarot
        sizeWords[i] = numAgdarot;

        do
        {
            printf("enter word number %d: ", i);
            _flushall();
            gets(word);
        } while (textSpace(word) == False);

        (array1)[i] = (char**)malloc(numAgdarot + 1 * sizeof(char*)); //set the num and agdarot

        //save the word
        (array1)[i][0] = (char*)malloc(strlen(word) * sizeof(char));
        strcpy(array1[i][0], word);
        //save the agdarot
        for (int j = 1; j <= numAgdarot; j++)
        {
            printf("enter descrip number %d: ", i);
            _flushall();
            gets(descrip);
            (array1)[i][j] = (char*)malloc(strlen(descrip) * sizeof(char));
            strcpy(array1[i][j], descrip);
        }
    }
}
int main() {
    int *sizeWords = NULL;
    int size = 0;
    char *x=NULL;// = "jk";

    char *** array1  = NULL;

    printf("enter number of word: ");
    scanf("%d", &size);
    array1 = (char***)malloc(size * sizeof(char**));
    sizeWords = (int*)malloc(size * sizeof(int));
    //x = temp(x,sizeWords);

    //input the word and agdarot
    input(array1, sizeWords, size);

    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < sizeWords[i] + 1; j++)
        {
            free(array1[i][j]);
        }
        free(array1);
    }

    return 0;
}

I get a "HEAP CORRUPTION DELETED" error after Normal block. Why? If i used a debugger I see the char * but i can not do a free..

Paul Roub
  • 36,322
  • 27
  • 84
  • 93

2 Answers2

3

Doing

malloc(strlen(word) * sizeof(char));

is almost always wrong. Remember that strings also contains an extra character that is not reported by the strlen function, the terminator character '\0'. That means your next call to strcpy will write beyond the end of the allocated memory.

What you should do is allocate memory for that extra terminator character as well:

array1[i][0] = malloc(strlen(word) + 1);

[Note that I changed the code, first because the parentheses around array are not needed, secondly because you in C one should not cast the return of malloc, and third because sizeof(char) is specified to always be 1.]

Remember to change on all other places where you use strlen in a call to malloc.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

These allocations are too small:

(array1)[i][0] = (char*)malloc(strlen(word) * sizeof(char));
strcpy(array1[i][0], word);

// ...    
(array1)[i][j] = (char*)malloc(strlen(descrip) * sizeof(char));
strcpy(array1[i][j], descrip);

You need an extra character for the terminating \0. strcpy() is writing into unallocated space.

Save yourself some trouble and:

(array1)[i][0] = strdup(word);

// ...    
(array1)[i][j] = strdup(descrip);

And, as pointed out in the comments,

for (int i = 0; i < size; i++)
{
    for (int j = 0; j < sizeWords[i] + 1; j++)
    {
        free(array1[i][j]);
    }
    free(array1);
}

should become:

for (int i = 0; i < size; i++)
{
    for (int j = 0; j < sizeWords[i] + 1; j++)
    {
        free(array1[i][j]);
    }

    free(array1[i]);
}

free(array1);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93