首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Memset指针错误

Memset指针错误
EN

Stack Overflow用户
提问于 2014-10-28 15:01:37
回答 1查看 774关注 0票数 0

当我试图运行这个简单的C代码来打印函数测试中的带有memset的ptr变量时.

代码语言:javascript
复制
#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]);
}

我得到下面的错误,我将如何修复这段代码?为什么这事不对?为什么我不能打印临时变量?

代码语言:javascript
复制
==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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-28 15:03:06

你是returning a pointer to an automatic variableptr在堆栈上创建,并在return执行时消失。

要解决这个问题,可以将ptr声明为static,也可以在调用函数中声明它,并将其作为参数传入。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26611603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档