malloc unable to assign memory + doesnt warn

Posted by sraddhaj on Stack Overflow See other posts from Stack Overflow or by sraddhaj
Published on 2012-11-03T10:58:43Z Indexed on 2012/11/03 11:00 UTC
Read the original article Hit count: 159

Filed under:
  char *str=NULL;
  strsave(s,str,n+1);
  printf("%s",str-n);

when I gdb debug this code I find that the str value is 0x0 which is null and also that my code is not catching this failed memory allocation , it doesnt execute str==NULL perror code ...Any idea

 void strsave(char *s,char *str,int n)
 {
    str=(char *)malloc(sizeof(char)* n);
            if(str==NULL)
                    perror("failed to allocate memory");
    while(*s)
    {
            *str++=*s++;
    }
            *str='\0';
 }

© Stack Overflow or respective owner

Related posts about c