32

运行 ac 程序时出现以下错误:

*** glibc detected *** ./a.out: double free or corruption (!prev): 0x080b8008 ***

我相信这是由于在程序结束时调用了 free() ,但我无法弄清楚在此之前 malloc 的内存在哪里被释放。这是代码:

#include <stdio.h>
#include <stdlib.h> //malloc
#include <math.h>  //sine

#define TIME 255
#define HARM 32

int main (void) {
    double sineRads;
    double sine;
    int tcount = 0;
    int hcount = 0;
    /* allocate some heap memory for the large array of waveform data */
    double *ptr = malloc(sizeof(double *) * TIME);
    if (NULL == ptr) {
        printf("ERROR: couldn't allocate waveform memory!\n");
    } else {
        /*evaluate and add harmonic amplitudes for each time step */
        for(tcount = 0; tcount <= TIME; tcount++){
            for(hcount = 0; hcount <= HARM; hcount++){
                sineRads = ((double)tcount / (double)TIME) * (2*M_PI); //angular frequency
                sineRads *= (hcount + 1); //scale frequency by harmonic number
                sine = sin(sineRads); 
                *(ptr+tcount) += sine; //add to other results for this time step
            }
        }
        free(ptr);
        ptr = NULL;     
    }
    return 0;
}

这是编译的:

gcc -Wall -g -lm test.c

瓦尔格林:

valgrind --leak-check=yes ./a.out

给出:

    ==3028== Memcheck, a memory error detector
==3028== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3028== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==3028== Command: ./a.out
==3028== 
==3028== Invalid read of size 8
==3028==    at 0x8048580: main (test.c:25)
==3028==  Address 0x41ca420 is 1,016 bytes inside a block of size 1,020 alloc'd
==3028==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
==3028==    by 0x80484F8: main (test.c:15)
==3028== 
==3028== Invalid write of size 8
==3028==    at 0x8048586: main (test.c:25)
==3028==  Address 0x41ca420 is 1,016 bytes inside a block of size 1,020 alloc'd
==3028==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
==3028==    by 0x80484F8: main (test.c:15)
==3028== 
==3028== 
==3028== HEAP SUMMARY:
==3028==     in use at exit: 0 bytes in 0 blocks
==3028==   total heap usage: 1 allocs, 1 frees, 1,020 bytes allocated
==3028== 
==3028== All heap blocks were freed -- no leaks are possible
==3028== 
==3028== For counts of detected and suppressed errors, rerun with: -v
==3028== ERROR SUMMARY: 8514 errors from 2 contexts (suppressed: 14 from 7)

我对不能自动管理自己的内存的语言没有太多经验(因此在 c 中做这个练习来学习一点)但是我被卡住了。任何帮助,将不胜感激。

该代码应该是加法音频合成器的一部分。在这方面它确实有效并给出了存储在 ptr 中的正确输出。

谢谢。

4

4 回答 4

26
double *ptr = malloc(sizeof(double *) * TIME);
/* ... */
for(tcount = 0; tcount <= TIME; tcount++)
                         ^^
  • 你越界了。更改或 <=分配元素<SIZE + 1
  • malloc错了,你会想要sizeof(double)而不是 sizeof(double *)
  • 作为ouah评论,虽然与您的腐败问题没有直接联系,但您在使用*(ptr+tcount)时没有初始化它

  • 就像样式注释一样,您可能想要使用ptr[tcount]而不是*(ptr + tcount)
  • 你真的不需要malloc+free因为你已经知道了SIZE
于 2012-09-01T19:39:08.117 回答
7

更改此行

double *ptr = malloc(sizeof(double *) * TIME);

double *ptr = malloc(sizeof(double) * TIME);
于 2012-09-01T19:39:39.893 回答
4

1 - 你的 malloc() 是错误的。
2 - 您超出了分配内存的范围
3 - 您应该初始化分配的内存

这是包含所有所需更改的程序。我编译并运行...没有错误或警告。

#include <stdio.h>
#include <stdlib.h> //malloc
#include <math.h>  //sine
#include <string.h>

#define TIME 255
#define HARM 32

int main (void) {
    double sineRads;
    double sine;
    int tcount = 0;
    int hcount = 0;
    /* allocate some heap memory for the large array of waveform data */
    double *ptr = malloc(sizeof(double) * TIME);
     //memset( ptr, 0x00, sizeof(double) * TIME);  may not always set double to 0
    for( tcount = 0; tcount < TIME; tcount++ )
    {
         ptr[tcount] = 0; 
    }

    tcount = 0;
    if (NULL == ptr) {
        printf("ERROR: couldn't allocate waveform memory!\n");
    } else {
        /*evaluate and add harmonic amplitudes for each time step */
        for(tcount = 0; tcount < TIME; tcount++){
            for(hcount = 0; hcount <= HARM; hcount++){
                sineRads = ((double)tcount / (double)TIME) * (2*M_PI); //angular frequency
                sineRads *= (hcount + 1); //scale frequency by harmonic number
                sine = sin(sineRads); 
                ptr[tcount] += sine; //add to other results for this time step
            }
        }
        free(ptr);
        ptr = NULL;     
    }
    return 0;
}
于 2012-09-01T19:46:26.183 回答
3

我没有检查所有代码,但我的猜测是错误在 malloc 调用中。你必须更换

 double *ptr = malloc(sizeof(double*) * TIME);

为了

 double *ptr = malloc(sizeof(double) * TIME);

因为您想为双精度分配大小(而不是指向双精度的指针的大小)。

于 2012-09-01T19:43:35.740 回答