0

When I trying to run this simple C code to print a ptr variable inizialited with memset in the function test ...

#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]);
}

I get the error below with valgrind, How I will be able to fix this code? Why is wrong this?. Why I can't print the temp variable?

==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)
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Juan
  • 2,073
  • 3
  • 22
  • 37

1 Answers1

2

You are returning a pointer to an automatic variable. ptr is created on the stack and disappears when the return executes.

To fix this, either declare ptr as a static or declare it in the calling function and pass it in as a parameter.

Community
  • 1
  • 1
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107