Find the answer to your Linux question:
Results 1 to 8 of 8
okay.. i've narrowed down the segfault to the while loop in this code by commenting out the section and testing to see if it worked without the loop.. gdb says ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078

    segfault in while loop


    okay.. i've narrowed down the segfault to the while loop in this code by commenting out the section and testing to see if it worked without the loop.. gdb says that the segfault is from the realloc() call... but i can not figure out what the error on my part is..
    Code:
    char *catstr(int x, ...)
    {
    	va_list argptr;
    	char *newstr=NULL;
    	char *str=NULL;
    	//char *test;
    	//test=newstr;
    
    	va_start(argptr, x);
    	str=va_arg(argptr,char *);
    	if(!(newstr=malloc(strlen(str)+1)))
    	{
    		fprintf(stderr,"\nMemory allocation error.\n");
    		exit(1);
    	}
    	newstr=str;
    	//printf("%s\n%s\n",str,newstr);
    
    	while(x)
    	{
    		str=va_arg(argptr,char *);
    		printf("%s",str);
    		if(!(newstr=realloc(newstr,(strlen(newstr)+strlen(str)+1))))
    		{
    			fprintf(stderr,"\nMemory allocation error.\n");
    			exit(1);
    		}
    		else
    		{
    			strcat(newstr,str);
    			--x;
    		}
    	}
    	va_end(argptr);
    
    	return(newstr);
    }
    i've double-checked the usage of realloc(), and the usage of the macros for variable args.. still no joy.. any ideas?
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  2. #2
    Just Joined!
    Join Date
    Jan 2004
    Location
    Portugal
    Posts
    93
    I seems to me like your while loop will never stop, because X will allways exist.
    if you start x=1, if you run the cicle onde, it will become x=0, run it again: x=-1.... and so on.
    maybe you should put: while (x>0)

    or maybe i'm just dead wrong

  3. #3
    Linux Enthusiast scientica's Avatar
    Join Date
    Sep 2003
    Location
    South- or "Mid-" Sweden
    Posts
    742
    false == 0
    true == !false thus true == !0 (however usually true is "set" to -1, at elast in lower languages due to !0 = -1 and !-1 = 0 -- bit wise not operation , still any non zero value is true)

    So what this loops should to is to loop while x!=0 (x==true), but as hll are pokey about data types, maybe it's like microft.pt says, x>0 is probably best.

    Just one question, what's teh value x of when it segfaults? (you culd dump x for each loop)
    Regards Scienitca (registered user #335819 - http://counter.li.org )
    --
    A master is nothing more than a student who knows something of which he can teach to other students.

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Well, the error seems quite natural to me:
    Quote Originally Posted by lordnothing
    Code:
    newstr=str;
    You know, that sets the newstr pointer to point to the same storage that str points to, it doesn't copy the string in str to newstr. Thus, when you realloc() newstr, you really realloc() str, which I'm guessing might even be statically allocated, or placed in the stack. In any case, it can obviously lead to errors. Instead, use this:
    Code:
    strcpy(newstr, str);
    As a side note, this code isn't really very fast, since you do extremely many NUL searches.

  5. #5
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by scientica
    true == !false thus true == !0 (however usually true is "set" to -1, at elast in lower languages due to !0 = -1 and !-1 = 0 -- bit wise not operation , still any non zero value is true)
    No, in fact in C !0 = 1 and ~0 = -1. The ! operator isn't the bitwise NOT operator, but the logical NOT operator. The ~ operator is the bitwise NOT operator. C differs between logical and bitwise operators like this:

    Bitwise:
    NOT: ~
    AND: &
    OR: |
    XOR: ^

    Logical:
    NOT: !
    AND: &&
    OR: ||
    (There is no logical XOR operator... I don't really know why)

    In many other languages, like BASIC, (NOT 0) is -1. C, however, usually uses the logical operators, that always resolve to either 0 or 1. !0 == 1, and !(anything other than 0) == 0. Likewise, (2 && 5) == 1, not 7, as you'd expect in other languages (however, (2 & 5) == 7).

    Having logical operators have the advantage of for example !5 being false. With bitwise operators, ~5 is -6 (with signed number operations), which would still be true.

  6. #6
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    dolda: i tried changing the newstr=str statement to strcat(newstr,str) and that failed, too... i'll try strcopy() and post the result..

    others: while(x) says the same thing as while(x!=0)... if x=<nonzero>, it is 'true'.. when x=0, it returns false. but i did look at the value of x in a backtrace call in gdb.. it failed on the first iteration of the loop (x=4)...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  7. #7
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    okay.. tried dolda's idea and it got further in the loop... still segfaulted, though.. this time it was when x=1.. gdb said that the segfault is occurring when x=1.. here's the output from gdb-->backtrace:
    Code:
    #0  0x400a2293 in strlen &#40;&#41; from /lib/libc.so.6
    #1  0x080485c2 in catstr &#40;x=1&#41; at catstr.c&#58;33
    #2  0x0804867e in main &#40;&#41; at catstr.c&#58;57
    'list' shows the section of code where catstr() is called in main()... if you need me to, i can post the rest of the program (main() just sets values to 4 char * variables to pass to catstr())
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  8. #8
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    okay.. scientica helped me solve it on IRC.. he told me to change the condition of the while loop to x!=1... it worked fine after that...

    UPDATE: just so somebody with a similar problem knows why that worke... i forgot to account for the fact that i used the first argument *before* entering the while loop.. so i was trying to get 4 *more* arguments when there were only 3 left...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •