Segmantion fault with a get method without using pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Segmantion fault with a get method without using pointers

  1. #1
    Join Date
    Aug 2013
    Posts
    1

    Question Segmantion fault with a get method without using pointers

    Hello guys, i've tried to create a method to return a rectangle struct, the class already has a object of this structure, but when i try return the variable(not a pointer), i receive a SIGSEGV and i dont know why, i dont acces invalid memory addresses:

    Code:
    Retangulo Crature::GetRect()
    {
        return rect;
    }
    using the method:
    Code:
    if(colision(player->GetRect(), this->rect)){
    ...
    the pointer for the player is passed to the class through a method, but i'm sure(testing with the debugger) the player is working and his memory address is valid.

    thanks for the help

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Segmantion fault with a get method without using pointers

    Quote Originally Posted by Shoxsz View Post
    i dont acces invalid memory addresses:
    Then why are you getting an error? Are you saying there is something wrong with the computer or OS, and not your program logic?
    [CODE]
    using the method:
    Code:
    if(colision(player->GetRect(), this->rect)){
    ...
    the pointer for the player is passed to the class through a method, but i'm sure(testing with the debugger) the player is working and his memory address is valid.
    There is no standard way to test if a pointer is valid or not, unless the pointer is NULL where you know the pointer is invalid.

    A pointer value could look OK, and still be pointing to invalid or memory that used to point to valid data. Unless your OS or compiler library has functions that test for pointer validity, or if the pointer is NULL, or your compiler implementation states exactly what pointer values are designated as invalid, there is no way for you to determine if a pointer is valid or not.

    Also, we don't know when, where, how, or what state your program is in when that line you posted is executed. So posting a single line of code from an entire program provides no help whatsoever. You have a runtime error, and the only way for us to tell you what's wrong is to run your program and debug.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 12th, 2013 at 12:27 PM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,121

    Re: Segmantion fault with a get method without using pointers

    You're dereferencing two pointers. Don't overlook the possibility that your this pointer is bad too.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center