Thread: Problem with structure and class

  1. #1
    Registered User
    Join Date
    Jan 2007

    Problem with structure and class

    Hi !

    Can I declare structure inside class and return pointer to structure.........

    like this.......

    struct line
    int width;
    int color;
    int length;

    class XML

    line* read(char* filepath);
    int m_width,m_color,m_length;


    Implementation of XML file.........

    line* Xml::read(char* filepath)
       line *m_line;
    //some operation of parsing file
    return line;

    And this function ic called from fileParser.cpp

    Xml *xml=new Xml();  
    line *m_line;

    It is compiling but giving segmentation fault.

    Can any body tell me that where is problem........or should I change implementation..


  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    London, UK
    You need to allocate memory for your line. At the moment m_line just points to random memory that isn't owned by your program. When your program tries to use that memory, the OS complains that you haven't asked for that memory and thus shouldn't be allowed to use it and issues a Segmentation Fault signal, which terminates your program.
    Use new to allocate some memory first:
      line *m_line = new line;
    //some operation of parsing file
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    you did allocate memory for m_line with new, right?

    m_line = new line;

    also your quote shows 'return line', when perhaps it should be 'return m_line'.

    but really, you have not pasted enough code or other information to tell why you are getting a seg fault.

    unrelated to your programming problems, XML is a very complex format. if you can avoid it, i recommend finding a 3rd party library to parse it for you. or, if you have a choice in the matter, switching to a more lightweight & friendly format, such as s-expressions:

    the 2nd hosts the source code for a c s-expression parser which i have not used before, but the truth is s-expressions are much better suited to writing your own parser from scratch than XML, which is quite complex and difficult to do properly.
    .sect signature

  4. #4
    Registered User
    Join Date
    Jan 2007
    Thanks QuantumPete and ggs for your help...........


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure vs. class
    By $l4xklynx in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 03:00 AM
  2. Abstract class (IDispatch) in a C structure
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2008, 08:38 AM
  3. Class Membership Problem
    By josephjah in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 01:48 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem constructing a class with a nested struc
    By pliang in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 07:43 PM