Thread: Resize array values of "empty" indices

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    Resize array values of "empty" indices

    I have a trivial resize function that just doubles array size when original list is full. From what I see, C++ treats unset indices as "empty" with values of 0. After I declared new double-sized array and copied elems from old list, I tried to output it in main but once it goes past last index w/ an item, it starts printing weird values, rather than 0's to indicate unset indices. The old and new lists are respectively: hashtable_1, hashtable_2.

    The code is very trivial. Please let me know.
    Code:
    #include <iostream>
    using namespace std;
     
    //Note: trivial resize...I think there's a better one yet
    void resize_v1(int* old_list, int* new_list, int size)//Note: new list will be declared in main
    {
     
     for ( int i = 0; i < size; ++i )
      new_list[i] = old_list[i];
    }
     
    int main()
    {
     int hashtable_1[15] = {0,0,9,0,1000,44,19,201,666,444,57,88,88908,77,33};
     
     int size_1 = sizeof(hashtable_1)/sizeof(int);//this is capacity, NOT num elems in list so far
      
     int hashtable_2[30] = {};
     
     cout << "Empty slot: " << hashtable_2[29] << endl;//I'm talking about this line, why does it output 0, yet in the for-loop below, it 
    outputs: -1 etc
     
     int size_2 = sizeof(hashtable_2)/sizeof(int);
     
     resize_v1(&hashtable_1[0], &hashtable_2[0], size_2);
     
     cout << size_2 << endl;
     
     for ( int i = 0; i < size_2; ++i )//weird values outputted once its past 15th index (last index that's occupied from old list: hashtable_1
     {
      cout << "cur i: " << i << endl;
      cout << hashtable_2[i] << " , " << endl << endl;
     }
     cout << endl;
     
     return 0;
    }
    Last edited by monkey_c_monkey; 07-08-2012 at 03:39 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First of all, C++ does not initialize any elements by default at all. Unless you explicitly initialize them, their values are undefined. There is no "empty" element. If you partly initialize an array, it implicitly initializes the rest of the array to a default value (0 for integers).
    Secondly, your function does not resize anything. It copies over elements from one array to another, essentially a std::copy.
    Thirdly, you are invoking undefined behavior by copying more elements into hashtable_1 than its capacity. Fix that before wondering about the output.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're asking it to copy 30 items out of an array of that only holds 15 items*, into an array that holds 30 items.
    *That's a buffer overrun!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-07-2011, 06:23 PM
  2. Replies: 2
    Last Post: 11-16-2011, 05:55 AM
  3. Disableing the "resize" function on a window
    By Queatrix in forum Windows Programming
    Replies: 15
    Last Post: 04-24-2005, 01:53 PM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. How do I "resize" an array?
    By Yoshi in forum C++ Programming
    Replies: 1
    Last Post: 09-10-2002, 12:17 PM