gpt4 book ai didi

c - Memset 指针错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:02 25 4
gpt4 key购买 nike

<分区>

当我尝试运行这个简单的 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)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com