null pointers

This is a discussion on null pointers within the C++ Programming forums, part of the General Programming Boards category; Code: class atom{ int protons; int electrons; int neutrons; public: atom* bond[4]; private: friend int bind(atom&,atom&); public: atom(int x) { ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    null pointers

    Code:
    class atom{
    int protons;
    int electrons;
    int neutrons;
    public:
    atom* bond[4];
    private:
    friend int bind(atom&,atom&);
    public:
     atom(int x) {
      protons=x;
      electrons=x;
      neutrons=x;
      for (int i=0;i<4;i++) bond[0]=NULL;
     }
     int type() { return protons; }
     int get_group() {
      int x=electrons;
      if (!x) return -1;
      if (x>=114) return -1;
      if (x==1) return 1;
      if (x==2) return 8; x-=2;
      for (int i=0;i<2;i++)   if (x<=8) return x;      x-=8;
      for (int i=0;i<2;i++) {
       if (x<=2) return x; x-=2;
       if (x<=10) return -2; x-=10;
       if (x<=6) return x+2; x-=6;
      }
      for (int i=0;i<2;i++) {
       if (x<=2) return x; x-=2;
       if (x<=14) return -2; x-=14;
       if (x<=10) return -2; x-=10;
       if (x<=6) return x+2; x-=6;
      }
      return -1;
     }
    
    };
    int bind(atom& x,atom& y) {
     int count=0; int count_1=0;
     if ((x.bond[3]!=NULL) || (y.bond[3]!=NULL)) return -1;
     while (x.bond[count++] != NULL);
     while (y.bond[count_1++]!=NULL);
     x.bond[count]=&y;
     y.bond[count_1]=&x;
    }
    
    int main()
    {
     atom* atoms[5];
     atoms[0]=new atom(CARBON);
      for (int i=1;i<5;i++) {
       atoms[i]=new atom(HYDROGEN);
       bind (*atoms[0],*atoms[i]);
      }
      cout << "Protons: " << atoms[0]->type() << endl;
      cout << atoms[0]->bond[0]->type() << endl;
      cout << atoms[0]->bond[1]->type() << endl;
      cout << atoms[0]->bond[2]->type() << endl;
      cout << atoms[0]->bond[3]->type() << endl;
     return 0;
    }
    for some reason, when i try to print the types of the atoms that are bonded (the last 4 couts) it gives me an error. i'm almost certain the problem is because bond[0] is a NULL pointer. the bond[0] pointer should have been set by the bind function. can anyone spot the error?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I stopped at -

    bond[0]=NULL;

    should be

    bond[i]=NULL;

    This might be your problem.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In addition Sorensen's change:
    Code:
    >int bind(atom& x,atom& y) {
    > int count=0; int count_1=0;
    > if ((x.bond[3]!=NULL) || (y.bond[3]!=NULL)) return -1;
    > while (x.bond[count++] != NULL);
    > while (y.bond[count_1++]!=NULL);
    > x.bond[count]=&y;
    > y.bond[count_1]=&x;
    //Change the previous two lines to:
     x.bond[--count]=&y;
     y.bond[--count_1]=&x;
    
    //Or modify the while-loops above
    }
    Last edited by swoopy; 03-29-2002 at 07:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 12:16 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21