Thread: Crash in a return statement

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Crash in a return statement

    Hi,
    I have a qs.

    Code:
    int my_func()
    {
      int r;
      // Some statements
      return r;         <<<< Here crash happens
    }
    
    int main()
    {
     // Some statements
     int y = my_func();
    ................
    }

    Now my qs is if in the return statement of my_func() the crash happens, then
    1. What does that mean? Which usually causes this type of crash?
    2. How to tackle this type of problem?

    Regards,
    u_peerless

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Do you ever set r to anything?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If a crash happens on a return of a simple integer, then the I would look into stack corruption. This is usually caused by writing to memory beyond the array boundaries.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to post the simplest posible compileable program that produces the problem. It will then be solved very quickly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MacGyver View Post
    If a crash happens on a return of a simple integer, then the I would look into stack corruption. This is usually caused by writing to memory beyond the array boundaries.
    array that is allocated on stack...
    Like
    Code:
    int func()
    {
       char buf[2];
       int ret = sprintf(buf, "%s", "Hello world");
       return ret;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If you never assign r to anything, it could be a trap value. When you return it, the program could crash then. Try initializing your variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM