| | |
Does this programme have any memory Leaks?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <ctime> #include <string> using namespace std; void RandomArrayFill(int* array, int size) { //int* array = new int[size]; cout << "Creating and filling the array with integers..." << endl; for(int i = 0; i< size; ++i) { array[i] = rand() % 101; } cout << "Array = {"; for(int i = 0; i < size; ++i) { cout << array[i] << " "; } cout << "}" << endl; } int main() { srand((unsigned)time(0)); int size; cout << "Enter Size: "; cin >> size; int* newArray = new int[size]; RandomArrayFill(newArray, size); delete[] newArray; newArray = 0; }
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
This is the same programme but done using a vector. Alsthough it works it still comes up with this warning twice:
Warning 1 warning C4018: '<' : signed/unsigned mismatch Line 15
Why is that anyone know?
Warning 1 warning C4018: '<' : signed/unsigned mismatch Line 15
Why is that anyone know?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <ctime> #include <vector> #include <string> #include <cmath> using namespace std; void RandomArrayFill(std::vector<int>& vec) { cout << "Creating and filling the array with integers..." << endl; for(int i = 0; i< vec.size(); ++i) { vec[i] = rand() % 101; } cout << "Array = {"; for(int i = 0; i < vec.size(); ++i) { cout << vec[i] << " "; } cout << "}" << endl; } int main() { srand((unsigned)time(0)); int size = 0; vector<int> intVector; cout << "New Size: " ; cin >> size; intVector.resize(size); RandomArrayFill(intVector); cout << endl; }
Are you sure. His program is tricky in a sense that there is a memory
leak. His function is definitely not doing what he thinks its doing.
One way to tell if you have memory leak is to see if every new
is matched with delete. In your function, you allocate new memory for the array
that is passed, which already has been allocated memory. So when
inside the function, the arrays thats passed is located in whatever
memory address that was reserved inside the function. When he
exits out that functions, that memory is no longer in play. Inside
main he has already the array pointing at a specific address.
So the memory inside the function has a leak. Also you use
2 instances of new but only 1 delete. Does that give you a hint?
leak. His function is definitely not doing what he thinks its doing.
One way to tell if you have memory leak is to see if every new
is matched with delete. In your function, you allocate new memory for the array
that is passed, which already has been allocated memory. So when
inside the function, the arrays thats passed is located in whatever
memory address that was reserved inside the function. When he
exits out that functions, that memory is no longer in play. Inside
main he has already the array pointing at a specific address.
So the memory inside the function has a leak. Also you use
2 instances of new but only 1 delete. Does that give you a hint?
Last edited by firstPerson; Sep 2nd, 2009 at 2:53 am.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
•
•
•
•
vector sizes are always in unsigned integers, so change the for-iterator i into an unsigned int.
unsigned int . It is safer to use the actual type that is typedef'd in the vector class: C++ Syntax (Toggle Plain Text)
void RandomArrayFill(vector<int>& vec) { cout << "Creating and filling the array with integers..." << endl; for (vector<int>::size_type i = 0; i < vec.size(); ++i) { vec[i] = rand() % 101; } cout << "Array = {"; for (vector<int>::size_type i = 0; i < vec.size(); ++i) { cout << vec[i] << " "; } cout << "}" << endl; }
•
•
•
•
Are you sure.••••also in the first program there is no memory leak.
array is redefined. -Tommy (For Great Justice!) Gunn
>In your function, you allocate new memory for the array
>that is passed, which already has been allocated memory.
I suppose he has commented that line.
Hence technically, there are no memory leaks in the first program.
OP>Why is that anyone know?
That is because the .size() member function of the std::vector returns a size_t and not a int. size_t is usually a unsigned int defined by your implementation. Hence it is actually safe to do for(size_t i = 0; i< vec.size(); ++i)
By the way, in your second program, rather than first creating a vector and then resizing, it would have been better if you could have constructed a vector of the given size before hand.
Anyways, it wont matter here for the problem in hand.
Edit: I replied a bit slow.
>that is passed, which already has been allocated memory.
I suppose he has commented that line.
Hence technically, there are no memory leaks in the first program.
OP>Why is that anyone know?
That is because the .size() member function of the std::vector returns a size_t and not a int. size_t is usually a unsigned int defined by your implementation. Hence it is actually safe to do for(size_t i = 0; i< vec.size(); ++i)
By the way, in your second program, rather than first creating a vector and then resizing, it would have been better if you could have constructed a vector of the given size before hand.
Anyways, it wont matter here for the problem in hand.
Edit: I replied a bit slow.
Last edited by siddhant3s; Sep 2nd, 2009 at 10:42 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
I guess it was late. Didn't see the backlashes.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Are you sure. His program is tricky in a sense that there is a memory
leak. His function is definitely not doing what he thinks its doing.
One way to tell if you have memory leak is to see if every new
is matched with delete. In your function, you allocate new memory for the array
that is passed, which already has been allocated memory. So when
inside the function, the arrays thats passed is located in whatever
memory address that was reserved inside the function. When he
exits out that functions, that memory is no longer in play. Inside
main he has already the array pointing at a specific address.
So the memory inside the function has a leak. Also you use
2 instances of new but only 1 delete. Does that give you a hint?
Actually the second instance of new in the function is a comment.
Anyway for those you said I didn't have any memory leaks, thanks thats what I though, just wanted to make sure

Thanks Tom Gunn, the vector<int>::size_type, actually worked
Last edited by invisi; Sep 2nd, 2009 at 3:10 pm.
![]() |
Similar Threads
- Memory Leaks (C++)
- Memory leaks! (C++)
- Memory leaks through stringstream.str() (C++)
- Memory Leaks in Java (Java)
- The definiton of "Memory leaks" (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help on developing simple recursive function
- Next Thread: Explain the following recursive function
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






