Thread: Question about memory allocation

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Question Question about memory allocation

    So, this is a pretty simple question I'm assuming, but I am new to memory management in general so I was wondering if you guys could help me.

    I was wondering if this code has a memory leak?: [C] #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> - Pastebin.com
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(int argc, char **argv) {
    
    	FILE *file_handle = fopen("in.xx", "rb");
    
    	int c;
    	char *delta = malloc(1);
    	delta[0] = '\0';
    	int delta_length = 0;
    	do {
    		c = fgetc(file_handle);
    		
    		if (isspace(c) || c == EOF) {
    			
    			if (delta_length == 0) {
    				goto free_delta;
    			}
    			
    			printf("%s\n", delta);
    			
    		free_delta:
    				free(delta);
    				delta = malloc(1);
    				delta[0] = '\0';
    				delta_length = 0;
    			
    		} else {
    			delta = realloc(delta, 1);
    			delta_length += 1;
    			delta[delta_length - 1] = c;
    			delta[delta_length] = '\0';
    		}
    		
    	} while (c != EOF);
    	
    	free(delta);
    	fclose(file_handle);
    
    	return 0;
    }
    I've ran it through "Deleaker" on Visual Studio and it says it does but it looks alright to me...

    Also, if anyone knows of a good resource of the do's and don'ts of memory management I'd love to hear it.

    Thanks.
    Last edited by Salem; 10-25-2016 at 12:19 AM. Reason: Inlined pastebin lazyness

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    1. You should test that file_handle is not NULL after fopen.

    2. Your goto is completely pointless. Why not just:
    Code:
        if (delta_length != 0)
            print("%s\n", delta);
        ...
    3. realloc doesn't add memory, it sets the allocation to the given size. You're only ever asking for 1 byte (and since you already have that, realloc is probably doing nothing). So something like:
    Code:
                delta_length++;
                void *newmem = realloc(delta, delta_length + 1);
                if (newmem == NULL) {
                    perror("realloc");
                    free(delta);
                    exit(EXIT_FAILURE);
                }
                delta = newmem;
    Note that I used a separate variable to receive realloc's return value just in case it's NULL. That way we still have access to delta's old value so we can free the memory (or whatever).

    4. We would never actually realloc a byte at a time like this. It's inefficient and pointless. Normally you would start out with some reasonable size (say 10 bytes) and then double that each realloc (or maybe multiply it by 3/2).


    BTW, you should post your code here (in plain text and in code tags).

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Quote Originally Posted by algorism View Post
    1. You should test that file_handle is not NULL after fopen.

    2. Your goto is completely pointless. Why not just:
    Code:
        if (delta_length != 0)
            print("%s\n", delta);
        ...
    3. realloc doesn't add memory, it sets the allocation to the given size. You're only ever asking for 1 byte (and since you already have that, realloc is probably doing nothing). So something like:
    Code:
                delta_length++;
                void *newmem = realloc(delta, delta_length + 1);
                if (newmem == NULL) {
                    perror("realloc");
                    free(delta);
                    exit(EXIT_FAILURE);
                }
                delta = newmem;
    Note that I used a separate variable to receive realloc's return value just in case it's NULL. That way we still have access to delta's old value so we can free the memory (or whatever).

    4. We would never actually realloc a byte at a time like this. It's inefficient and pointless. Normally you would start out with some reasonable size (say 10 bytes) and then double that each realloc (or maybe multiply it by 3/2).


    BTW, you should post your code here (in plain text and in code tags).
    Ah, I miss-remembered realloc, thanks! Also, yeah I kinda realized the goto was pointless after a bit :P. As for the byte at a time reallocation you're totally right, but this is just a small test case so I was okay with it. Anyways, thanks! However, I'm assuming there are no leaks?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By raczzoli in forum C Programming
    Replies: 1
    Last Post: 01-07-2013, 07:07 PM
  2. Memory allocation question
    By raczzoli in forum C Programming
    Replies: 13
    Last Post: 09-12-2012, 07:46 AM
  3. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  4. Question about memory allocation between methods
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-16-2008, 08:32 AM
  5. Another Question...memory allocation
    By quickclick330 in forum C Programming
    Replies: 4
    Last Post: 12-12-2007, 04:25 PM

Tags for this Thread