c - Printing (Char*)(Void*) works in main program but not function -


i have array of structures called nodes. each node contains field of void pointer.

in function take specific node , assign void pointer string, string containing result of decimal has been converted binary.

the issue accessing , printing void pointer cast char* works fine in function assign void* new char* , prints fine when returning main function. not print when try print in separate function take node[] , index of array arguments.

to illuminate confusion simplified version of program:

#include <stdio.h> #include <stdlib.h> #include <string.h>  #define listsize 100  typedef struct node {     union{         void *dataptr;         int countr;     }dataitem;     int  link; }node;  void loaddata(node*); void printfunc(node[], int);  void main() {     struct node stack[listsize];      loaddata(&stack[0]);      //this prints fine     char * temp;     temp = (char*)(stack[0].dataitem.dataptr);     printf("\nmain():  stack[empty].dataitem.dataptr = %s\n", temp);       printfunc(stack, 0);  }  void loaddata(node* link){     char string[220];      int n, c, k, i;      printf("enter integer: ");     scanf("%d", &n);      = 0;      (c = 31; c >= 0; c--)     {         k = n >> c;          if (k & 1){             string[i] = '1';             i++;         }         else{             string[i] = '0';             i++;         }         if (c == 0){ string[i] = '\0'; }//end string     }      link->dataitem.dataptr = &string;      //this prints fine:     printf("\nloaddata(): link->dataitem.dataptr %s\n", (char *)(link->dataitem.dataptr)); }   void printfunc(node stack[], int newlink){      //this not work!     char* temp;     temp = (char*)(stack[newlink].dataitem.dataptr);     printf("\npush():  stack[newlink].dataitem.dataptr %s\n", temp); } 

output: output of program

i compiling in visual studios 2012. know pointers in gcc microsoft c compiler can little different.

what writing making program unable print void* cast char* in printfunc function?

this happens in code:

as call loaddata function, allocates string[220] on stack. stack looks this:

[main variables] [loaddata variables, including string[220]] <-- head 

then load data exits. exits, moves stack pointer back. @ time stack looks this:

[main variables] <-- head [loaddata variables, including string[220]] 

note, in case string still technically there, can read , accessed, purely coincidence , depends on complier implementation. other compiler might have erased it, or optimized in other way. function exits, there should no pointers left pointing variables allocated on stack! code violates this, leaking pointer stack-allocated string. @ time code in danger zone! moment access pointer, code can crash, computer can catch fire, or world can cease exist.

but in particular case string happens still accessible, print main, appears printed properly. gives illusion fine. illusion disappears call printfunc, because occupy stack space string located!

[main variables] [printfunc variables] <-- head 

note, string gone! stack variable still points memory, contains garbage!

how address it? well, if plan return function, need either allocate string on calling side:

int main {     ...     char string[220];     loaddata(&stack[0], string); // , make loaddata use argument string instead of creating own     ... } 

or make loaddata allocate string on heap:

char* loaddata (...) {     char* string = malloc(220);     ... } 

in case not forget deallocate in main later:

int main() {     loaddata(&stack[0]);     ...     free(stack[0].dataitem.dataptr); } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -