I'm tracking down a stupid computer bug. I'm just sort of curious why
'/0' string won't show up after I add it. Here is the code snippet
void *allocate_string_array(size_t len, char *line, char **strarr)
{
int count = 0;
if( (len+1) < MAXLINE) {
if((strarr=malloc(sizeof(line) + 1)) == NULL){
return NULL;
}
strarr[count] = dupstr(line);
}
else {
fprintf(stderr, "Input line is too long \n");
exit(EXIT_FAILURE);
}
*(strarr+len+1)='\0';
return *strarr;
}
When I first enter the function
(gdb) print *strarr
$4 = 0x0
(gdb) print line
$5 = 0x804a180 "this is a really long long line\n"
Right after I leave the function
(gdb) step
120 return *strarr;
(gdb) print line
$6 = 0x804a180 "this is a really long long line\n"
(gdb) print *strarr
$7 = 0x840a180 "this is a really long long line\n"
(gdb)
My brain doesn't always function correctly after I've worked 14 hours.
if((strarr=malloc(sizeof(line) + 1)) == NULL)
should have been
if((strarr=malloc(strlen(line) + 1)) == NULL)
However, my compiler didn't bitch about the typo. Here is what happens when I turn on full warnings.
[cda@localhost ~]$ gcc -g -Wall -ansi -Werror -pedantic x_tty.c -o x_tty
[cda@localhost ~]$
Now what does
What is "*(strarr+len+1)='\0';" trying to do?
It's just any ugly hack because I was getting some funky output. I figured the reason why I was getting funky stuff was because my string wasn't terminated. However, it only works half the time. I still somethines get
I wasn't too sure what I was doing either. I was just trying to get it to work at the time. Anyhow, I thought about it and I think using strcpy() is the correct approach.