Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,200 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,322 people online right now. Registration is fast and FREE... Join Now!




Bus error.

 
Reply to this topicStart new topic

Bus error.

Sepanto
21 May, 2008 - 01:01 PM
Post #1

D.I.C Head
Group Icon

Joined: 20 Mar, 2008
Posts: 97


Dream Kudos: 50
My Contributions
Hi guys. I'm writing a program, whcih gives me a bus error when compiled under g++. would anyone mind telling me why. the bus error is given when initialising the program, never gettign to menuManager() method in the end of interface costructor.
CODE

#include "Interface.h"
#include <iostream>
using namespace std;
int main()
{
    cout<<2;
    Interface *i1=new Interface();
    return 0;
}


CODE

using namespace std;
Interface::Interface()
{    
    numberOfCenters=0;
    numberOfChains=0;
    Chains=NULL;
    Centers=NULL;
    cout<<-3;
    addCenter("Hilel",212);
    cout<<-4;
    addCenter("Hasharon",432);
    cout<<-5;
    addChainClothing("Bastro","Moshe");
    cout<<-6;
    addChainFood("Columbus",3);
    addChainFastFood("George",3,25,35);
     cout<<-1;
    addChainBook("Shuper","C++ programming is fun");
     cout<<-2;
    Chains[0]->addShop(2);
    Chains[1]->addShop(12);
     cout<<0<< endl;
    Chains[2]->addShop(43);
     cout<<1<< endl;
    Chains[3]->addShop(65);
     cout<<2<< endl;
     cout<<"Finished initializing"<< endl;
    Centers[0]->addChainToMall(Chains[0]);
    Centers[0]->addShop(Chains[0]->getShop(2));
     cout<<3<< endl;
    Centers[0]->addChainToMall(Chains[2]);
    Centers[0]->addShop(Chains[2]->getShop(43));
     cout<<4<< endl;
    Centers[1]->addChainToMall(Chains[1]);
    Centers[1]->addShop(Chains[1]->getShop(12));
     cout<<5<< endl;
    Centers[1]->addChainToMall(Chains[3]);
    Centers[1]->addShop(Chains[3]->getShop(65));
     cout<<6<< endl;
    menuManager();
}

CODE

void Interface::addCenter(const char* name,int number)
{
int j,place;
if(findCenter(name,number)==-1)
    {
        place=findPlaceNewCenter(number);
        Center** tempo=Centers;
        Centers= new Center* [++numberOfCenters];
        for (j=0;j<numberOfCenters-1;j++)
            if(j!=place)
                Centers[j]=tempo[j];
        Centers[place]= new Center(number,name);
        delete []tempo;
    }
else
cout<<"not unique error"<< endl;
}

User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Bus Error.
21 May, 2008 - 03:51 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
I think we would need to see [more of] your code, but in general, a bus error is likely to be caused by improperly aligned access to a memory area. For example, if you have an int, which is 4 bytes, you probably can't dereference an int pointer who's pointer value is odd (it likely needs to be in multiples of 2 or 4). The rules are machine-dependent. This would happen when casting a byte pointer type (such as char*) to a type of a larger access width (int*).
User is offlineProfile CardPM
+Quote Post

Sepanto
RE: Bus Error.
21 May, 2008 - 11:42 PM
Post #3

D.I.C Head
Group Icon

Joined: 20 Mar, 2008
Posts: 97


Dream Kudos: 50
My Contributions
What more code do you need?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month