我不精通 C 编程,所以如果这不是一个强有力的问题,请原谅我。在下面的代码中,我只能在获取nsamplepts
的值后为samplesVec
分配内存,但我需要将 vector samplesVec
返回给main
供进一步使用(尚未编码)。但是,我收到以下错误:
终端窗口中出现错误: ImportSweeps(3497,0x7fff7b129310) malloc: * 对象 0x7fdaa0c03af8 错误: 未分配正在释放的指针 *在malloc_error_break中设置断点进行调试 中止陷阱:6
我正在使用 Mac OS X Mavericks 和 gcc 编译器。感谢您的帮助。
*已编辑!!!经过评论者的宝贵意见后,以下内容代表了原始问题的解决方案(不再可用)*
下面的代码修改似乎解决了我原来的问题。感谢大家提出的宝贵意见!
/* 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;
}
最佳答案
所以...据我所知...:-)
samplesVec
,如果fileref
为 false,则 ImportSweeps
的返回值未初始化。如果 samplesVec
未显式初始化,自动(== 本地)变量无法保证其值 - 换句话说 samplesVec
可以携带任何地址。如果 samplesVec
幸运地不是 NULL
(另一方面可能经常出现这种情况),您可以尝试释放
未分配的内存垃圾,或者运气不好,在其他地方分配了一个。
如果我的猜测是正确的,您可以通过以下方式轻松解决此问题:
int32_t *samples;
int32_t *samplesVec = NULL;
无论如何,如果您不在下一行中使用它,那么尽快使用一些有意义的错误或虚拟值初始化任何变量是一个好主意。由于指针是可怕的野兽,如果我不在声明时使用有用的值初始化它们,我总是NULL
它们。
编辑:为了可读的英语近似值进行了一些小的改动。 :-)
关于c - 未分配正在释放的指针,中止陷阱 : 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22163219/