- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误:* glibc 检测到 randtest: free(): 无效的下一个大小(正常): 0x00000000023abbe0 **当两件事之一发生时。首先,当我尝试释放已分配的内存时,或者(删除该代码后)我的 main 方法返回(这也会隐式释放该内存)。
这是代码(没有免费)。我正在尝试构建两个随机大小的排序数组来测试函数。
int main(int argc, char **argv) {
int tests = 10;
int i;
int j;
int *array1;
int *array2;
int holder;
int size1;
int size2;
for(i = 0; i < tests; i++){
size1 = rand() % 30;
array1 = malloc(sizeof(int) * (size1));
size2 = rand() % 30;
array2 = malloc(sizeof(int) * (size2));
for (j = 0; j < size1; j++){
if (j == 0)
holder = 0;
else
holder = array1[j-1];
array1[j] = rand() %5 + holder;
}
for (j = 0; j < size1; j++){
if (j == 0)
holder = 0;
else
holder = array2[j-1];
array2[j] = rand() %5 + holder;
}
merge(array1, size1, array2, size2);
//assert(isSorted(merge(array1, size1, array2, size2), size1+size2) == 0);
}
return 0;
}
完整的错误跟踪:
======= Backtrace: =========
/lib64/libc.so.6[0x3751876166]
/lib64/libc.so.6[0x3751878c93]
/lib64/libc.so.6(fclose+0x14d)[0x37518667cd]
randtest[0x4019df]
randtest[0x402306]
/lib64/libc.so.6(exit+0xe2)[0x3751835e22]
/lib64/libc.so.6(__libc_start_main+0x104)[0x375181ed24]
randtest[0x400d09]
======= Memory map: ========
00400000-00404000 r-xp 00000000 00:22 33295023 /nfs/stak/students/t/todtm/src/midterm/randtest
00603000-00604000 rw-p 00003000 00:22 33295023 /nfs/stak/students/t/todtm/src/midterm/randtest
00604000-00605000 rw-p 00000000 00:00 0
023ab000-023cc000 rw-p 00000000 00:00 0 [heap]
3751000000-3751020000 r-xp 00000000 fd:00 131107 /lib64/ld- 2.12.so
375121f000-3751220000 r--p 0001f000 fd:00 131107 /lib64/ld-2.12.so
3751220000-3751221000 rw-p 00020000 fd:00 131107 /lib64/ld-2.12.so
3751221000-3751222000 rw-p 00000000 00:00 0
3751800000-375198b000 r-xp 00000000 fd:00 131112 /lib64/libc-2.12.so
375198b000-3751b8a000 ---p 0018b000 fd:00 131112 /lib64/libc-2.12.so
3751b8a000-3751b8e000 r--p 0018a000 fd:00 131112 /lib64/libc-2.12.so
3751b8e000-3751b8f000 rw-p 0018e000 fd:00 131112 /lib64/libc-2.12.so
3751b8f000-3751b94000 rw-p 00000000 00:00 0
3751c00000-3751c83000 r-xp 00000000 fd:00 131151 /lib64/libm-2.12.so
3751c83000-3751e82000 ---p 00083000 fd:00 131151 /lib64/libm-2.12.so
3751e82000-3751e83000 r--p 00082000 fd:00 131151 /lib64/libm-2.12.so
3751e83000-3751e84000 rw-p 00083000 fd:00 131151 /lib64/libm-2.12.so
3756400000-3756416000 r-xp 00000000 fd:00 131134 /lib64/libgcc_s-4.4.7-20120601.so.1
3756416000-3756615000 ---p 00016000 fd:00 131134 /lib64/libgcc_s-4.4.7-20120601.so.1
3756615000-3756616000 rw-p 00015000 fd:00 131134 /lib64/libgcc_s-4.4.7-20120601.so.1
7fd4fae39000-7fd4fae3c000 rw-p 00000000 00:00 0
7fd4fae65000-7fd4fae68000 rw-p 00000000 00:00 0
7fffbdf80000-7fffbdf95000 rw-p 00000000 00:00 0 [stack]
7fffbdfff000-7fffbe000000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abort (core dumped)
关于我做错了什么有什么想法吗?
最佳答案
这可能只是“未定义行为”的情况,因为您在初始化 array2
的循环中使用了 size1
。一旦您写入的内容超出了为 array2
预留的内存,您就会覆盖 malloc
用于跟踪内存分配的一些额外数据,此时所有的赌注都将被取消。
此外,如果您想要随机数,您应该调用 srand
来为 RNG 提供种子。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv) {
int tests = 10;
int i, j;
int *array1, *array2;
int size1, size2;
srand((unsigned)time(NULL));
for(i = 0; i < tests; i++){
size1 = rand() % 30;
array1 = malloc(sizeof(int) * size1);
size2 = rand() % 30;
array2 = malloc(sizeof(int) * size2);
array1[0] = rand() % 5;
for (j = 1; j < size1; j++)
array1[j] = rand() % 5 + array1[j-1];
array2[0] = rand() % 5;
for (j = 1; j < size2; j++)
array2[j] = rand() % 5 + array2[j-1];
merge(array1, size1, array2, size2);
//assert(isSorted(merge(array1, size1, array2, size2), size1+size2) == 0);
}
return 0;
}
关于c - 检测到 glibc - 主方法返回时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542901/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!