malloc unable to assign memory + doesnt warn
- by sraddhaj
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';
}