GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2016, 19:26
budgie1980 budgie1980 is offline
New Member
 
Join Date: Feb 2016
Posts: 6
budgie1980 is on a distinguished road

Can anyone help with this segmentation fault


I'm trying to debug this program and I keep getting a fault message. I've marked where the fault happens, but I can't figure out why. Thanks for your help

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main( void ) {
    int i = 0, count = 0, search = 1, found = 0;
    int number = 5;
    int table[10];

    table[0] = number;
    printf( "table[%i]: %i\n", count, table[count] );

    count = 1;
    while( count < 10 ) {
                 table[count] = number++ * 2;
                 printf( "table[%i]: %i\n", count, table[count] );
       count++;
    }

    while( search = 1 ) {
        if( table[i++] == 11 ) {  //Segmentation fault occurs.
           search = 0;
                          found = i - 1;
                  }
                  if( count == i ) {
                          search = 0;
                  }
    }

         if( found )
       printf( "The number 11 is in the table at location: %d\n", found );
    else
       printf( "The number 11 is not in the table.\n" );

    return 0;
}
  #2  
Old 09-Feb-2016, 21:38
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,516
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: can anyone help with this segmentation fault


I believe that you are trying to compare here?
CPP / C++ / C Code:
 while( search = 1 ) {  // search will be set to 1, then the loop is endless, and makes your
         table[i++]     // array go out-of-bounds to segfault!
...should instead be:
CPP / C++ / C Code:
 while( search == 1 ) { 
That is why it's better to keep constants on the left-hand side.

Try compiling with this to see what happens:
CPP / C++ / C Code:
 while( 1 = search ) { // this will error
Then use the real comparison:
CPP / C++ / C Code:
 while( 1 == search ) { // this will be ok
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 11-Feb-2016, 15:48
budgie1980 budgie1980 is offline
New Member
 
Join Date: Feb 2016
Posts: 6
budgie1980 is on a distinguished road

Re: Can anyone help with this segmentation fault


Thanks for your help TurboPT, that was it. I'm pretty new at c programming and it's hard for me to spot my errors sometimes. I'm glad people like you are around to help out dummies like me. Thanks again, I really appreciate it.
  #4  
Old 11-Feb-2016, 20:27
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,516
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Can anyone help with this segmentation fault


Nah, no dummies... I was a newbie myself in the beginning!

Like davekw7x sometimes said in many of his posts: "No one was born knowing this stuff, you know!"

... so you have to start somewhere!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Runs OK but segmentation fault error after Lucas Masalskas C Programming Language 4 01-Jun-2012 16:36
Segmentation fault - strftime - tm structure eurydice C Programming Language 2 12-Apr-2012 01:47
A weird segmentation fault error uwowizard C Programming Language 2 19-Dec-2011 11:32
Debugging a segmentation fault theplankt C++ Forum 1 01-Dec-2011 16:55
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 10:18

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:14.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.