用于GDB的Valgrind工具
Valgrind检测到的适当错误是什么用于GDB的Valgrind工具,gdb,valgrind,Gdb,Valgrind,Valgrind检测到的适当错误是什么 首先,这个代码段给出了两个编译错误 在函数“int main()”中: 错误:从“void*”到“char*”的转换无效[-fpermissive] p=malloc(10) ^ 错误:返回“int”[-fppermissive]的函数中的返回语句没有值 返回 使用这些更改更正代码 在使用malloc分配内存时键入cast,如下所示。 p=(char*)malloc(10) main函数的返回类型是int,因此相应地更改返回值,如下所述。 返回0;//使
^ 错误:返回“int”[-fppermissive]的函数中的返回语句没有值 返回
- 在使用malloc分配内存时键入cast,如下所示。 p=(char*)malloc(10)
- main函数的返回类型是int,因此相应地更改返回值,如下所述。 返回0;//使用返回0,而不是仅返回
- 在使用delete操作之前,对指针进行有效性检查,如下所示。 如果(p) 删除(p)
int main()
{
char *p;
p = malloc(10);
delete(p);
return;
}
你自己试试看?
==30050== Memcheck, a memory error detector
==30050== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==30050== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==30050== Command: ./valgrindtest
==30050==
main Start
==30050== Mismatched free() / delete / delete []
==30050== at 0x4C2C18D: operator delete(void*) (vg_replace_malloc.c:576)
==30050== by 0x4006E0: main (valgrindtest.cpp:16)
==30050== Address 0x5a1b040 is 0 bytes inside a block of size 10 alloc'd
==30050== at 0x4C2ABE3: malloc (vg_replace_malloc.c:299)
==30050== by 0x4006C2: main (valgrindtest.cpp:11)
==30050==
main End
==30050==
==30050== HEAP SUMMARY:
==30050== in use at exit: 0 bytes in 0 blocks
==30050== total heap usage: 1 allocs, 1 frees, 10 bytes allocated
==30050==
==30050== All heap blocks were freed -- no leaks are possible
==30050==
==30050== For counts of detected and suppressed errors, rerun with: -v
==30050== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)