当我尝试运行这个简单的C代码,在函数测试中打印一个用memset命名的ptr变量...
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
unsigned char * test(int len){
unsigned char ptr[len];
memset(ptr, 1, len);
return ptr;
}
int main(){
unsigned char * temp;
int i;
temp = test(10);
for (i=0;i<10;i++)
printf("temp[%d]=%c", i, temp[i]);
}
我用valgrind得到了以下错误,我将如何修复此代码?为什么这是错的?为什么我无法打印临时变量?
==26745== Conditional jump or move depends on uninitialised value(s)
==26745== at 0x4EB2271: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:867)
==26745== by 0x4E818BF: vfprintf (vfprintf.c:1661)
==26745== by 0x4E8B388: printf (printf.c:33)
==26745== by 0x40064A: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745== Uninitialised value was created by a stack allocation
==26745== at 0x400617: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745==
==26745== Conditional jump or move depends on uninitialised value(s)
==26745== at 0x4EB229E: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:875)
==26745== by 0x4E818BF: vfprintf (vfprintf.c:1661)
==26745== by 0x4E8B388: printf (printf.c:33)
==26745== by 0x40064A: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745== Uninitialised value was created by a stack allocation
==26745== at 0x400617: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745==
==26745== Conditional jump or move depends on uninitialised value(s)
==26745== at 0x4E818C3: vfprintf (vfprintf.c:1661)
==26745== by 0x4E8B388: printf (printf.c:33)
==26745== by 0x40064A: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745== Uninitialised value was created by a stack allocation
==26745== at 0x400617: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745==
==26745== Syscall param write(buf) points to uninitialised byte(s)
==26745== at 0x4F233B0: __write_nocancel (syscall-template.S:81)
==26745== by 0x4EB0A82: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1261)
==26745== by 0x4EB1F5B: _IO_do_write@@GLIBC_2.2.5 (fileops.c:538)
==26745== by 0x4EB3ADD: _IO_flush_all_lockp (genops.c:848)
==26745== by 0x4EB3C39: _IO_cleanup (genops.c:1013)
==26745== by 0x4E730FA: __run_exit_handlers (exit.c:95)
==26745== by 0x4E73194: exit (exit.c:104)
==26745== by 0x4E58ECB: (below main) (libc-start.c:321)
==26745== Address 0x4025008 is not stack'd, malloc'd or (recently) free'd
==26745== Uninitialised value was created by a stack allocation
==26745== at 0x400617: main (in /home/grados-sanchez/git/merkle-code/merkle-codigos-C/test)
==26745==
temp[0]=temp[1]=temp[2]=temp[3]=temp[4]=temp[5]=temp[6]=temp[7]=temp[8]=�temp[9]=�==26745==
==26745== HEAP SUMMARY:
==26745== in use at exit: 0 bytes in 0 blocks
==26745== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==26745==
==26745== All heap blocks were freed -- no leaks are possible
==26745==
==26745== For counts of detected and suppressed errors, rerun with: -v
==26745== ERROR SUMMARY: 31 errors from 4 contexts (suppressed: 0 from 0)
答案 0 :(得分:2)
你是returning a pointer to an automatic variable。 ptr
在堆栈上创建,并在return
执行时消失。
要解决此问题,请将ptr
声明为static
或在调用函数中声明它并将其作为参数传递。