Thread: Code for int *growArray (int* p_values, int *size) is incorrect, Chapter 14, page 152

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    11

    Code for int *growArray (int* p_values, int *size) is incorrect, Chapter 14, page 152

    Code for int *growArray (int* p_values, int *size) is incorrect, Chapter 14, page 152 in the Jumping into C++

    Code:
    int *growArray (int* p_values, int *size)
    {
    *size *= 2;
    int *p_new_values = new int[ *size ];
    
    
    /* size has been changed by this time, so in the below for loop when you loop thru' o to size-1 and dereference the values in the original array p_values, once you go beyond the value of the original size -1 you go beyond the bounds of the original array and are dereferencing stuff that doesn't belong to the original array. You should save the original size and only loop from 0 to the size -1 of the original size value */
    
    for ( int i = 0; i < *size; ++i )
    {
    p_new_values[ i ] = p_values[ i ];
    }
    delete [] p_values;
    return p_new_values;
    }
    Sample Code 37: resize_array.cpp (continued)

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    When you loop, you need to use the old value of size, so maybe store it in a variable, before updating size.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So this is a completely over-engineered solution but this is a more C++11 version of what you have.
    Code:
    #include <array>
    #include <memory>
    #include <algorithm>
    
    
    template <typename T>
    auto grow_array(
        T const* const old_data, 
        size_t const old_size, 
        size_t const new_size)
    -> std::unique_ptr<T[]>
    {
        std::unique_ptr<T[]> data{new T[new_size]};
        std::uninitialized_copy(old_data, old_data + old_size, data.get());
        return data;
    }
    
    
    auto test(void) -> void
    {
        std::array<int, 3> const x{0, 1, 2};
    
    
        auto data = grow_array(x.data(), x.size(), size_t{26});
    }
    godbolt

    Compiled but not run.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    For "over-engineered" read "completely retarded".

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    O_o

    T_T

    Well, such is life.

    You're just lucky uninitialized_move is C++14/17 otherwise I would've even SFINAE'd it up and had separate overloads for movable classes vs non-movable classes :P

    Also, you can just return unique_ptrs, how cool is that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-04-2017, 12:54 AM
  2. Incorrect struct size
    By the_jackass in forum C Programming
    Replies: 5
    Last Post: 02-09-2015, 10:05 AM
  3. Incorrect struct size
    By A10 in forum C++ Programming
    Replies: 7
    Last Post: 07-18-2010, 07:21 AM
  4. Struct size incorrect
    By redneon in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 07:01 PM
  5. [C] Get the page-size in memory
    By BianConiglio in forum Windows Programming
    Replies: 2
    Last Post: 12-18-2004, 05:12 AM

Tags for this Thread