Мне нужно выделить массив строк. 100 в длину, и каждая ячейка может содержать размер строки в 100 символов., когда я освобождаю массив строк, с которыми связана моя программа
typedef char* (*Encryptor3)(char*, char);
char** encryptToString(char** input,int length,int first,Encryptor3 encryptor)
{
int i=0;
char** output=(char **)malloc(sizeof(char*)*length);
for(i=0;i<length;i++){
output[i]=(char *)malloc(sizeof(char)*(100+1));
}
output[0]=encryptor(first,input[0]);
output[1]=encryptor(first,input[1]);
for(i=2; i<length ; i++)
{
output[i]=encryptor(output[i-2],input[i]);
}
return output;
}
int main()
{
char plain[] = {'p','l','a','i','n','t','e','x','t'};
char** outputS = encryptToString(plain, 9, "test", idenString);
int i;
for(i=0; i<9; i++)
printf("%s\n", outputS[i]);
for(i=0; i<9; i++) //deallocating my array of strings
free(outputS[i]);
free(outputS);
return 0;
}
строка "free (outputS [i]);" приведет к сбою программы, и я получу обычную ошибку: «myp.exe перестает работать».
Почему вы передаете '' test '' как параметр 'int'? – icktoofay
'output [i] = шифр (выход [i-2], вход [i]);' утечка памяти. Кроме того, как распределяется возвращаемое значение 'encryptor'? –
Эти строки 'output [0] = encryptor (во-первых, вход [0]);' (и т.д.) заменяют * вашу переменную, поэтому она не использует пространство 'malloc' – Dave