PDA

View Full Version : Memory leak in c++



fasoulas
October 12th, 2010, 08:41 PM
I think i have a memory leak in this code and i need your help
In the increaseby function i copy the contents of the old array to the new one and return the pointer to the new array.
Will the contents of the old array stay in the heap??


#include <iostream>

using namespace std;

//finds array average
double find_avg(int* arr,int size);

//increase the size of the array
int* increaseBy(int* a,int size,int inc);

void main()
{
//variables
int size;

//array size
cout << "Enter array size : ";
cin >> size;

//allocate mamory for new array
int* arr = new int[size];

//enter values
cout << "Enter values: ";
for(int i=0;i<size;i++)
cin >> arr[i];


///////////////////////////////////////

int new_size;

cout << "\nEnter new size: ";
cin >> new_size;

arr = increaseBy(arr,size,new_size);

///////////////////////////////////////

//deallocate memory
delete[] arr;

system("pause");
}

//increase the size of the array
int* increaseBy(int* a,int size,int inc)
{
//allocate mamory for new array
int *p =new int[size+inc];

//copy values from old array
for(int i=0;i<size;i++)
p[i]=a[i];

//deallocate old memory
delete[] a;

//return new array location
return p;
}

I know that this could be implemented with vectors but i do just to have a better understanding of pointers

ibuclaw
October 12th, 2010, 08:54 PM
You can check for memory leaks using valgrind.

I also see this program is written for Windows? system("pause") is yucky and should be avoided. And having a 'void main()' function is illegal...

edit:
To answer your question though, I can't see there being any leaks are possible in your small program.

Regards

GeneralZod
October 12th, 2010, 09:10 PM
int *p =new int(size+inc);


Read this line very carefully - character by character :) Can you spot the mistake? (It's a very common one :))

fasoulas
October 12th, 2010, 09:23 PM
int *p =new int(size+inc);


Read this line very carefully - character by character :) Can you spot the mistake? (It's a very common one :))

i don't see a mistake here maybe you can help me.I am doing this to increase the size of the array.Basically create a new array with all the elements from the old one.

But i think that the old array elements just stay in the memory(heap) and i just change the value of the pointer that points to them,by making it point to the new place in memory that i allocated.

GeneralZod
October 12th, 2010, 09:27 PM
Hint: compare it to this line:



int* arr = new int[size];


(Brackets :))

fasoulas
October 12th, 2010, 09:27 PM
You can check for memory leaks using valgrind.

I also see this program is written for Windows? system("pause") is yucky and should be avoided. And having a 'void main()' function is illegal...

edit:
To answer your question though, I can't see there being any leaks are possible in your small program.

Regards

Yes this is a program that runs on windows (unfortunately).
Why is it illegal to use void main()?

dwhitney67
October 12th, 2010, 09:29 PM
i don't see a mistake here ...
Did you read the line "very carefully", or did you do a quick glance at it and deemed it to be correct?

fasoulas
October 12th, 2010, 09:31 PM
Hint: compare it to this line:



int* arr = new int[size];


(Brackets :))

:mad: You are right,i haven't noticed it.

This program compiles and runs normally on visual studio and windows,is it a mistake in expression or i do something else without knowing it?

lisati
October 12th, 2010, 09:37 PM
Why is it illegal to use void main()?
It's a style thing.

It's "good form" to be able to return something to the OS that indicates success or otherwise of the program
If you're not going to use argc & argv, it's also "good form" to use "void" for the arguments for main.

dwhitney67
October 12th, 2010, 09:49 PM
:mad: You are right,i haven't noticed it.

This program compiles and runs normally on visual studio and windows,is it a mistake in expression or i do something else without knowing it?
Yes... you did something else without knowing it. You allocated a single int object and initialized it (by calling the int constructor) to the value equal to size + inc.

Btw, think about your program... what are the ramifications if one should type in a size of -1 or even -100? Ditto for the incremental value. Consider adding the checks in your code to ensure that bogus values are not inputted by the user.

fasoulas
October 12th, 2010, 10:04 PM
Yes... you did something else without knowing it. You allocated a single int object and initialized it (by calling the int constructor) to the value equal to size + inc.

Btw, think about your program... what are the ramifications if one should type in a size of -1 or even -100? Ditto for the incremental value. Consider adding the checks in your code to ensure that bogus values are not inputted by the user.

thanks a lot for the information about my mistake.