-2

Can someone explain me why when i back form function i lost my data from tabOfOffsets. I did the same thing twice and program crash only with the second array. I printed values of this array on the end of function and everything is clear and correct. Maybe i make mistake somewhere with delete? Below it is the code.

#include<iostream>
#include <algorithm>

using std::cout;
using std::endl;

void changeSizeOfVector(int *tabValue, int *tabOffsets, int &oldSize, int 
newSize) {
int temp = std::min(oldSize, newSize);

int *newTabOfValues = new int [newSize] {0};
int *newTabOfOffsets = new int [newSize] {0};

for (int i = 0; i < temp; i++) {
    newTabOfValues[i] = tabValue[i];
    newTabOfOffsets[i] = tabOffsets[i];
}

delete[] tabValue;
delete[] tabOffsets;

tabValue = new int [newSize] {0};
tabOffsets = new int [newSize] {0};



for (int i = 0; i < newSize; i++) {
    tabValue[i] = newTabOfValues[i];
    tabOffsets[i] = newTabOfOffsets[i];
    std::cout << tabOffsets[i] << tabValue[i] << endl;
}

oldSize = newSize;
delete[] newTabOfValues;
delete[] newTabOfOffsets;
for (int i = 0; i < newSize; i++) {
    std::cout << tabOffsets[i] << tabValue[i] << endl;
}

}
int main() {

int SIZE = 10;

int * tabOfOffsets = new int[SIZE];
int * tabOfValues = new int[SIZE];

for (int i = 0; i < SIZE; i++)
{
    tabOfValues[i] = i;
    tabOfOffsets[i] = i;
    cout << tabOfValues[i] << " : " << tabOfOffsets[i] << endl;
}

changeSizeOfVector(tabOfValues, tabOfOffsets, SIZE, 12);


for (int i = 0; i < SIZE; i++) {
    cout << tabOfOffsets[i] << " : " << tabOfValues[i] << endl;

}
delete[] tabOfOffsets;
delete[] tabOfValues;

}

Sewer
  • 27
  • 1
  • 5
  • 3
    Get a couple of [good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and read about *references* and how to pass arguments *by reference*. – Some programmer dude Oct 17 '17 at 09:45
  • 1
    don't use pointers to arrays. That's C-style programing. Use the `std::vector` and pass by reference. – JHBonarius Oct 17 '17 at 09:47
  • 1
    After you learn about references, and know how to fix your program, then think about the *double copying* you make. Why not simply *assign* the pointers? Like `tabValue = newTabOfValues`? And once you considered and implemented and tested that, throw you program away and learn how to use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Oct 17 '17 at 09:48
  • Possible duplicate of [When I change a parameter inside a function, does it change for the caller, too?](https://stackoverflow.com/questions/1698660/when-i-change-a-parameter-inside-a-function-does-it-change-for-the-caller-too) – Tadeusz Kopec for Ukraine Oct 17 '17 at 10:38

1 Answers1

0

This function declaration is wrong:

void changeSizeOfVector(int *tabValue, int *tabOffsets, int &oldSize, int 
newSize);

it means you can change the values of tabOffsets but not the pointer itself in order to make it behave correctly you should declare it as follows:

void changeSizeOfVector(int *tabValue, int **tabOffsets, int &oldSize, int 
newSize);

This way you can change the pointer itself and assign a newly allocated array to it.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
  • That's indeed answering his question... But evading the point that he is programming C-style, while he could use STL and such. – JHBonarius Oct 17 '17 at 09:48
  • Thank you. You made my day ;). Btw i know that i can use STL but i had this example and i wanted to know why it wasn't work. – Sewer Oct 17 '17 at 10:44