Thread: Is this kind of dynamic memory allocation good?

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    3

    Question Is this kind of dynamic memory allocation good?

    Hi! I just started learning a bit more intermediate C and I am implementing the Convex Hull algorithm. Now the problem I face is I don't want the user to have to enter the number of points he has. So this is how i currently handle that:

    First there is an array dynamically allocated where the user input is stored.
    It keeps reading user input until user enters -1.
    Everytime user enters a number that is not -1 size is increased by one.
    Then a new array is allocated size*sizeof(int)
    All the known data from readbuffer is then transfered to the new array and the readbuffer is freed.

    If I understand this correctly the program uses a lot of memory at the begging then a lot is freed.

    NOTE: init_memsize defines the max amount of points allowed. I know that I need 2 dimension to define a point but this question is purely about memory and efficiency.

    CODE:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(){
        int size = 0;
        int init_memsize = 100000*sizeof(int);
        int* readbuffer = (int*) malloc(init_memsize);
        bool endread = false;
        int temp;
        while(!endread){
            scanf("%d", &temp);
            if(temp>0){
                readbuffer[size++]=temp;
            }else{
                endread = true;
            }
        }
        int memsize = size*sizeof(int);
        int* data = (int*) malloc(memsize);
        for(int i = 0; i<=size; i++){
            data[i] = readbuffer[i];
        }
        free(readbuffer);
        /*Actual alogrithm code would go here*/
        free(data);
    }
    Last edited by 64humans; 01-19-2019 at 05:01 PM.

  2. #2
    Registered User
    Join Date
    May 2016
    Posts
    104
    I would go with:
    Code:
    while ((scanf("%d"), &temp) && temp > 0)
        readbuffer[size++] = temp;
    Which I think is clearer and more concise. Either way you'll run into problems if you read more data than readbuffer can handle.

    Also, I don't like this double allocation thing you are going for. it's like counting how many piles of garbage you have to know how many trips you'll need to take to the garbage bin, instead of just taking as much garbage as you can handle at a time.
    I would assign an initial size for data, and use realloc if you determine you need more memory later.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You should also check for NULL to see if realloc or malloc failed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    2
    Another suggestion, instead of
    Code:
    100000 * sizeof(int)
    I recommend replacing 100000 with a #define, enum, or const value as it's a "magic number" and thus for maintainability it would be better if you described what it means.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C dynamic memory allocation
    By Romyo2 in forum C Programming
    Replies: 4
    Last Post: 06-03-2015, 07:04 AM
  2. Dynamic Memory Allocation
    By tictac232434 in forum C Programming
    Replies: 2
    Last Post: 11-20-2012, 12:48 PM
  3. Dynamic memory Allocation
    By exclusive in forum C Programming
    Replies: 4
    Last Post: 11-10-2011, 07:16 AM
  4. Help with dynamic memory allocation
    By malooch in forum C Programming
    Replies: 2
    Last Post: 12-13-2006, 01:26 PM
  5. dynamic memory allocation
    By mag_chan in forum C Programming
    Replies: 13
    Last Post: 10-21-2005, 07:54 AM

Tags for this Thread