0

I'm doing a c++ assignment that allow user to enter numbers one by one and store then in an array, and dynamically increase this array capacity (multiplying by 2). basically to mimic a “vector” using some rudimental code.

#include<iostream>
using namespace std;

struct myArray {
    const size_t initial_size = 1;
    const size_t growth_factor = 2;
    size_t length = 0;
    size_t capacity = initial_size;
    int *v; // vector
};

int main() {
    int x; 
    myArray numbers;

    while (true) {
        cout << "Please input a numebr (program terminated once a -ve number is entered)" << endl;
        cin >> x;
        if (x < 0) 
            break;

        if (numbers.length == numbers.capacity) {
            int *temp = new int[numbers.capacity *= numbers.growth_factor];
            for (size_t i = 0; i < numbers.length; ++i)
                temp[i] = numbers.v[i];
            delete[] numbers.v;
            numbers.v = temp;
        }
        numbers.v[numbers.length++] = x;
    }

    for (size_t i = 0; i < numbers.length; ++i) 
        cout << numbers.v[i] << ",";

    cout << endl;

    return 0;
}

This is the error I get, there little red cross next to my line 33 code: numbers.v[numbers.length++] = x; and when i hover over the red cross, here's the error: picture of error message

Exception thrown: read access violation.
numbers.v was 0x111011101110111. occurred

By looking at my line of code, I'm just assigning int value "x" to the first element of array "numbers.v"

I tried looking for other solutions already, however I found that everyone else's problems were much more advance and this read access violation error seems due to different reason under different circumstance.

Jeremy
  • 379
  • 2
  • 11
  • Use a debugger to run your program one line at time, and observe the values of all variables; and the bug will then be very obvious. This is what a debugger is for. Knowing how to effectively use a debugger is a mandatory skill for every C++ developer. – Sam Varshavchik Sep 25 '19 at 02:24
  • @SamVarshavchik trust me I definitely did that, how ever in the very first loop numbers.v is showing value 0x111011101110111(???) which i have no idea what happens to it. appreciated if u can help – Jeremy Sep 25 '19 at 02:48
  • Strongly consider building the resize logic that is currently in `main` into a method of `myArray` where it can be protected and reused. Handy reading: [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) – user4581301 Sep 25 '19 at 03:01
  • Correct. You are forgetting the golden rule of computer programming: a computer will always do exactly what you tell it to do, instead of what you want it to do. You have never told your computer to create a new array for `v` to point to, or initialize it to anything, on the "very first loop", that's why your computer never did that, and `v` points to random garbage. Which part of this is unclear to you? – Sam Varshavchik Sep 25 '19 at 03:03

1 Answers1

0

When you do:

int *v;

You are creating a pointer to an int, but as of now it just points to undetermined memory. So trying to access even v[0] is undefined behavior and can throw a read access violation. initial_size is incorrect. v does not have any elements at the very start, so v.length will return 0 on the first iteration of the loop and the if statement will evaluate to false. Then you will try to access

 numbers.v[numbers.length++] = x;

Which is undefined behavior.

You should start v with one element, so that the starting size of v will be correct:

int* v = new int[1];

Also don't forget to release the memory at the end to avoid a memory leak.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • i definitely cannot start with initial_size = 0 as i cannot grow my array capacity by multiplying by 2: (0 * 2 = 0). @1201ProgramAlarm I dont quite get what u mean, I initialized "myArray numbers", so "numbers.v" should be initialized as a int* type which points to the memory address of the first element in the arrary, am i right or wrong? – Jeremy Sep 25 '19 at 02:42