Dynamic memory allocation and runtime errors.

This is a discussion on Dynamic memory allocation and runtime errors. within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a program which involves storing a fair amount of data in arrays a therefore repeated ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Leamington, Warwickshire, United Kingdom, United Kingdom
    Posts
    3

    Dynamic memory allocation and runtime errors.

    Hi,

    I'm trying to write a program which involves storing a fair amount of data in arrays a therefore repeated calls of malloc (On the scale of a few MB, i'm nowhere near the 2GB/4GB limit). This works perfectly up until I try and build my third array (a little bigger than the others) at which point the program crashes. If I build the array with about a factor of ten less entries it works (until i get a crash later which i guess is because i'm no massively overrunning the array). Interestingly if i try to free any arrays at this point i also get crashes. All i can do in terms of memory allocation is build small arrays.

    Here's a code snippet to give the idea of what i'm doing, i've checked that all of the array sizes are reasonable and aren't being screwed up by something before any malloc calls:


    Code:
    int main ()
    {
    ...
    
    
        double* convlvData;
        
        double *x_input, *y_input, *x_inputPad, *y_inputPad, *y_input_cmplx, *y_input_fft;
        int len_input, len_input_cmplx, len_inputPad;
        
        double *x_resp, *x_respW, *y_resp, *y_respW, *y_resp_cmplx, *y_resp_fft;
        int len_resp, len_resp_cmplx; //len resp must be less than len_data, someone validate for this please.
    
    ...
    
    
    
                if (statusFlag != 0) statusFlag = wrap(x_resp, &x_respW, y_resp, &y_respW, len_resp, len_input, &DataPadRef);
    
    
                showMessage(" Padding Data... ", 1);
    
    
                if (statusFlag != 0) statusFlag = convlvDataPad(x_input, y_input, &x_inputPad, &y_inputPad, DataPadRef, len_input);
    
    //The crash happens within convlvDataPad
    ...
    }
    
    int wrap(double* x_resp, double** x_respW, double* y_resp, double** y_respW, int len_resp, int len_input, int *DataPadRef)
    {
    ...
    
        statusFlag = buildArray ( x_respW , (len_input) );
        statusFlag = statusFlag && buildArray ( y_respW , (len_input) );
    ...
    }
    
    //I.e Wrap contains some array creation.
    
    int convlvDataPad(double* data_x, double* data_y, double** paddata_x, double** paddata_y, int DataPadRef, int len_input)
    {
        ...
                
        statusFlag = buildArray ( paddata_x , padLen);
            
           //CRASH OCCURS IN ABOVE FUNCTION
    
    
        statusFlag = statusFlag && buildArray ( paddata_y , padLen);
    
    
    ...
    }
    
    int buildArray ( double** array, int size )
    {
        int statusFlag = 1;
        showMessage(" (buildArray) Enter BuildArray... ", 1);
        *array = ( double* ) malloc ( sizeof ( double ) * size );
          
     //CRASH HAPPENS HERE IN CALL FROM CONVLV DATA DURING MALLOC LINE
    
        showMessage(" (buildArray) Malloc finished... ", 1);
        statusFlag = ( *array != NULL );
        checkError ( statusFlag , "Memory allocation has failed (buildArray)." , 0 );
        return statusFlag;
    }

    Thanks in advance for any help that can be offered.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Location
    Leamington, Warwickshire, United Kingdom, United Kingdom
    Posts
    3
    Might have fixed it, turns out i was overrunning the arrays built in wrap so when new arrays started to be built they were trying to build on a memory section which was already occupied (I think) which i guess caused the crash.

    I'm using visual studio express for coding right now, is there any functionality in this to check that i'm not overrunning arrays?

    Cheers!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Memory Leak Detection Enabling

    And use this to enable heap sanity checking periodically.
    _CrtSetDbgFlag

    It's OK for $0 in a small programs.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Talus View Post
    Might have fixed it, turns out i was overrunning the arrays built in wrap so when new arrays started to be built they were trying to build on a memory section which was already occupied (I think) which i guess caused the crash.

    I'm using visual studio express for coding right now, is there any functionality in this to check that i'm not overrunning arrays?

    Cheers!
    Nope.

    C has absolutely no run time error checking that is not provided by the operating system.

    It is on you as a programmer to write code that doesn't overrun your array boundaries or whatever other nasties might happen.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Leamington, Warwickshire, United Kingdom, United Kingdom
    Posts
    3
    @Salem

    Thanks, I'll give that a look. It could be incredibly helpful when i'm allocating and freeing everywhere.

    @CommonTater

    So you're saying that the program would never crash due to the fact that I was overrunning arrays and then making new ones where they may have overran into? I didn't realise this. Must have been something else causing the crash, which is a little worrying. I'll look more into it.

    Thanks for your help anyhow

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Talus View Post
    @Salem

    Thanks, I'll give that a look. It could be incredibly helpful when i'm allocating and freeing everywhere.

    @CommonTater

    So you're saying that the program would never crash due to the fact that I was overrunning arrays and then making new ones where they may have overran into? I didn't realise this. Must have been something else causing the crash, which is a little worrying. I'll look more into it.

    Thanks for your help anyhow
    Nope, I didn't say it wouldn't crash... In fact Windows would catch that in many cases and produce an allocation error.

    What it absolutely will do is behave in an unexpected and/or unpredictable manner... You may get variables that change "like magic", you may get stack corruption and/or lockups, you might go for months and see nothing, then one day (usually when demonstrating it for either your boss or a big client) it's going to screw up royally... One prime example was something that happened to me in one of my programs. I was accidentally overrunning an array and clobbered a variable next to it (a struct with an int at the top)... one day I gets a call from my customer... "How come it says we have 4 million and some torgue wrenches in stock?"... Took me a while to figure that one out, I can tell you.

    Just about any screwy thing can happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  2. MAX memory allocation from c runtime heap
    By sandylovesgnr in forum C Programming
    Replies: 2
    Last Post: 10-23-2008, 11:02 PM
  3. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  4. dynamic memory allocation w/o new
    By bd43274 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 01:12 AM
  5. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 10:39 AM

Tags for this Thread


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