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: invisi is an unknown quantity at this point 
Solved Threads: 0
invisi invisi is offline Offline
Newbie Poster

Does this programme have any memory Leaks?

 
0
  #1
Sep 1st, 2009
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void RandomArrayFill(int* array, int size)
  9. {
  10. //int* array = new int[size];
  11.  
  12. cout << "Creating and filling the array with integers..." << endl;
  13. for(int i = 0; i< size; ++i)
  14. {
  15. array[i] = rand() % 101;
  16. }
  17.  
  18.  
  19. cout << "Array = {";
  20.  
  21. for(int i = 0; i < size; ++i)
  22. {
  23. cout << array[i] << " ";
  24. }
  25. cout << "}" << endl;
  26.  
  27.  
  28. }
  29.  
  30. int main()
  31. {
  32.  
  33. srand((unsigned)time(0));
  34.  
  35. int size;
  36. cout << "Enter Size: ";
  37. cin >> size;
  38. int* newArray = new int[size];
  39. RandomArrayFill(newArray, size);
  40.  
  41. delete[] newArray;
  42. newArray = 0;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: invisi is an unknown quantity at this point 
Solved Threads: 0
invisi invisi is offline Offline
Newbie Poster

Re: Does this programme have any memory Leaks?

 
0
  #2
Sep 1st, 2009
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?


  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <vector>
  5. #include <string>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void RandomArrayFill(std::vector<int>& vec)
  12. {
  13.  
  14. cout << "Creating and filling the array with integers..." << endl;
  15. for(int i = 0; i< vec.size(); ++i)
  16. {
  17. vec[i] = rand() % 101;
  18. }
  19.  
  20.  
  21. cout << "Array = {";
  22.  
  23. for(int i = 0; i < vec.size(); ++i)
  24. {
  25. cout << vec[i] << " ";
  26. }
  27. cout << "}" << endl;
  28. }
  29.  
  30.  
  31. int main()
  32. {
  33.  
  34. srand((unsigned)time(0));
  35.  
  36. int size = 0;
  37.  
  38. vector<int> intVector;
  39.  
  40. cout << "New Size: " ;
  41. cin >> size;
  42.  
  43. intVector.resize(size);
  44.  
  45. RandomArrayFill(intVector);
  46.  
  47. cout << endl;
  48. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: Topi Ojala is an unknown quantity at this point 
Solved Threads: 5
Topi Ojala's Avatar
Topi Ojala Topi Ojala is offline Offline
Newbie Poster

Re: Does this programme have any memory Leaks?

 
0
  #3
Sep 1st, 2009
vector sizes are always in unsigned integers, so change the for-iterator i into an unsigned int.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Does this programme have any memory Leaks?

 
0
  #4
Sep 1st, 2009
also in the first program there is no memory leak.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Does this programme have any memory Leaks?

 
0
  #5
Sep 1st, 2009
If you are using UNIX, you can use valgrind to check for memory leaks. You need to add --tool=memcheck on the command line to check for leaks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,252
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Does this programme have any memory Leaks?

 
0
  #6
Sep 2nd, 2009
Originally Posted by NathanOliver View Post
also in the first program there is no memory leak.
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?
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Does this programme have any memory Leaks?

 
0
  #7
Sep 2nd, 2009
vector sizes are always in unsigned integers, so change the for-iterator i into an unsigned int.
The type might not be unsigned int . It is safer to use the actual type that is typedef'd in the vector class:
  1. void RandomArrayFill(vector<int>& vec)
  2. {
  3. cout << "Creating and filling the array with integers..." << endl;
  4.  
  5. for (vector<int>::size_type i = 0; i < vec.size(); ++i)
  6. {
  7. vec[i] = rand() % 101;
  8. }
  9.  
  10. cout << "Array = {";
  11.  
  12. for (vector<int>::size_type i = 0; i < vec.size(); ++i)
  13. {
  14. cout << vec[i] << " ";
  15. }
  16.  
  17. cout << "}" << endl;
  18. }

also in the first program there is no memory leak.
Are you sure.
I am. The allocation in the function is commented and will not run. If you uncomment it, there will be a compiler error because array is redefined.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Does this programme have any memory Leaks?

 
0
  #8
Sep 2nd, 2009
>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.
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,252
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Does this programme have any memory Leaks?

 
0
  #9
Sep 2nd, 2009
Originally Posted by siddhant3s View Post
>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.
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: invisi is an unknown quantity at this point 
Solved Threads: 0
invisi invisi is offline Offline
Newbie Poster

Re: Does this programme have any memory Leaks?

 
0
  #10
Sep 2nd, 2009
Originally Posted by firstPerson View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC