• Dynamic Arrays

    From Student Project@21:1/5 to All on Tue Oct 29 04:30:57 2024
    XPost: alt.comp.lang.c++.misc

    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] << ", ";
    }
    }

    /********************************************************************************/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Schwarz@21:1/5 to [email protected] on Mon Oct 28 21:48:41 2024
    XPost: alt.comp.lang.c++.misc

    On Tue, 29 Oct 2024 04:30:57 +0000, Student Project
    <[email protected]d> wrote:

    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] << ", ";
    }
    }

    /********************************************************************************/

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Student Project on Tue Oct 29 08:56:00 2024
    XPost: alt.comp.lang.c++.misc

    On 29/10/2024 05:30, Student Project wrote:
    Do you guys use dynamic arrays to create arrays at compile time (without using vector class)? For example:


    "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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paavo Helde@21:1/5 to Student Project on Tue Oct 29 13:47:23 2024
    XPost: alt.comp.lang.c++.misc

    On 29.10.2024 06:30, Student Project wrote:
    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};

    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().

    /* 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.


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)