A note: You would not do this in real life. You would use a container of int
, possibly std::list
but more likely std::vector
, not pointers to int
. This is probably an assignment to make the learner struggle for a while with pointers.
So let's deal in pointers and do this the hard way.
int* random = (int*)std::rand();
means Get me a random number and store the random number as a memory address. What does this address point to? Anybody's guess. It's random. That makes it a bad idea.
A pointer must point to a valid object of the same type (or has an is-a relationship with the pointer's type) or it should be pointed at a "safe" parking location like nullptr
until it can be pointed at a valid object. There are exceptions to this, but you'll learn those later.
Given
I'm implementing an assignment question that requires to store a list of pointers to random integers.
std::list<int*> randomInt;
Is probably correct. But... You still need an int
to point at, so
- Get a valid integer,
- store the random number as an integer in that integer, and
- store a pointer to that integer in the
list
.
So how do you get a valid integer? Obviously you need more than one so,
int number; // easy, but not enough numbers.
is out. All of randomInt
would point at the same place and only the last value generated would be stored.
If you don't know how many numbers you're getting you need dynamic storage and should use new
int * random = new int; // get an int from dynamic memory
*random = std::rand(); // generate random number and assign to int pointed at by random
(or a smart pointer if they are available to you). Remember everything you new
you will have to delete
for (int * p:randomInt) // for all pointers in randomInt
{
delete p; // free the memory (and other stuff you'll cover later)
}
after you are finished using it to avoid memory leaks. This is disturbingly error-prone, it is sickeningly easy to have a path that misses the delete
or delete
s while the allocation is still needed, so avoid this in real life. Use Automatic allocation, containers and smart pointers. If you can't, embrace the power of RAII.
Where possible avoid having to mess around with managing dynamic memory, smart pointer managed or otherwise, because dynamic memory always has a cost.
In this case it looks like a maximum of five, so you can make an array
int numbers[5];
(but NOT a local variable
std::list<int*> generateInt(){
int numbers[5]; // do not use!!! FATAL!!!
as it would go out of scope and vanish at the end of the function, leaving the program with a list
full of pointers to invalid objects) and point at the elements of the array. For a simple program, you could get away with a static
local variable
std::list<int*> generateInt(){
static int numbers[5];
or a global variable
int numbers[5];
std::list<int*> generateInt(){
but what if the function is called more than once? The second call would destroy the results of the first call. This may be tolerable, but the responsibility for making this call and guaranteeing the program works as expected falls on the programmer.
My suspicion is the Asker is intended to use new
. Check with whomever assigned the problem to see what other options they will accept.