How come '\0' doesnt show up in the debugger?


 
Thread Tools Search this Thread
Top Forums Programming How come '\0' doesnt show up in the debugger?
# 1  
Old 06-21-2007
How come '\0' doesnt show up in the debugger?

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)
# 2  
Old 06-21-2007
Quote:
Originally Posted by frequency8
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.
'\0' is the end of string character.
# 3  
Old 06-21-2007
Looks horrible.

Your malloc will always do a malloc of 5 bytes on a 32bit computer.

What relationship is the return value supposed to have with strarr?

Count is a constant, what is it needed for?

What is "*(strarr+len+1)='\0';" trying to do?

Does it compile cleanly with "-Wall -Werror" on gcc?
# 4  
Old 06-21-2007
Bear with this.

My brain doesn't always function correctly after I've worked 14 hours.

Code:
if((strarr=malloc(sizeof(line) + 1)) == NULL)

should have been
Code:
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

now
is a really long long line

Last edited by frequency8; 06-21-2007 at 01:16 AM..
# 5  
Old 06-21-2007
What is len for if you are then doing a strlen?

I still haven't worked out why you are assigning to strarr when it was given to you as an argument?

Do you actually mean

Code:
strarr[0]=malloc(strlen(line)+1);

then you should be doing

Code:
strcpy(strarr[0],line);

# 6  
Old 06-21-2007
Sometime this week, I'll post a URL with the ENTIRE code.
# 7  
Old 06-21-2007
I suggest you just post a formal specification for the function "allocate_string_array".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Doesnt allow me to use [CODE] tags

Just got penalised for not performing a feature that didnt work for me. I tried to edit the post twice to add code tags, both manually and via button did not work. code - manual code - button (7 Replies)
Discussion started by: 06s23
7 Replies

2. UNIX for Dummies Questions & Answers

why doesnt it work?

I am trying to print out two fields in a file using awk. So, I have got awk -F '\t' 'NF = 2 {print $1 $2 "]"}' two.txt in a script called what.awk When i run this version like this - ./what.awk then it runs however I want to run the program like this awk -f what.awk two.txt. When I... (8 Replies)
Discussion started by: The undertaker
8 Replies

3. AIX

Vi doesnt work

Hi Guys, I have a strange problem.( AIX 6.1) "vi" is not working at all..Whenever i #vi <anythin> ,, it returns the prompt back. Any clues folks?? (14 Replies)
Discussion started by: muzahed
14 Replies

4. Programming

Alternative debugger to GNU insight debugger

GNU insight debugger is not available now a days and it is required to debug/inspect assembly code as written in the book Assembly Language Programming step by step in Linux so my question is; is there any alternative to insight that I can use instead of insight in which I can get the same... (5 Replies)
Discussion started by: vectrum
5 Replies

5. Shell Programming and Scripting

compiler doesnt work

this is my file I have written. // My first C++ program #include <iostream> int main() { std::cout << "Hi there!" << std::endl"; std::cout << "This is my first C++ program" << std::endl"; return(0); } This is the error I get, why? $ g++ first.cpp ksh: g++: not found (1 Reply)
Discussion started by: gustave
1 Replies

6. Shell Programming and Scripting

loop doesnt work

It just does the break...even though the files are not the same... # Compare extracts #========================================== count=0 while (( count < 5 )) do (( count+=1 )) echo "Try $count" file1=$(ls -l /tmp/psjava.xml|... (5 Replies)
Discussion started by: sigh2010
5 Replies

7. Red Hat

ldapsearch doesnt work.

Hii All, I am using openldap v2.3 on redhat El-4. When i run ldapsearch it returns all the entries. The command runs successfully. But when I run the ldapsearch with following filter option it doesnt work and immediately returns to the shell. ldapsearch uidNumber>=2000 I've started slapd... (0 Replies)
Discussion started by: shamik
0 Replies

8. UNIX for Dummies Questions & Answers

cp doesnt work - Help

When trying to copy a file in Solaris 8 it doesnt copy file or give a error. This worked 100% until the 29th. I've checked the rights and everything seems fine: drwxrwxrwx 2 bmuser bmgroup 11776 Jan 3 10:32 spool This is the file I want to copy: -rwxrwxrwx 1 bmuser bmgroup ... (26 Replies)
Discussion started by: rudi.okelly
26 Replies

9. Programming

why doesnt this compile correctly

hello there when i try and compile i keep getting this error that 'error C2115: 'function' : incompatible types' on the following line of the code: for (i = 0; i < nCommands; ++i) { printf(" Command: \n", i); printComStruct(com); // HERE } the following is my printComStruct... (0 Replies)
Discussion started by: mile1982
0 Replies

10. UNIX for Dummies Questions & Answers

cd.. doesnt work

hi when i want to go to previous directory by typing cd.. i get the following message $ cd.. ksh: cd..: not found Please help rintingtong (2 Replies)
Discussion started by: rintingtong
2 Replies
Login or Register to Ask a Question