Thread: seg_fault I can't debug

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    seg_fault I can't debug

    EDIT: SOLVED PROBLEM THANKS ANYWAY


    I keep getting a seg_fault .. I am certain it has to do with my setting 'processed_pixels' array to some value ..

    heres the gdb debug entry

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x08048cbd in set_bg_processed (png_rows=0x8051000, processed_pixels=0x1)
        at splicer.c:95
    95                                      processed_pixels[i][pixel_j] = 1;
    Ive commented out some code and simplified to help me find the error but I can't quite find it.

    Code:
    	char **processed_pixels = malloc(img_height * sizeof(char*));
    	int i;
    	for(i=0;i<img_height;++i)
    		processed_pixels[i] = calloc(stride, 1);
    
    	set_bg_processed(png_rows);
    Code:
    void set_bg_processed(png_bytepp png_rows, char **processed_pixels)
    {
    	int i,j;
    	for(i=0;i<img_height;++i)
    	{
    		for(j=0;j<stride;j+=channels)
    		{
    			if( 1 ) //condition code elided, still seg_faults
    			{
    				int pixel_j = j / channels;
    				printf("debug: setting %d %d to processed ... \n ",i, pixel_j);
                                    assert( pixel_j >= 0); // asserting pixel_j so it can be shown that it is positive here on the forum wihout showing 'channels' code
    				processed_pixels[i][pixel_j] = 1;
    				printf("\tdone\n");
    			}
    		}
    	}
    }
    i am new to C

    I think it crashes on i=0, pixel_j = 0

    Thanks for helping!
    Last edited by rodrigorules; 05-13-2010 at 05:56 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If the code you've shown is at all accurate, set_bg_processed() requires two arguments, but you are only passing one when you call it.

    Having a variable in a function with the same name as an argument of a function does not magically pass that variable to the function.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yes , I did not know that C would let me compile the code if such an error existed

    hopefully its at least a compiler warning, I should turn those on

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rodrigorules View Post
    yes , I did not know that C would let me compile the code if such an error existed

    hopefully its at least a compiler warning, I should turn those on
    C will let you compile code in a lot of similar situations that look completely unreasonable. That is the beauty of C: it's a blessing and a curse at the same time.

    Rule of thumb is: never assume that the C compiler will warn you about anything that seems unreasonable, as most likely it won't.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-12-2010, 12:57 PM
  2. Binary was not built with debug information.
    By studentffm in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2010, 12:13 PM
  3. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM