试图释放内存会导致错误
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I have declared the following struct:

typedef struct _RECOGNITIONRESULT {
        int begin_time_ms, end_time_ms;
        char* word;
} RECOGNITIONRESULT;

There is a method that creates an array of RECOGNITIONRESULT and fills it (for test purposes only):

void test_realloc(RECOGNITIONRESULT** p, int count){
    int i;
    *p = (RECOGNITIONRESULT *)realloc(*p, count * sizeof(RECOGNITIONRESULT));
    for (i = 0; i < count; i++){
        (*p)[i].begin_time_ms = 2*i;
        (*p)[i].end_time_ms = 2*i+1;
        (*p)[i].word=(char *) malloc ( (strlen("hello"+1) * sizeof(char ) ));
        strcpy((*p)[i].word,"hello");
    }
}

The method to free memory is:

void free_realloc(RECOGNITIONRESULT* p, int count){
    int i = 0;
    if(p != NULL){
        if (count > 0){
            for (i = 0; i < count; i++){
                free(p[i].word);      //THE PROBLEM IS HERE.
            }
        }
        free(p);
    }
}

The main method calls those methods like this:

int main(int argc, char** argv)
{
    int count = 10;
    RECOGNITIONRESULT *p = NULL;
    test_realloc(&p,count);
    free_realloc(p,count);
    return 0;
}

Then if I try to free the memory allocated for "word", I get the following error:

HEAP CORRUPTION DETECTED: after normal block (#63) at 0x003D31D8. CRT detected that the application wrote to memory after end of heap buffer.

Using the debugger I've discovered that the crash occurs when calling free(p[i].word);

What am I doing wrong? How can I free he memory for the strings?

#0

The problem is in your allocation of memory for word. strlen("hello"+1) should be strlen("hello")+1.

#1

Since you appear to allocate a whole array of structures in one strike

RECOGNITIONRESULT **p;
*p = (RECOGNITIONRESULT *)realloc(*p, count * sizeof(RECOGNITIONRESULT));

you can free them in one call to free() as well :

void free_realloc(RECOGNITIONRESULT *p, int count){
        free(p);
}

And the strlen("hello"+1) is also wrong, as detected by Chowlett.

推荐文章

验证(CSS 3.0):“文本溢出”不是已知的CSS属性名

验证(CSS 3.0):“文本溢出”不是已知的CSS属性名

推荐文章

共Y期第X页

共Y期第X页

推荐文章

在python中比较两个由字典和唯一键组成的列表

在python中比较两个由字典和唯一键组成的列表

推荐文章

沿bezierCurveTo或quadraticCurveTo设置动画

沿bezierCurveTo或quadraticCurveTo设置动画

推荐文章

xcode 4.3的断点无效

xcode 4.3的断点无效

推荐文章

如何取消USSD需求响应

如何取消USSD需求响应

推荐文章

试一试看风景不好吗?

试一试看风景不好吗?

推荐文章

多维数组输出。请帮我把这个烂摊子弄出来

多维数组输出。请帮我把这个烂摊子弄出来

推荐文章

VB.NET版基于排序对Excel列进行排序

VB.NET版基于排序对Excel列进行排序

推荐文章

XMLHttpRequest responseType=“json”给出错误语法\u ERR:DOM Exception 12

XMLHttpRequest responseType=“json”给出错误语法\u ERR:DOM Exception 12

推荐文章

Galaxy Nexus:传感器读数非常慢

Galaxy Nexus:传感器读数非常慢

推荐文章

Java套接字BufferReader.readline获取空值

Java套接字BufferReader.readline获取空值

推荐文章

用户界面不反映我所做的更改

用户界面不反映我所做的更改

推荐文章

检查函数结果是否为空值

检查函数结果是否为空值

推荐文章

无序列表分隔页脚

无序列表分隔页脚

推荐文章

垂直居中jQuery手风琴

垂直居中jQuery手风琴