943,735 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 688
  • C++ RSS
Sep 1st, 2009
0

Does this programme have any memory Leaks?

Expand Post »
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
invisi is offline Offline
27 posts
since Aug 2009
Sep 1st, 2009
0

Re: Does this programme have any memory Leaks?

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?


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
invisi is offline Offline
27 posts
since Aug 2009
Sep 1st, 2009
0

Re: Does this programme have any memory Leaks?

vector sizes are always in unsigned integers, so change the for-iterator i into an unsigned int.
Reputation Points: 13
Solved Threads: 18
Junior Poster in Training
Topi Ojala is offline Offline
60 posts
since May 2009
Sep 1st, 2009
0

Re: Does this programme have any memory Leaks?

also in the first program there is no memory leak.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is online now Online
1,066 posts
since Apr 2009
Sep 1st, 2009
0

Re: Does this programme have any memory Leaks?

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.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Sep 2nd, 2009
0

Re: Does this programme have any memory Leaks?

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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,862 posts
since Dec 2008
Sep 2nd, 2009
0

Re: Does this programme have any memory Leaks?

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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. }

Quote ...
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Sep 2nd, 2009
0

Re: Does this programme have any memory Leaks?

>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Sep 2nd, 2009
0

Re: Does this programme have any memory Leaks?

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
>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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,862 posts
since Dec 2008
Sep 2nd, 2009
0

Re: Does this programme have any memory Leaks?

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
invisi is offline Offline
27 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help on developing simple recursive function
Next Thread in C++ Forum Timeline: Explain the following recursive function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC