Jump to content

  • Log In with Google      Sign In   
  • Create Account


Memory Allocation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 01 July 2012 - 03:39 AM

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[i] = 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[i];
}
cin.get();
cin.get();
return 0;
}


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1148

Like
1Likes
Like

Posted 01 July 2012 - 03:51 AM

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.

#3 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 01 July 2012 - 04:12 AM

Thanks! I've got another issue, but I'm going to see if I can work it out before askin' another question. Thanks again! 0/

#4 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 01 July 2012 - 04:34 AM

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[i] = 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[i] << " ";

}

cin.get();

cin.get();

return 0;

}



#5 Brother Bob   Moderators   -  Reputation: 7774

Like
1Likes
Like

Posted 01 July 2012 - 04:50 AM

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.
void randomArrayFill(int *&a, int size)
{
	 ...
}

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.

#6 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 01 July 2012 - 05:05 AM

Thanks Brother Bob! I shall read/practice with pointers some more. :D

Edited by afmack, 01 July 2012 - 05:05 AM.


#7 osmanb   Crossbones+   -  Reputation: 1455

Like
2Likes
Like

Posted 01 July 2012 - 11:48 AM

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, 01 July 2012 - 11:50 AM.


#8 NightCreature83   Crossbones+   -  Reputation: 2669

Like
0Likes
Like

Posted 02 July 2012 - 01:25 AM

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.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#9 rip-off   Moderators   -  Reputation: 7641

Like
1Likes
Like

Posted 02 July 2012 - 03:57 AM

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[i] = 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.

#10 BeerNutts   Crossbones+   -  Reputation: 2555

Like
2Likes
Like

Posted 02 July 2012 - 03:15 PM

Be aware, rip-off has a typo in his first example. The line:
a[i] = rand() % 100;
should be
result[i] = rand() % 100;

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

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#11 NightCreature83   Crossbones+   -  Reputation: 2669

Like
0Likes
Like

Posted 03 July 2012 - 03:12 AM

Note that it is idiomatic to use the return value of a function where possible:
...
Once you have learned about memory allocation, you can then go on to use the standard library containers to avoid unnecessary manual memory management:
...
This will prevent the memory leaks NightCreature83 mentioned.

Just as a side note you have to learn memory management and all it's little caveats before you move onto the standard containers as not all shops use the standard containers. Some shops avoid the standard library if they can for various reasons and thus you are off having to know memory managment, also it gives you an insight into how the Standard Containers work in the background.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#12 mhagain   Crossbones+   -  Reputation: 7413

Like
0Likes
Like

Posted 03 July 2012 - 03:24 AM

One note about memory management is that - depending on the APIs available to you - manual memory management may very well turn out to be more suitable for you. E.g. on Windows, a single HeapDestroy call is all that is needed to free a whole chunk of separate allocations, and without needing to keep track of individual allocations within that chunk.

Of course when learning you should treat this as an "advanced topic" and keep away from it - learn the proper way first. But do be aware that it does exist and can occasionally be a preferable choice - just don't do it yet.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS