Thread: stack overflow...need help

  1. #1
    Unregistered
    Guest

    Unhappy stack overflow...need help

    I just translated a Fortran program to C. The C program consists of a main program and several functions. There is one structure that contains integers, integer arrays, character types, and character arrays that I pass with each call to each function. The main program calls one function which calls another function and depending on specific conditions being satisfied this function calls different functions. Well the problem I am experiencing is that a particular function gets called several hundreds of times. This functions purpose is to read a line using fgets from a file and then using sscanf get the required strings from each line. After several hundred iterations of calling this function the program aborts with a stack overflow. In the debugger, I looked at all of the elements contained in the structure including the arrays and they all looked good. Can anyone tell me what might be happening? If needed I will post my code or part of it. I would have done it but it's length is about 2K lines. Please respond.
    Thanks!! Kim

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    When dealing with arrays, did you remember the following

    For an N element array,
    the subscripts in Fortran are 1..N
    the subscripts in C are 0..N-1

  3. #3
    Unregistered
    Guest

    Wink

    I took that into account.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Look for this at the bottom of the reply
    Attach file:
    Maximum size: 102400 bytes

    Put your source code and data file in a zip file, and attach it

    Failing that, post it on a website and post the link here

    Make sure it's reasonably ANSI-C
    Lots of OS/compiler specific stuff makes it hard to work with

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    YOU wrote 2K lines of code and then ask others what might be wrong with it?

    Post a couple of lines in the function if it is possible. If not, try to remove the parts which do not cause the problem(and keep a backup)
    and post the rest of it.

  6. #6
    Unregistered
    Guest

    Smile

    I need to clean the code up a little then I'll zip it and attach it. Thanks so much!

  7. #7
    *
    Guest
    If you are getting stack overflow, it sounds like you have a recursion problem.

  8. #8
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Try to avoid the number of times the function recurses or increase the STACK SIZE of the process

  9. #9
    Unregistered
    Guest

    Smile

    I apologize for the long delay in replying. How can I increase the stack size. I don't know if it matters or not but I am running my application using MS C++ Developer Studio. Thanks! Kim

  10. #10
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    I don't really know if MS VC++ provides with a function called setrlimit(). If it provides this function, you can accomplish that task with this call.

  11. #11
    Guilty Spark 343
    Guest
    Usually, you can set your stacksize in project settings in the compiler. However, be sure that it's not a recursion problem or you'll never have a large enough stack.

  12. #12
    Unregistered
    Guest

    Smile

    I don't know if it would be a recursion problem. The function that gets called hundreds of times only reads a line from a file using fgets. It then uses sscanf to get the required information from the line that was just read in and then leaves. Woudl this be recursion? Kim

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Recursion is where a function calls itself - this doesn't appear to be the problem in this case.

    Here are a couple of possible causes
    1) lots of large arrays as local variables
    2) any kind of data overrun of local variables - especially arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM