Thread: Memory overlap in function call

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Memory overlap in function call

    Am facing a strange problem. On debugging the code it is seen that a local double array in a function overlaps the memory address of a passed integer array.

    How is such an issue taken care of in a foolproof manner?

    Thanks,
    Last edited by wots_guge; 06-05-2006 at 09:47 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Can you show us the code which resulted in this, and the resulting disassembly if possible?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by Tonto
    Can you show us the code which resulted in this, and the resulting disassembly if possible?
    The entire code runs into a few thousand lines and am not sure if snippet would help.

    This problem occurs on increasing the dimensions of memory required, the addresses looks like this:

    &S[1] (int array passed to function): 0x00433a24
    &wts[i] (double array - local to function): 0x004339a8

    as index i increases it forces contents of S to change and leads in crash (as per this progm logic).

    Does this info help? Has anyone come across something similar, why does stack memory creep into heap?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Does this info help? Has anyone come across something similar, why does stack memory creep into heap?

    A question like this came up in a thread about exceeding the memory with an STL container on comp.lang.c++.moderated, and I feel that you may find the answer to this question there when you start reading. I know that you aren't using STL containers, but this is very closely related.

    copy pasta: http://groups.google.com/group/comp....806e916abf26d3

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post the current function, and the function which calls it.
    Plus any declarations for sizes of objects or types.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The link by citizen is a red herring in this. If adding to an STL container failed, an exception will have been thrown, so you could not pass a pointer to data it contains. The only way you could trigger this problem in that circumstance would be to catch the exception and act as if no error had occurred (and then yield undefined behaviour by assuming the container contains more than it does).

    At a guess (without seeing code), the problem is that the your function is falling off the end of the double array. The difference (in bytes, I assume) between the addresses you've quoted is 7C (124), which means that (effectively) writing to wts[i] with i 15 or more will overwrite your passed integer array.

    Another possible cause of the effect you're seeing is that you're passing an uninitialised pointer as an argument to the function (and the value passed happens to correspond to your double array + 124 bytes offset).

    Either way, you're getting undefined behaviour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. create variable size arrays
    By s-men in forum C Programming
    Replies: 6
    Last Post: 08-27-2007, 07:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM