Thread: pattern: caller is responsible to release memory?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    pattern: caller is responsible to release memory?

    hey, there,

    Here is the pseudo code:
    1. go through a very large sorted timeseries datafile to determine number of records N
    2. dynamically allocate a matrix M of size N * 3 to hold all data
    3. do statistical analysis
    4. release memory

    step 1 and 2 needs about 227 lines, and I would like to wrap them up as a function, i.e., ??? readdata(???, ???, ???)
    and caller should get the point to the matrix M, and be able to release memory used by M.
    I am not sure how to design the prototype of M. Or is there a better design. Thanks.

    Michael

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what do you think "design the prototype of M" means? Do you mean the prototype of readdata?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And in an easily deletable post, in case I'm wrong:
    On the assumption that you mean "what should readdata look like", M, if it's dynamically allocated, will have to be a int** (or double**, or whatever). You will also need to return to the calling routine the number N. Your function may or may not need to know the name of the file. So there's two or three parameters -- a const char * for the file name [input], an int* for the size [output] and a (whatever)** for the data [output]. You can pick either of the output parameters to be returned if you want, rather than passed in, or you can return a success code (since there's a couple ways to go wrong -- no such file, file invalid, unable to get memory).

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    reply

    yes, tabstop, I need to pass in the filename, return a 2-d double matrix M or size N*3, so is this right?

    void readdata(char *filename, int * N, double **M);

    and caller does this?

    double **M;
    int N;

    readdata("filename", &N, M);

    free(M);

    I am worrying that this probably won't do the job. I an guessing I should use
    double ***M;
    But I cannot sort out my mind.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess you want readdata to take a double ***M and pass in &M as well, if you want that changed.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    48
    so do I free(&M) as well? and would caller be able to access M element as M[1][2]. I guess not, since the leading dimension is unknown at the compilation time. can you check the following code? Thanks!
    Code:
    double **M;
    int N;
    readdata("filename", &N, &M);//call the function
    printf("%f",M[2][2]); //access M element
    free(&M);
    
    where the prototype of readdata() is:
    void(char * filename, int * N, double ***M);

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So there's two methods of making a dynamic array and I have no idea which you've chosen. Method 1 looks like
    Code:
    array = malloc(N * sizeof(*array));
    for (int i = 0; i < N; i++) {
        array[i] = malloc(3*sizeof(*array[i]));
    }
    This allows you to use the standard double-subscript notation; since array is a double **, then array[1] is a double *, which means array[1][2] is a double.
    You would need to clean this up using free in a loop, and then freeing M at the end. M would have to get passed in as &M and array would actually be *the_parameter_passed_in, since you would have to pass in a double*** parameter.

    Or you could just say to heck with it and do one malloc, guaranteeing you contiguous memory:
    Code:
    array = malloc(N * 3 * sizeof(double));
    But as you say, this won't allow you to use [][] notation; you'd have to do it yourself -- M[1][2] would end up being M[1*3+2], which is kind of annoying. But the parameter would only be a double**, M would be a double *, and you would only need to free M at the end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  4. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM