Discussion:
Dynamic Arrays
(too old to reply)
Student Project
2024-10-29 04:30:57 UTC
Permalink
Do you guys use dynamic arrays to create arrays at compile time (without
using vector class)? For example:

/********************************************************************************/
#include <iostream>

void Generate_Random(size_t size);

int main(void)
{
// generate dynamic array by changing 20 to something else!
Generate_Random(20);
}

void Generate_Random(size_t size)
{
srand((unsigned)time(NULL));
int *numbers = new int[size]{0};

/* initialize the first number to 0 then don't use it
* start the for loop from 1 as usual to make it easy
*/
numbers[0] = 0;

for (size_t i = 1; i <= size; i++)
{
numbers[i] = 100 + rand() % 101;
}

for (size_t i = 1; i <= size; i++)
{
std::cout << numbers[i] << ", ";
}
}

/********************************************************************************/
Barry Schwarz
2024-10-29 04:48:41 UTC
Permalink
On Tue, 29 Oct 2024 04:30:57 +0000, Student Project
Post by Student Project
Do you guys use dynamic arrays to create arrays at compile time (without
/********************************************************************************/
#include <iostream>
void Generate_Random(size_t size);
int main(void)
{
// generate dynamic array by changing 20 to something else!
Generate_Random(20);
}
void Generate_Random(size_t size)
{
srand((unsigned)time(NULL));
int *numbers = new int[size]{0};
/* initialize the first number to 0 then don't use it
* start the for loop from 1 as usual to make it easy
*/
numbers[0] = 0;
for (size_t i = 1; i <= size; i++)
{
numbers[i] = 100 + rand() % 101;
}
for (size_t i = 1; i <= size; i++)
{
std::cout << numbers[i] << ", ";
}
}
/********************************************************************************/
The last iteration through both for loops invokes undefined behavior
(and hopefully results in some kind of access violation).

The largest array index you can use with numbers is size-1. Change
the <= to < in both for statements.

Why do you consider starting your loop at 1 easier than starting it at
0?
--
Remove del for email
David Brown
2024-10-29 07:56:00 UTC
Permalink
Post by Student Project
Do you guys use dynamic arrays to create arrays at compile time (without
"Dynamic compile-time" is an oxymoron.

What you are doing here is creating a C-style array at run-time.

There's nothing wrong with using C-style arrays when you need them, but
if you are creating the array in dynamic memory (with "new") and a size
known only at run time, then it's usually better to use a vector<> for
flexibility and convenience unless you are trying to keep overheads to
the absolute minimum.

And if you know the size of the array at compile time, consider using a
std::array<> type instead of a C-style array. It is again more flexible
and somewhat "safer", but has no overheads.

No matter what kind of array you have, however, remember that in C++,
arrays of size N are indexed from 0 to N-1. Loops are therefore almost
invariably of the form "for (size_t i = 0; i < size; i++) ...". Your
code has an off-by-one error.

Note that if you had used a std::array<> and have good warnings enabled
in your compiler, there's a fair chance that the compiler would have
spotted that error. And if you had used a std::vector<> appropriately,
you could have got a run-time exception showing the problem. And if you
had used either of these, you could have used a modern C++ syntax for
the loop that doesn't need the endpoints spelt out in the code - when
you don't write these things manually, it's a lot harder to make a mistake.

Keep up the learning!
Paavo Helde
2024-10-29 11:47:23 UTC
Permalink
Post by Student Project
Do you guys use dynamic arrays to create arrays at compile time (without
/********************************************************************************/
#include <iostream>
void Generate_Random(size_t size);
int main(void)
{
// generate dynamic array by changing 20 to something else!
Generate_Random(20);
}
void Generate_Random(size_t size)
{
srand((unsigned)time(NULL));
int *numbers = new int[size]{0};
This is basically what std::vector does, and std::vector would do it so
much better (exception safety, iterator support, resize support,
avoiding pointer decay, etc). IOW, the above line has no reason for
existence.

In general, there should be no naked 'new' in application level code,
maybe only in some low level classes. For dynamically created objects
there are std::make_unique() and std::make_shared().
Post by Student Project
/* initialize the first number to 0 then don't use it
* start the for loop from 1 as usual to make it easy
*/
numbers[0] = 0;
Been there, done that. This would be a source of endless confusion,
futile struggle, and nasty bugs as demonstrated below. Suggesting to
learn to count from 0, after a while it will appear more natural and
also simpler.
Post by Student Project
for (size_t i = 1; i <= size; i++)
{
numbers[i] = 100 + rand() % 101;
Loading...