请帮我解决内存损坏问题 [英] Please help me in memory corruption

查看:53
本文介绍了请帮我解决内存损坏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,其中使用new分配内存但未删除。功能如下:


void AddItem(char * buffer,char * items [],int& index)

{

char * temp = new char [strlen(buffer)+ 1];


strcpy(temp,buffer);


items [ index] = temp;


index ++;

}


此函数AddItem()被调用52次程序内部。

然后我认为这会导致一些内存问题或者它会减慢程序的速度。请让我知道我可采取哪些纠正措施。

谢谢。

解决方案


我有一个功能,其中使用new分配内存,而不是删除。功能如下:


void AddItem(char * buffer,char * items [],int& index)

{

char * temp = new char [strlen(buffer)+ 1];


strcpy(temp,buffer);


items [ index] = temp;


index ++;

}


此函数AddItem()被调用52次程序内部。

然后我认为这会导致一些内存问题或者它会减慢程序的速度。请让我知道我可采取哪些纠正措施。

谢谢。



在你的函数中使用它

展开 | 选择 | Wrap | 行号



在函数中使用此函数

展开 | 选择 | Wra p | 行号


< blockquote>


但是这里它将temp复制到item [index]。

如果我将删除函数内部的temp,它会将内存松散到item [index] ]。

请告诉我。

展开 | 选择 < span class =codeDivider> | Wrap | 行号


I have a function in which memory has been allocated using new and not being deleted. The function is as below:

void AddItem(char* buffer, char* items[], int& index)
{
char* temp = new char[strlen(buffer) + 1];

strcpy(temp, buffer);

items[index] = temp;

index++;
}

This function AddItem() has been called 52 times inside the program.
Then I think this is causing some memory issue or its slowing down the program. Please let me know what corrective action can I take.
Thanks.

解决方案

I have a function in which memory has been allocated using new and not being deleted. The function is as below:

void AddItem(char* buffer, char* items[], int& index)
{
char* temp = new char[strlen(buffer) + 1];

strcpy(temp, buffer);

items[index] = temp;

index++;
}

This function AddItem() has been called 52 times inside the program.
Then I think this is causing some memory issue or its slowing down the program. Please let me know what corrective action can I take.
Thanks.

Use this in your function

Expand|Select|Wrap|Line Numbers


Use this in your function

Expand|Select|Wrap|Line Numbers


But here its copying temp to item[index].
if I will delete temp inside the function it will loose the memory to item[index].
Please let me know.

Expand|Select|Wrap|Line Numbers


这篇关于请帮我解决内存损坏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆