segmentation fault

This is a discussion on segmentation fault within the Linux Programming forums, part of the Platform Specific Boards category; hi, i want to know why i get "segmentation fault" when i try to run exe file in linux using ...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    Unhappy segmentation fault

    hi,
    i want to know why i get "segmentation fault" when i try to run exe file in linux using './' command after compiling using g++ command .what is the problem??
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I believe it gives you that error because there is a problem with memory in your program. Either allocating, or check your boundaries of your arrays, you may have gone over the limit of the array size somewhere.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    okay...

    thanx.
    heres the code.Now wheres the fault ?? PLease let me know.
    Code:
    struct iode
    {
    char* name ;
    };
    
    int main(void)
    {
    iode* ptr;
    cin >> ptr->name ;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    name is a pointer that is uninitialised. You can either change it to be an array, or use dynamic memory allocation. Your choice
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    okay.

    hi,
    thanx for that but anywayz you say the name isnt initialized. how? its already initialised as pointer to char inside the struct.
    Code:
    struct iode
    {
    char*  name ; // do you want me to initialise it  as char* name[] ??
    };
    int main(void)
    {
    // now here i declare the object
    iode* ptr ;
    // and i access name 
    cin>> ptr->name; // do you mean to say i do 
                                // ptr = new[]  char*  ??? or what??
    }
    thanx again dude.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: okay.

    >>its already initialised
    No, it's not.

    >>as pointer to char inside the struct.
    Thats what type of pointer it is. To initialise it, you must point it at a specific memory location.

    You could change your struct entry to have name like this:

    >>char name[100];

    That is the simplest route. It gives you an array of 100 bytes to store the name.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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