2014-10-28 104 views
0

當我試圖運行這個簡單的C代碼打印與功能測試memset的inizialited一個PTR變量...MEMSET指針錯誤

#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: [email protected]@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: [email protected]@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: [email protected]@GLIBC_2.2.5 (fileops.c:1261) 
==26745== by 0x4EB1F5B: [email protected]@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) 
+2

瞭解變量範圍並閱讀[未定義的行爲](http://en.wikipedia.org/wiki/Undefined_behavior)。 – 2014-10-28 15:05:35

回答