Thread: commenting out printf causes segmentation fault

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    commenting out printf causes segmentation fault

    Hi,

    I am getting a strange problem in my program.

    There is an printf statement that outputs the value of a variable. If I comment that statement out, the program gives me Segmentation Fault, if I leave it there, the program runs fine.


    can someone elaborate the reason for this behavior or give suggestions.

    Thanks,

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Can you provide us with the code?

    Is the value accessed via a pointer?
    Last edited by iceaway; 10-04-2011 at 06:08 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Compile with the -g flag, then run the code in the debugger, or under valgrind.

    Then understand that your printf is just the innocent bystander. The REAL problem is somewhere else in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    here is the piece of code.
    The statement, that I talk about is highlighted.
    If I comment it out, the program gives segmentation fault.

    Code:
     
    
    if(sw_s != sw_d){
    			route_size = 0;
    			route = route_discovery(message_reqs->sw_producer_idx, message_reqs->sw_consumer_idx[c], &route_size);
    			printf("%d\n", route_size);
    			//~ printRoute(route, route_size);	
    			l = 0; 
    			do{
    				link_id = getLink(route[l], route[l+1]);
    				dir = getLoadDirection(link_id);	
    				//~ printf("[check_message_fit_multidimensinally]************ load_limit: %lu, link: %d, dir: %d\n", load_limit, link_id, dir);
    				if (!check_switch_link(
    								dir, 
    								&(sw_link_load[link_id]), 
    								link_id, 
    								(l+1), 
    								&(current_ports_load[sw_s][port_s]), 
    								message_reqs, 
    								packet, 
    								m_size_us, 
    								load_limit))
    				{
    					return 0;
    				}	
    				l++;
    			}while(l < (route_size-1) );		
    		}
    Thanks,

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What about commenting out the previous line instead?

    Somewhere previously in the code, you trashed some memory that wasn't yours.

    This is 'cause' and 'effect'.
    The 'cause' is memory corruption, which you NEED to find and fix.
    The 'effect' is any seemingly innocent line of code which happens to go wrong for no apparent reason.

    Staring at the effect isn't going to tell you anything - it's called rearranging the deck chairs on the Titanic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Unfortunately, you're going to have to look harder than that.

    Generally speaking, a segmentation fault is caused by some form of pointer molestation (dereferencing a NULL, dereferencing an uninitialised pointer, falling off the end of an array). When such operations happen, some random area of memory gets tromped (i.e. data is written to some area of memory where it should not be written). Then later some code comes along and uses the corrupted data to compute some new values. If the value computed is another pointer (or an array index), then some other area of memory may be tromped. Depending on the code, multiple areas of memory might be tromped on as your program continues to execute. Eventually, one of the tromped areas of memory is some memory that the operating system deems your process should not be accessing. The operating system responds by sending a SIGSEGV signal to your process, so your process terminates with a segmentation violation.

    How is this relevant to your problem? Well, your printf() call subtly changes the layout of memory used by your program (or process). For example, there may be some memory allocated to the format string, some stack space may be used by the printf() call itself, etc etc. Because of that, the detectable effects of tromping random areas of memory changes ..... the entire memory tromping sequence may be different because of your one line of code. The original problem (or problems - there can be more than one) will still exist, but the immediate and propagated effects of that problem change. If the operating system does not detect some area of memory being corrupted, it will not send the SIGSEGV signal to your process.

    You may also see similar effects by tweaking compiler optimisation settings - they can also adjust the layout of program and data in memory, and can cause symptoms like segmentation faults to appear in different circumstances or not at all.

    The sad thing is: this means your problem is somewhere in your entire code base, but you can't know where. The root cause may be in code executed before the printf() call, or it may be in code executed subsequently. All the printf() call is doing is highlighting the existence of a problem in your code base. It does not provide any help in identifying what the offending code is.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    thanks everyone,

    I will try to look more,

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by zahid990170 View Post
    Hi,

    here is the piece of code.
    The statement, that I talk about is highlighted.
    If I comment it out, the program gives segmentation fault.

    Code:
     
    
    if(sw_s != sw_d){
    			route_size = 0;
    			route = route_discovery(message_reqs->sw_producer_idx, message_reqs->sw_consumer_idx[c], &route_size);
    			printf("%d\n", route_size);
    			//~ printRoute(route, route_size);	
    			l = 0; 
    			do{
    				link_id = getLink(route[l], route[l+1]);
    				dir = getLoadDirection(link_id);	
    				//~ printf("[check_message_fit_multidimensinally]************ load_limit: %lu, link: %d, dir: %d\n", load_limit, link_id, dir);
    				if (!check_switch_link(
    								dir, 
    								&(sw_link_load[link_id]), 
    								link_id, 
    								(l+1), 
    								&(current_ports_load[sw_s][port_s]), 
    								message_reqs, 
    								packet, 
    								m_size_us, 
    								load_limit))
    				{
    					return 0;
    				}	
    				l++;
    			}while(l < (route_size-1) );		
    		}
    Thanks,
    My guess is that route_discovery is causing a corruption of the value in route_size. You pass the address of the route_size variable which allows route_discovery to manipulate the value it points to. Do you have access to the code in route_discovery?
    As Salem suggested, try commenting out route_discovery. If that stops the segmentation fault, then route_discovery is the most likely culprit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault on printf() [NOOB ALERT]
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-26-2011, 06:44 PM
  2. segmentation fault due to "printf" ?
    By MarkZWEERS in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2009, 12:12 PM
  3. Segmentation fault
    By phoneix_hallows in forum C Programming
    Replies: 8
    Last Post: 08-27-2009, 05:56 AM
  4. Segmentation fault?
    By Camambert in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2009, 11:21 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM