Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I have class IMAGEINFO which stores information about BMP image. Code: class IMAGEINFO { int IMfilesize; int IMheight; int IMwidth; int IMbitsperpixel; int IMnoofcolors; unsigned char IMcolortable; int IMvectorsize; ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5

    segmentation error

    Hello,

    I have class IMAGEINFO which stores information about BMP image.

    Code:
          
     class IMAGEINFO
              {
                 int IMfilesize;
                 int IMheight;
                 int IMwidth;
                 int IMbitsperpixel;
                 int IMnoofcolors;
                 unsigned char IMcolortable;
                 int IMvectorsize;
                 char* IMheader;
                char* IMrasterdata;
                public:
                IMAGEINFO();
               ~IMAGEINFO();
               void getimageheader(ifstream&);
               void getcolortable(ifstream&);
               void getrasterdata(ifstream&);
               void getimageinfo(string);
               void putimageheader(ofstream&);
               void putcolortable(ofstream&);
               void writeimageinfo(string);
            };
             
      
            
               void IMAGEINFO::putimageheader(ofstream& file)
                {
                   int counter;
       
                  for(counter=0;(IMheader+counter)!=NULL;counter++)
                  {
                      file.write((IMheader+counter),sizeof(char));
                  }
    }



    I get a segmentation error:
    Program received signal SIGSEGV, Segmentation fault.
    0xb7d398aa in memcpy () from /lib/tls/i686/cmov/libc.so.6


    I know that the segmentation error i happening because the program i trying to access an unallocated memory. I have checked if it is NULL .
    I would like to know why it is happenning?
    -thankyou
    swetha

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    What is it that you want to achieve with this statement?

    Code:
    (IMheader+counter)!=NULL
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    5
    Hi,

    Code:
    void  IMAGEINFO::getimageheader(ifstream& filenm)
    {
       int counter=0,index;
       if(filenm.is_open())
       {
          filenm.seekg(2,ios::beg);
          filenm.read((char*)&IMfilesize,sizeof(int));
          cout<<"The filesize is"<<IMfilesize<<"\n";
          filenm.seekg(18,ios::beg);
          filenm.read((char*)&IMwidth,sizeof(int));
          cout<<"The width is"<<IMwidth<<"\n";
          filenm.seekg(22,ios::beg);
          filenm.read((char*)&IMheight,sizeof(int));
          cout<<"The height is"<<IMheight<<"\n";
          filenm.seekg(28,ios::beg);
          filenm.read((char*)&IMbitsperpixel,sizeof(int));
          cout<<"Bits per pixel-"<<IMbitsperpixel<<"\n";
          filenm.seekg(46,ios::beg);
          filenm.read((char*)&IMnoofcolors,sizeof(int));
          cout<<"Number of colors-"<<IMnoofcolors<<"\n";
          filenm.seekg(0,ios::beg);
          IMheader=new unsigned char[53];
    
          while(filenm.tellg()<54)
          {
            filenm.read((char*)&IMheader[counter],sizeof(char));
           counter=counter+1;
          }
    }

    I have allocated memory for IMheader.
    so, i expect (IMheader+54)==NULL.i.e when the counter value is 54 i expected the value to be NULL.
    -swetha

  4. #4
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by swethapradeep View Post
    so, i expect (IMheader+54)==NULL.i.e when the counter value is 54 i expected the value to be NULL.
    But IMheader is a pointer to char, i.e. a number that is a memory address. This number can be anything from 0 to about 2^32 or more. If you add 54 to it, it is certainly not NULL

    Dereferencing you do with a leading '*', or you employ the array index operator '[]'.
    Debian GNU/Linux -- You know you want it.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Show your implememtation of the constructors for the IMAGEINFO class. A common newbie C++ programming error is failure to properly initialize member variables, especially pointers. In your case, if you don't initialize it, the pointer is a random bunch of bits - crash is your best hope. If you initialize it to 0 (null) and this is a valid state, then you need to test for null in your code. If it must be initialized when the object is created, then make sure you have done so, but also that you delete it in your destructor (otherwise you end up with a memory leak).
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...