Thread: Memory Management

  1. #1
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59

    Memory Management

    Hi guys,
    I'm a beginner in C and I need to handle memory. I have lot of arrays, most of all of pretty big size, and I'd like to know if I'm using fine the memory management.

    Code:
    complex *originalSignal = malloc(OVERLENGTH*sizeof(complex)); 
        complex *noisevector = malloc(OVERLENGTH * sizeof (complex)); 
        complex *receivedSignal = malloc(OVERLENGTH * sizeof (complex));
       complex *copiaOriginale = malloc(OVERLENGTH * sizeof (complex));
    complex *crossCorrVect;
    I generate 4 arrays of size of OVERLENGTH complex items.
    The last one is a simple pointer that will become an array. A function gets this pointer as parameter and builds an array; the size of this array is determined by the function, so I don't know if I should malloc here in the main, or just declarate the variable even in the function without using malloc.

    By the way, my main function goes on: when I need to free space with these arrays I just need to do:

    Code:
    free(copiaOriginale);
    free(originalSignal);
    ...
    Is it wrong to do the same with the array crossCorrVect, which I didn't allocate with malloc?

    Code:
    free(crossCorrVect);
    Is there a way to check if i really deleted the arrays?

    I tried to make a check like:

    Code:
    if(originalSignal[randomindex]==NULL)
    printf("ok");
    but I get == as invalid operator.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DeliriumCordia
    The last one is a simple pointer that will become an array. A function gets this pointer as parameter and builds an array; the size of this array is determined by the function, so I don't know if I should malloc here in the main, or just declarate the variable even in the function without using malloc.
    It sounds like the function will be doing the malloc, hence there is no point using malloc at this point. Instead, initialise the pointer to be a null pointer.

    Quote Originally Posted by DeliriumCordia
    Is it wrong to do the same with the array crossCorrVect, which I didn't allocate with malloc?
    It depends. What does this mysterious function that you talked about do? If it allocates memory using malloc (or calloc or realloc) then yes, otherwise, no.

    Quote Originally Posted by DeliriumCordia
    Is there a way to check if i really deleted the arrays?
    Yes, if you set the pointers to be null pointers immediately after calling free on them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    So each time i declare a pointer it should point to NULL?

    And after i free arrays i should declare again pointer=NULL?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DeliriumCordia
    So each time i declare a pointer it should point to NULL?
    Yes, if you are not going to immediately make it point to something. (It does not really "point to NULL". Rather, its value is set to NULL, hence it is a null pointer.)

    Quote Originally Posted by DeliriumCordia
    And after i free arrays i should declare again pointer=NULL?
    Yes, if the pointer is not going to go out of scope soon after, or otherwise will most certainly not be used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Ok, thank you very much.

    Another question. For the crossCorrVect I was talking about, I create a pointer to the array that will be created in a specific function. It will point to the first element of the vector generated by the function.

    Is it wrong to malloc the array in the function, and to free it in the main when it becomes unnecessary?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, that is not wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Thank you very much, very kind

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Yes, if you are not going to immediately make it point to something. (It does not really "point to NULL". Rather, its value is set to NULL, hence it is a null pointer.)


    Yes, if the pointer is not going to go out of scope soon after, or otherwise will most certainly not be used.
    An alternative to initialising a pointer to NULL (if it is not going to be used immediately) is to ensure the pointer is not created until it is needed.

    Similarly, the obvious alternative to setting a pointer to NULL if it is not going out of scope is to ensure it does go out of scope.

    It is quite easy to create a new scope within a function using curly braces, or to create a new function (which also has its own scope). It is then easy to ensure a pointer is created and memory allocated to immediately in the new scope, and similarly it is easy deallocation immediately before the pointer passes out of scope.

    Things are a little trickier if you need a pointer (or memory malloc'd to it) to exist outside the scope it is created. Then the initialising or setting to NULL techniques are certainly options, although I disagree with using them routinely.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management
    By chris.r in forum C Programming
    Replies: 5
    Last Post: 06-08-2010, 07:14 PM
  2. Memory management
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 12-26-2009, 01:11 PM
  3. Memory management - How/why/when
    By micke_b in forum C Programming
    Replies: 29
    Last Post: 11-07-2007, 12:26 PM
  4. Memory management in C
    By nicolas in forum C Programming
    Replies: 2
    Last Post: 03-19-2004, 03:25 PM
  5. Memory Management
    By rasdfasfd in forum C Programming
    Replies: 2
    Last Post: 12-03-2002, 08:57 PM