Cfree5可以JAVA_无法使用free()释放内存

我无法释放我使用malloc分配的内存 . 程序运行正常,直到它应该使用free释放内存的部分 . 程序冻结了 . 所以我想知道问题是什么,因为我只是在学习C.语法上代码似乎是正确的,所以我需要删除该位置的所有东西,然后从该位置或其他地方释放内存?

这是代码 .

// Program to accept and print out five strings

#include

#include

#include

#define NOOFSTRINGS 5

#define BUFFSIZE 255

int main()

{

char buffer[BUFFSIZE];//buffer to temporarily store strings input by user

char *arrayOfStrngs[NOOFSTRINGS];

int i;

for(i=0; i

{

printf("Enter string %d:\n",(i+1));

arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer)+1));//calculates string length and allocates appropriate memory

if( arrayOfStrngs[i] != NULL)//checking if memory allocation was successful

{

strcpy(arrayOfStrngs[i], buffer);//copies input string srom buffer to a storage loacation

}

else//prints error message and exits

{

printf("Debug: Dynamic memory allocation failed");

exit (EXIT_FAILURE);

}

}

printf("\nHere are the strings you typed in:\n");

//outputting all the strings input by the user

for(i=0; i

{

puts(arrayOfStrngs[i]);

printf("\n");

}

//Freeing up allocated memory

for(i=0; i

{

free(arrayOfStrngs[i]);

if(arrayOfStrngs[i] != NULL)

{

printf("Debug: Memory deallocation failed");

exit(EXIT_FAILURE);

}

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值