Thread: Question about memory allocation

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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