How do I generate random numbers on the heap? C++ question.?

default_random_engine engine{ static_cast<unsigned int>(time(0)) };

uniform_int_distribution<int> randomInt{ -10, 10 };

for (int i{ 1 }; i <= 10; ++i)

{

cout << setw(2) << (randomInt(engine)) << endl;

}

return 0;

I have no idea how to add "new" to dynamically allocate each number. I want to randomly generate 10 points between -10 and 10 on the heap.

Update:

And does a vector automatically add to the heap? So this can be done with a vector as well and without using the "new" function?

Update 2:

I updated it:

vector<int> myVector(10);

default_random_engine engine{ static_cast<unsigned int>(time(0)) };

uniform_int_distribution<int> randomInt{ -10, 10 };

for (int i{ 0 }; i < 10; ++i)

{

myVector[i] = randomInt(engine);

cout << myVector[i] << endl;

}

myVector.clear()

Did I do it dynamically? Did I free it from the heap at the end?

1 Answer

Relevance
  • 1 year ago
    Favorite Answer

    Yes, use a vector<> or a list<> to store a bunch of items of the same type in a single object.

    "The heap" is a Unix concept, not a C++ concept. The primary C++ (and C) memory categories are static, automatic and dynamic. There's also the possibility of application-controlled memory, but you can worry about that when you're writing something like a device driver or embedded application.

    What's usually meant by "the heap" is dynamic memory. The low-level C++ way to manage that is with the new and delete operators. Applications should normally avoid those, and prefer to use standard library data structures that (presumably) have all the bugs worked out and avoid the dangling pointer and memory leak errors that are common to direct pointer-management code.

    So, unless you have a special requirement, yes...do use std::vector<> objects to hold dynamic arrays, or std::list<> if fast insertions are more important that fast random access. The memory for stored elements is dynamic ("on the heap"), even though the controlling object could be in any other class of memory, such as a local variable or a member variable in a struct/class/union type.

    A std::vector<> object (or, rather, the data it controls) will grow dynamically , as needed, limited only by available memory at runtime.

    Edit:

    The normal way to release all memory allocated by a vector is to let the vector variable go out of scope. That's guaranteed to call the destructor, which is guaranteed to delete[] all internal pointers created by new[].

    If you want to keep the vector variable, but reduce the size, there are issues:

    1. First off, it's never guaranteed that you'll actually release memory for reuse.

    2. As of C++11, the .clear() member function won't do that ever. It's prohibited by the standard.

    Your best bet in C++11 or later is to resize (or clear) the vector, and then call the .shrink_to_fit() member function. Your code then looks like:

    myVector.clear();

    myVector.shrink_to_fit();

    That will keep the vector object, and shrink the size of "heap" memory if the implementation supports it.

    • husoski
      Lv 7
      1 year agoReport

      That's also why using vector<int>(n) is recommended over new int[n]. Proper allocation and deallocation are guaranteed. (Well, not on a power failure or blue-screen, but whenever possible!)

Still have questions? Get your answers by asking now.