Jump to content
  • Advertisement
Sign in to follow this  
Spencer Allen McMillan

Memory Allocation

This topic is 2150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm learning memory allocation online right now and I'm having some issues. I'm trying to create the size of an array in run-time and I seem to be having some issues. When I code it all inline it works fine, but when I put it in a function I seem to be doing something wrong. I keep getting this error on Visual Studio: error C2082: redefinition of formal parameter 'a'

Here's the code.


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void randomArrayFill( int *a, int size )
{
int *a = new int[size];
for( int i = 0; i < size; i++ )
{
a = rand() % 100;
}
}

int main()
{
//----Init--------------------------------------------------------------
int size = 0;
int *a = 0;
srand(0);
//----End Init----------------------------------------------------------
cout << "Enter the size of an array to create: ";
cin >> size;

randomArrayFill( a, size );
for( int i = 0; i < size; i++ )
{
cout << a;
}
cin.get();
cin.get();
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
The problem is the line int *a = new int[size]. Get rid of the first int (as it makes that line a variable declaration) and that should fix this problem.

Share this post


Link to post
Share on other sites
I'm stumped. It compiles fine, but when it gets to the for function in main that prints off the array it says I don't have access to it. Here's the revised code.
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void randomArrayFill( int *a, int size )
{
a = new int[size];
for( int i = 0; i < size; i++ )
{
a = rand() % 100;
}
}

int main()
{
//----Init--------------------------------------------------------------
int size = 0;
int *a = 0;
srand(0);
//----End Init----------------------------------------------------------

cout << "Enter the size of an array to create: ";
cin >> size;

randomArrayFill( a, size );

for( int i = 0; i < size; i++ )
{
cout << a << " ";
}
cin.get();
cin.get();
return 0;
}

Share this post


Link to post
Share on other sites
The variable a in main is different from the variable a in randomArrayFill because you're passing it by value. You need to pass a reference or a pointer to the variable you want to modify.
[source]
void randomArrayFill(int *&a, int size)
{
...
}
[/source]
If you're passing the pointer by value, you're only modifying a copy of the pointer which is local to the function. With a reference to the pointer, you can modify the original pointer in main.

Share this post


Link to post
Share on other sites
It's worth pointing out that the issue you're having here is not unique to pointers. You need to understand pass-by-value and pass-by-reference, even without pointers getting involved. What does this code print?

[source lang="cpp"]
void changeValue( int a )
{
a = 42;
}

int main()
{
int a = 7;
changeValue( a );
cout &lt;&lt; a;
return 0;
}
[/source] Edited by osmanb

Share this post


Link to post
Share on other sites
Also you are leaking memory as the array you create with a = new int[size]; is never deleted. You should add a "delete [] a;" at the bottom of your main.

Share this post


Link to post
Share on other sites
Note that it is idiomatic to use the return value of a function where possible:

int *randomArrayFill( int size )
{
int *result = new int[size];
for( int i = 0; i < size; i++ )
{
a = rand() % 100;
}
return result;
}


Once you have learned about memory allocation, you can then go on to use the standard library containers to avoid unnecessary manual memory management:

#include <vector>

std::vector<int> randomArrayFill( int size )
{
std::vector<int> result;
for( int i = 0; i < size; i++ )
{
a.push_back(rand() % 100);
}
return result;
}

This will prevent the memory leaks NightCreature83 mentioned.

Share this post


Link to post
Share on other sites
Be aware, rip-off has a typo in his first example. The line:
a = rand() % 100;
should be
result = rand() % 100;

Like this

int *randomArrayFill( int size )
{
int *result = new int[size];
for( int i = 0; i < size; i++ )
{
result = rand() % 100;
}
return result;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!