首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未分配被释放的指针,中止陷阱:6

未分配被释放的指针,中止陷阱:6
EN

Stack Overflow用户
提问于 2014-03-04 04:51:53
回答 2查看 6.5K关注 0票数 0

我不精通C语言编程,如果这不是一个很强的问题,请原谅。在下面的代码中,我只能在获得samplesVec的值之后将内存分配给nsamplepts,但是我需要将向量samplesVec返回给main以供进一步使用(尚未编码)。但是,我得到了以下错误:

终端窗口中的错误:ImportSweeps(3497,0x7fff7b129310) malloc:* *对象0x7fdaa0c03af8:指针未分配*在malloc_error_break中设置断点以调试中止陷阱:6

我正在使用Mac小牛和gcc编译器。谢谢你的帮助。

*编辑在评论员提供有价值的意见之后,以下是原问题(不再可用)的解决方案*

下面的代码修改似乎解决了我最初的问题。感谢大家宝贵的投入!

代码语言:javascript
复制
/* Header Files */
#define LIBAIFF_NOCOMPAT 1 // do not use LibAiff 2 API compatibility
#include <libaiff/libaiff.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <math.h>

/* Function Declarations */
void FileSearch(char*, char*, char*, char*, char*);
int32_t *ImportSweeps(char*);

/* Main */
int main()
{
char flag1[2] = "N";
char binname[20] = "bin1"; // dummy assignment
char buildfilename[40] = "SweepR";
char skeletonpath[100] = "/Users/.../Folder name/";
int k, len;

/* Find the sweep to be imported in the directory given by filepath */
FileSearch(skeletonpath, binname, buildfilename, skeletonpath, flag1);
if (strcmp(flag1,"Y")) {
    printf("No file found. End of program.\n");
} else {
    len = (int) strlen(skeletonpath);
    char *filepath = malloc(len);
    for (k = 0; k < len; k++) {
        filepath[k] = skeletonpath[k];
    }
    printf("File found! Filepath: %s\n", filepath);
    // Proceed to import sweep
    int32_t *sweepRfile = ImportSweeps(filepath);
    if (sweepRfile) {
        printf("Success!\n");
        // Do other things with sweepRfile
        free(sweepRfile);
    }
    free(filepath);
}
return 0;
}

/* Sub-Routines */
void FileSearch(char *dir, char *binname, char *buildfilename, char* filepath, char* flag1)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
    fprintf(stderr,"Cannot open directory: %s\n", dir);
    return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
    lstat(entry->d_name, &statbuf);
    if(S_ISDIR(statbuf.st_mode)) {
        /* Found a directory, but ignore . and .. */
        if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
            continue;
        strcpy(binname,entry->d_name);
        strcpy(buildfilename,"SweepR");
        /* Recurse at a new indent level */
        FileSearch(entry->d_name, binname, buildfilename, filepath, flag1);
    }
    else {
        sprintf(buildfilename, "%s%s.aiff", buildfilename, binname);
        if (strcmp(entry->d_name,buildfilename)) {
            strcpy(buildfilename,"SweepR");
        } else {
            sprintf(filepath, "%s%s/%s", filepath, binname, buildfilename);
            strcpy(flag1,"Y");
            break;
        }
    }
}
chdir("..");
closedir(dp);
}


int32_t *ImportSweeps(char *filepath)
{
char *filepathread = filepath;

/* Initialize files for importing */
AIFF_Ref fileref;

/* Intialize files for getting information about AIFF file */
uint64_t nSamples;
int32_t *samples = NULL;
int32_t *samplesVec = NULL;
int channels, bitsPerSample, segmentSize, ghost, nsamplepts;
double samplingRate;

/* Import Routine */
fileref = AIFF_OpenFile(filepathread, F_RDONLY) ;
if(fileref)
{
    // File opened successfully. Proceed.
    ghost = AIFF_GetAudioFormat(fileref, &nSamples, &channels, &samplingRate, &bitsPerSample, &segmentSize);
    if (ghost < 1)
    {
        printf("Error getting audio format.\n");
        AIFF_CloseFile(fileref); return (int32_t) 0;
    }
    nsamplepts = ((int) nSamples)*channels;
    samples = malloc(nsamplepts * sizeof(int32_t));
    samplesVec = malloc(nsamplepts * sizeof(int32_t));
    ghost = AIFF_ReadSamples32Bit(fileref, samples, nsamplepts);
    if (ghost) {
        for (int k = 0; k < nsamplepts; k++) {
            samplesVec[k] = *(samples+k);
        }
    }
    free(samples);
    AIFF_CloseFile(fileref);
}
return samplesVec;
}
EN

回答 2

Stack Overflow用户

发布于 2014-03-04 05:24:56

所以..。据我所知..。:-)

如果samplesVec为false,则未初始化ImportSweeps的返回值。如果==没有显式初始化,自动( samplesVec )局部变量对其值没有任何保证-换句话说,samplesVec可以携带任何地址。如果samplesVec不是运气方面的NULL (另一方面可能经常是这样),那么您可以尝试使用free,一种没有分配的内存垃圾,或者运气非常差的情况下,尝试使用其他分配的内存。

如果我的猜测是正确的,你可以很容易地解决这个问题:

代码语言:javascript
复制
int32_t *samples;
int32_t *samplesVec = NULL;

无论如何,如果没有在下一行中使用任何变量,那么使用一些有意义的错误或虚拟值来初始化任何变量是个好主意。由于指针是可怕的野兽,如果我在声明中没有使用有用的值初始化它们,我总是NULL它们。

编辑:几个小的小变化,一个可读的近似英语。:-)

票数 1
EN

Stack Overflow用户

发布于 2014-03-04 05:29:01

如果AIFF_OpenFile失败,ImportSweeps将返回一个未定义的值,因为samplesVec没有初始化。如果该值为非空值,main将尝试释放它.可以初始化samplesVec = NULL,也可以将代码重新组织为

代码语言:javascript
复制
fileref = AIFF_OpenFile(filepathread, F_RDONLY) ;
if(!fileref) {
{
    // print error message here
    return NULL;
}
// File opened successfully. Proceed.
...

有些人会坚持一个只应该有一个出口的官员--他们的信息不灵通,说的是来自其他人的错误教条,他们同样是不知情和教条的。上面对错误和返回的检查称为保卫子句。每次测试成功时缩进的另一种样式会产生更难读、更难修改和更容易出错的箭头反模式。有关一些讨论,请参见http://blog.codinghorror.com/flattening-arrow-code/http://c2.com/cgi/wiki?ArrowAntiPattern

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22163219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档