#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;
}

Recommended Answers

All 9 Replies

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?

#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;
}

vector sizes are always in unsigned integers, so change the for-iterator i into an unsigned int.

also in the first program there is no memory leak.

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.

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?

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:

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;	
}

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.

>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.

>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.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.