473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Program received signal SIGBUS, Bus error.

I have my c project which works fine and compiles fine on my home machine (MacBook Pro Mac OSX). When I upload it to my school server and compile it there it compiles but I get a bus error when I try option 2. My program gets graded on the school server, so its important I get it to compile lol. My school server is some sort of linux machine, not sure which.

I think the problem code is here:
Expand|Select|Wrap|Line Numbers
  1.                         head->paths[letter] = (node*)malloc(sizeof(node));
  2.                         head->paths[letter]->c = word[i];
  3.                         head->paths[letter]->setn = malloc((strlen(head->setn) +2) * sizeof(char));
  4.                         strcpy(head->paths[letter]->setn,head->setn);
  5.                         head->paths[letter]->setn[strlen(head->setn)] = word[i];
  6.                         head->paths[letter]->setn[strlen(head->setn)+1] = '\0';                        
  7.                         head->paths[letter]->isWord = 0;
  8.                         head = head->paths[letter];
  9.  
Any help is greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node {
  6.     char c;    // the current char
  7.     char *setn; //total
  8.     int isWord;    /* To check if this is a valid word to be written into dict.txt,
  9.                0 is false, 1 is true*/
  10.     struct node *paths[26];    // A subexpression can be followed by any of the 26 characters
  11. };
  12.  
  13. typedef struct node node;
  14.  
  15. void suggest(node *n);
  16.  
  17. int main() {
  18.     int choice,i,letter,j;
  19.     int startWord = 0;
  20.     char word[100];
  21.     FILE *fp;
  22.     char c;
  23.     char cc[2];
  24.     cc[1] = '\0';
  25.     node *head = (node *) malloc(sizeof(node));
  26.     node *cur = (node *) malloc(sizeof(node));
  27.     node *paths[26];
  28.  
  29.     for(i = 0;i<26;i++) {
  30.         paths[i] = (node *) malloc(sizeof(node));
  31.         paths[i]->c = (char)(i+65);
  32.         paths[i]->setn = malloc(2);
  33.         paths[i]->setn[0] = '\0';
  34.         cc[0] = (char)(i+65);
  35.         paths[i]->setn = strcat(paths[i]->setn,cc);
  36.         paths[i]->isWord = 0;
  37.     } //create tree root
  38.  
  39.     while(1) {
  40.         printf("\n=============================================================================\n\n");
  41.         printf("1.    Create a dictionary\n");
  42.         printf("2.    Add a word\n");
  43.         printf("3.    Suggest a word\n");
  44.         printf("4.    Spell check\n");
  45.         printf("5.    Save the dictionary\n");
  46.         printf("6.    Exit\n\n");
  47.         printf("Enter choice: ");
  48.         scanf("%d",&choice);
  49.         if(choice == 1) {
  50.             //Build Dictionary
  51.             fp = fopen("dict.txt","r");
  52.             while((c = fgetc(fp)) != EOF) {
  53.                 letter = (((int)c)-65);
  54.                 if(c == '#') {
  55.                     if(startWord == 0) {
  56.                         startWord = 1;
  57.                         head = 0;
  58.                     }
  59.                     else {
  60.                         startWord = 0;    
  61.                         head->isWord = 1;
  62.                         i = 0;
  63.                     }
  64.                 }
  65.                 else if((int)c >= 65 && (int)c <= 90) {
  66.                     if(head == 0) {
  67.                         head = paths[letter];
  68.                     }
  69.                     else {
  70.                         if(head->paths[letter] == 0) {
  71.                             head->paths[letter] = (node *)malloc(sizeof(node));
  72.                             head->paths[letter]->c = c;
  73.                             head->paths[letter]->setn = malloc((strlen(head->setn) +2) * sizeof(char));
  74.                             strcpy(head->paths[letter]->setn,head->setn);
  75.                             head->paths[letter]->setn[strlen(head->setn)] = c;
  76.                             head->paths[letter]->setn[strlen(head->setn)+1] = '\0';
  77.                             head->paths[letter]->isWord = 0;
  78.                             head = head->paths[letter];
  79.                         }
  80.                         else {
  81.                             head = head->paths[letter];
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.             printf("Dictionary has been created.");
  87.             fclose(fp);
  88.             head = 0;
  89.         }
  90.         if(choice == 2) {
  91.             //Add Word
  92.             printf("Enter word: ");
  93.             scanf("%s",word);
  94.             j = 0;
  95.             while(word[j] != '\0')
  96.                 {j++;} //length
  97.             for(i = 0;i<j;i++) {
  98.                 letter = (((int)word[i])-65);
  99.                 if(head == 0) {
  100.                     head = paths[letter];    
  101.                 }
  102.                 else {
  103.                     if(head->paths[letter] == 0) {
  104.                         head->paths[letter] = (node*)malloc(sizeof(node));
  105.                         head->paths[letter]->c = word[i];
  106.                         head->paths[letter]->setn = malloc((strlen(head->setn) +2) * sizeof(char));
  107.                         strcpy(head->paths[letter]->setn,head->setn);
  108.                         head->paths[letter]->setn[strlen(head->setn)] = word[i];
  109.                         head->paths[letter]->setn[strlen(head->setn)+1] = '\0';                        
  110.                         head->paths[letter]->isWord = 0;
  111.                         head = head->paths[letter];
  112.                     }
  113.                     else {
  114.                         head = head->paths[letter];    
  115.                     }
  116.                 }
  117.             }
  118.             head->isWord = 1;
  119.             printf("New word \"%s\" added to the dictionary",word);
  120.             head = 0;
  121.         }
  122.         if(choice == 3) {
  123.             //Suggest a word    
  124.             printf("Enter subexpression: ");
  125.             scanf("%s",word);
  126.             j=0;
  127.             while(word[j] != '\0') 
  128.                 {j++;} //length
  129.             for(i = 0;i<j;i++) {
  130.                 letter = (((int)word[i])-65);
  131.                 if(head == 0) {
  132.                     head = paths[letter];    
  133.                 }
  134.                 else {
  135.                     head = head->paths[letter];    
  136.                 }
  137.             }
  138.             printf("The suggested words are: ");
  139.             suggest(head);
  140.             head = 0;
  141.         }
  142.         if(choice == 4) {
  143.             //Spell Check
  144.             printf("Enter string: ");
  145.             scanf("%s",word);
  146.             j=0;                 
  147.             while (word[j] != '\0') 
  148.                 {j++;} //string length
  149.             for(i = 0;i<j;i++) {
  150.                 letter = (((int)word[i])-65);
  151.                 if(head == 0) {
  152.                     head = paths[letter];
  153.                 }
  154.                 else {
  155.                     if(head->paths[letter] == 0) {
  156.                         printf("Result: Spelling is incorrect.");
  157.                         break;
  158.                     }
  159.                     else {
  160.                         head = head->paths[letter];
  161.                     }
  162.                 }
  163.             }
  164.             if(head->c == word[i-1]) {
  165.                 printf("Result: Spelling is correct.");    
  166.             }
  167.             head = 0;    
  168.         }
  169.         if(choice == 5) {
  170.             //Save the Dictionary    
  171.         }
  172.         if(choice == 6) {
  173.             //Exit
  174.             break;
  175.         }
  176.     }
  177. }
  178.  
  179. void suggest(node *n) {
  180.     int i;
  181.     if(n->isWord == 1) {
  182.         printf("%s, ",n->setn);    
  183.     }
  184.     for(i = 0;i<26;i++) {
  185.         if(n->paths[i] != 0) {
  186.             suggest(n->paths[i]);    
  187.         }
  188.     }
  189. }
  190.  
Dec 13 '08 #1
2 9303
newb16
687 512MB
Use strlen() instead of home-made loop. sizeof(char) is 1 by definition.

Compile it at school in debug mode and run under gdb until it crashes ( or run gdb providing dumped core ), then post relevant info ( line number, stack trace ) here.
Dec 14 '08 #2
vmpstr
63
I see two (and a half) things that I would fix.

The half is that half of your malloc returns are cast and have are not. Pick one (not casting malloc is the correct thing to do in C).

Other than that,

1. You might want to check if letter is negative before you index into an array using it. I'm referring to character-65 calculation. There are plenty of characters that will cause that expression to be negative.

2. You might want to switch the order of lines 108 and 109. Reason being, once you have overwritten the nul terminator on line 108, strlen on lines 109 loses much of its correctness. If there was only one nul terminator, then it will probably cause the error you are seeing.
Dec 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Vivek Mohan | last post by:
Hi everybody ! I have this (very) simple program written in C which when run, gives me a "Bus Error". I did a lot of googling around on "Bus Error" every where it was all about misaligned...
8
by: rohityogi | last post by:
This statement gives core dump if( pError->pFileName != '\0' ) can someone suggest why ? This statement was failing in all our unix environments so we changed it to: if(...
4
by: Chris | last post by:
I posted this in the C# language group, then thought it might be more appropriate in this group. I would not cross-post except I want the answer so badly. I built small C# Web and Web Service...
1
by: nbs.tag | last post by:
Hi, ive created a webservice that im going to use to filter a table, however when i try to pass the parameters in that will be used, it crashes in the webbrowser with: ...
2
by: MLH | last post by:
I was instructed to get a new updated help file. How might I go about that. The 3072 error was trapped but had no description. It had some- thing to do with having misspelled table name in a...
3
by: mirzarezaee | last post by:
Dear all, I want to measure received signal stregth from a wireless network card. Does any body knows what should I do? I look forward to hearing from you, Yours, Mitra Mirzarezaee
10
by: contactmayankjain | last post by:
Hi, I am getting the following error. Can you tell me any solution to this problem and the reason for this 298 Program received signal SIGBUS, Bus error. 0x000000080137ae02 in...
1
by: vquanski | last post by:
How to program around ora-02068 error On DB2 side, newbie to oracle platform. . .HELP !! Oracle procedure, db link to DB2, gets 02068 rpc disconnect errors inconsistently/randomly, each require...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.