Memory Leaks or not.

C C C C C C C C C C C

Page 1 of 1

5 Replies - 474 Views - Last Post: 08 January 2010 - 08:54 PM Rate Topic: -----

#1 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Memory Leaks or not.

Post icon  Posted 08 January 2010 - 08:16 PM

I'm dynamically allocated memory with this piece of code.
#include <stdio.h>

int main() {
	int i;
	int tempC;
	void *tempPt;
	int maxSize = 5;
	char *dynamicArray = (char*) malloc(5 * (sizeof(char)));
	
	if (dynamicArray == NULL) {
		printf("Error could not allocate enough memory! Exiting. \n");
		exit(1);
	}
	
	for (i = 0;;++i) {
		if (i == maxSize) {
			maxSize = maxSize * 2;
			tempPt = realloc(dynamicArray, maxSize);
			if (tempPt == NULL) {
				printf("Error could not allocate enough memory! Exiting. \n");
				exit(1);
			}
			else {
				dynamicArray = tempPt;
				printf("dynamicArray now has a new memory size of: %i\n", maxSize);
			}
		}
		if((tempC = getchar()) != '\n') {
			dynamicArray[i] = tempC;
			printf("dynamicArray[%i] = %c\n", i, dynamicArray[i]);
		}
		else
			--i;
		if(tempC == EOF) {
			free(tempPt);
			free(dynamicArray);
			
			printf("Deallocated Memory.\n");
			getchar();
		}
	}
}




However, I'm worried that if you don't exit with CNTRL+C, there might be a memory leak.
I'm running Dev-C++ with mingw, on windows. Does it take care of it if you just exit it with the X on the top right corner or what...

Is This A Good Question/Topic? 0
  • +

Replies To: Memory Leaks or not.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6056
  • View blog
  • Posts: 23,493
  • Joined: 23-August 08

Re: Memory Leaks or not.

Posted 08 January 2010 - 08:25 PM

When the program exits, the memory is reclaimed by the operating system. Memory leaks are more of a concern for long-running programs.
Was This Post Helpful? 0
  • +
  • -

#3 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Memory Leaks or not.

Posted 08 January 2010 - 08:37 PM

K also in this part,
else {
				dynamicArray = tempPt;
				printf("dynamicArray now has a new memory size of: %i\n", maxSize);
			}



Should I add the line free(dynamicArray); right after else before I assign it to tempPt?

Edit:

Also, am I making this dynamic array right? Because Since I was wondering, I printed out the character in the loop before it was assigned the getchar() and it gave me basically the whole alphabet in caps and lower. Nvm Its just uninitialized :P

This post has been edited by awfa3: 08 January 2010 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Memory Leaks or not.

Posted 08 January 2010 - 08:40 PM

When you assign dynamicArray to tempPtr, you're losing the handle to the memory to which dynamicArray points.


So yes, you need to free that up before you hand it another pointer/handle.
Was This Post Helpful? 0
  • +
  • -

#5 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Memory Leaks or not.

Posted 08 January 2010 - 08:44 PM

Thanks KYA and Jack

This post has been edited by awfa3: 08 January 2010 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Memory Leaks or not.

Posted 08 January 2010 - 08:54 PM

I was looking through your code and was wondering why you have a separate pointer for your realloc? Why not assign it back to the same pointer?


It's preference, but for conciseness, why not manipulate the same handle?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1