Visual Studio pointer error - c++

When I run this code:
#include <iostream>
using namespace std;
void makeArrayBigger(int*, int&, int);
void arrayIni(int*, int, int);
void displayArray(int*, int);
int main(){
int size = 5;
int *arr = new int[5];
arrayIni(arr, size, 0);
//displayArray(arr, size);
makeArrayBigger(arr, size, 8);
//displayArray(arr, size);
arrayIni(arr, size, 1);
system("pause");
return 0;
}
void makeArrayBigger(int *arr, int &size, int newSize){
int *newArr = new int[newSize];
for (int counter = 0; counter < size; counter++){
newArr[counter] = arr[counter];
}
delete[] arr;
arr = new int[newSize];
for (int index = 0; index < size; index++){
arr[index] = newArr[index];
}
delete[] newArr;
size = newSize;
}
void arrayIni(int *arr, int size, int ini){
for (int index = 0; index < size; index++){
arr[index] = ini;
}
}
void displayArray(int *arr, int size){
if(arr == NULL)
return;
for (int counter = 0; counter < size; counter++){
cout << arr[counter] << endl;
}
cout << endl;
}
I get this breakpoint error:
Windows has triggered a breakpoint in pointer.exe.
This may be due to a corruption of the heap, which indicates a bug in pointer.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while pointer.exe has focus.

The reason of this error is that you confused the conception 'Pass by value' with 'Pass by Reference'.
The answer of the following question might be an good reference:
What's the difference between passing by reference vs. passing by value?
The problem will be fixed by replacing the void makeArrayBigger() interface with the following codes
void makeArrayBigger(int* &arr, int &size, int newSize);
Update:
when I pass a pointer I'm passing a pointer that points to that same location.
It is the same location but not the same pointer. Whether delete or malloc the new pointer will not change the value of the actual parameter.
In this case, the arr pointer keeps the same size after makeArrayBigger(). But it will be assumed to be the new size(8) in arrInit().

Pass in a reference to the array (int*) just like you pass a reference to the array size (int). Then you can replace the array with a new array and update the size as you are currently doing:
void resize (int*& array, int& arraySize, int newSize)
{
delete [] array;
arraySize = newSize;
array = new int [arraySize];
}
int main() {
int testSize = 5;
int* testArray = new int [testSize];
// Do stuff
resize (testArray, testSize, 8);
// Do more stuff
delete [] testArray;
testArray = NULL;
testSize = 0;
return 0;
}

Related

Array resizing. Why cannot I resize the array second time? _CrtIsValidHeapPointer(PUserData)

I want to resize the array multiple times.It does that the first time but after that, it throws me an error. When I do it the second time, I get an error _CrtIsValidHeapPointer(PUserData). Can someone help me out?
int main()
{
int size = 8;
int *arr = new int[size];
arr[1] = 25;
arr[2] = 30;
int count = 0; //to check size
for (int i = 0; i < size; i++)
{
count = count + 1;
}
cout << count << endl;
resize(arr, size);
int new_count = 0; //to confirm size change
for (int i = 0; i < size; i++)
{
new_count = new_count + 1;
}
cout << new_count << endl;
resize(arr, size);
int new_count2 = 0; //to confirm size change
for (int i = 0; i < size; i++)
{
new_count2 = new_count2 + 1;
}
cout << new_count2 << endl;
return 0;
}
void resize(int *a,int &size)
{
int newsize = 2 * size;
int *arr_new = new int[newsize];
for (int i = 0; i < size; i++) //copy everything
{
arr_new[i] = a[i];
}
size = newsize; //new value of size
delete [] a;
a = arr_new; //Pointer pointing to new array
delete arr_new;
}
There are two problems with this code:
void resize(int *a,int &size)
{
[...]
delete [] a;
a = arr_new; //Pointer pointing to new array
delete arr_new; // huh????
}
The first problem is that you are calling the delete operator twice; the first call deletes the old array (which makes sense), but then you attempt to delete the newly allocated array also (via delete arr_new). How is the caller going to able to use the newly allocated array when resize() has already deleted it before it returned?
The second problem is that you set a to point to the new array (i.e. a = arr_new;) but a is a local function-argument that goes out of scope when resize() returns, so the calling code will never see its new value. I think you want this instead:
void resize(int * & a,int &size) // note the & before a!
Passing a by reference will allow the caller to see a's new value after resize() returns.

Extract pair numbers from array

Good evening, folks.
I'm currently experiencing difficulties with extracting pair numbers from an array. I have the following code:
#include <iostream>
using namespace std;
int *paire(int *d, int length) {
int counter = 0;
int position = 0;
for (int i=0; i<length; i++) {
if (d[i] % 2 ==0)
counter++;
}
int *k = new int[counter];
for (int i=0; i<length; i++) {
if (d[i] % 2 ==0) {
k[position] = d[i];
position++;
}
}
return k;
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int *array1 = paire(b,8);
for (int i=0; i<5; i++) { // how can I point here to the counter in paire() ?
cout<<array1[i];
}
delete[] array1;
return 0;
}
So I think I've got it right with initializing the new array in function paire, but I'm having difficulties to iterate through the array.
P.S. I'm first year in university, so I would really be thankful if you can keep the same simplicity in the answers. Thanks in advance!
It appears that you need to return 2 separate values: the number of even numbers in the array b, and the address of the newly allocated memory that is storing exclusively those even numbers.
Since you can not return multiple variables, one solution that does minimal modification to your code would be as follows.
int *paire(int *d, int length, int& counter) {
counter = 0;
// rest of your function remains unchanged
// ...
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int evenNumbers;
int *array1 = paire(b,8, evenNumbers);
for (int i=0; i<evenNumbers; i++) {
cout<<array1[i];
}
delete [] array1;
return 0;
}
Alternatively, you can return the value in counter and send the reference to the int* variable as an argument to paire function. Or, you can declare paire to have return type void and use references to pass back both the values.
You can further simplify your function by allocating to that of the length and returning the counter by an output parameter.
#include <iostream>
using namespace std;
int *paire(int *d, int length, int &counter) {
counter = 0;
int *k = new int[length]; // allocate for the maximum memory
for (int i = 0; i < length; ++i) {
if (d[i] % 2 == 0) {
k[counter++] = d[i];
}
}
return k;
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int counter = 0;
int *array1 = paire(b,8, counter);
for (int i=0; i<counter; i++) { // how can I point here to the counter in paire() ?
cout<<array1[i] << " ";
}
delete [] array1;
return 0;
}
But please note that as others have already pointed out this method is quite error prone in the sense that it leaves the responsibility to the client to delete the internal memory used by paire function.

Reversing an array using pointers

I have this program that makes and populates an array. Then it is sent to a function called reverse, which reverses the order in the array. The compiler keeps giving errors. I'm not quite sure why.
CODE
void reverse(int* array, int size) {
for (int i = 0; i < size/2; i++) {
int temp = array[i];
array[i] = array[size-i];
array[size-i] = temp;
} // end of for loop
} // end of reverse
int main( int argc, char** argv ) {
int array[8];
// get and print size of the array
int size = sizeof(array) / sizeof(array[0]);
printf("Size is %d\n", size);
// populate array
for (int i = 0; i < size; i++) {
array[i] = i;
} // end of for loop
// display array before reversing
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
} // end of for loop
// new line
printf("\n");
// reverse the array
reverse(&array, size);
// display the array again after reversing
for (int i = 0;i < size; i++) {
printf("%d ", array[i]);
} // end of for loop
} // end of main
It keeps giving me this error
main.cc:17:14: error: indirection requires pointer operand ('int' invalid)
int temp = *array[i];
^~~~~~~~~
main.cc:18:3: error: indirection requires pointer operand ('int' invalid)
*array[i] = *array[size-i];
^~~~~~~~~
main.cc:18:15: error: indirection requires pointer operand ('int' invalid)
*array[i] = *array[size-i];
^~~~~~~~~~~~~~
main.cc:19:3: error: indirection requires pointer operand ('int' invalid)
*array[size-i] = temp;
^~~~~~~~~~~~~~
4 errors generated.
make: *** [main.o] Error 1
I did solve this problem a little differently, maybe you will use this code:
#include <iostream>
void displayArray(int table[], int size);
void rev(int table[], int size);
void fillTheArray(int table[], int size);
int main(int argc, char** argv) {
int myArray[8];
int size = sizeof(myArray) / sizeof(myArray[0]);
std::cout << "Array size is: " << size << std::endl;
fillTheArray(myArray, size);
displayArray(myArray, size);
std::cout << std::endl;
rev(myArray, size);
displayArray(myArray, size);
std::cin.get();
return 0;
}
void fillTheArray(int table[], int size) {
for (int i = 0; i < size; i++) {
table[i] = i;
}
}
void displayArray(int table[], int size) {
for (int i = 0; i < size; i++) {
std::cout << table[i] << " ";
}
std::cout << std::endl;
}
void rev(int table[], int size) {
int *start = table;
int *end = table + (size - 1);
for (int i = 0; i < size; i++) {
if (start < end) {
int temp = *end;
*end = *start;
*start = temp;
}
start++;
end--;
}
}
I can see two errors in this code. First is: wrong way of passing parametr to function:
// reverse the array
reverse(&array, size);
you should do this just like this(array name is pointer to first element of this array):
reverse(array, size);
Second problem is with reverser - you try to access some random memory outside arrar range:
array[i] = array[size-i];
Remember that in C++ array index's start with 0 not 1. So if your array is size of 8 - largest insext of this array is 7 (0, 1, 2, 3, 4, 5, 6, 7). Your code should look like this:
array[i] = array[size -i -1];
And it should work as you expected.
It is my solution with pointers:
void reverse(int arr[], int count)
{
int* head = arr;
int* tail = arr + count - 1;
for (int i = 0; i < count/2; ++i)
{
if (head < tail)
{
int tmp = *tail;
*tail = *head;
*head = tmp;
head++; tail--;
}
}
for (int i = 0; i < count; ++i)
{
std::cout << arr[i] << " ";
}
}
or just use functions build in C++: std::reverse in 'algorithm' library.
It's a lot of examples on stackoverflow with this kind of examples:
Reverse Contents in Array
You have fixed most of the compiler errors in your code except one.
The line
reverse(&array, size);
should be
reverse(array, size);
After that is fixed, you have to fix logic errors in reverse.
You are using the wrong index for accessing the upper half of the array.
void reverse(int* array, int size) {
for (int i = 0; i < size/2; i++) {
int temp = array[i];
array[i] = array[size-i]; // When i is 0, you are accessing array[size]
// That is incorrect.
array[size-i] = temp;
} // end of for loop
} // end
You need to use
void reverse(int* array, int size) {
for (int i = 0; i < size/2; i++) {
int temp = array[i];
array[i] = array[size-i-1];
array[size-i-1] = temp;
}
}
Another way to approach the algorithm would be to use two indices.
void reverse(int* array, int size) {
for (int i = 0, j = size-1; i < j; ++i, --j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
Working program: http://ideone.com/ReVnGR.
You're passing **int instead of *int to the reverse method:
reverse(&array, size);
Pass it like that:
reverse(array, size);

Getting out of scope error with dynamically allocated array

I am getting an out of scope error in my copy function, when I try to copy the contents of the first array into the DMA one. The try-catch block is required.
#include <iostream>
#include <cstdlib>
using namespace std;
void show( const int a[], unsigned elements );
int * copy( const int a[], unsigned els );
void die(const string & msg);
int main()
{
int arr[4] = {4, 2, 3, 6};
show(arr, 4);
int * newArr = copy(arr, 4);
}
void show( const int a[], unsigned elements )
{
for (int i = 0; i < elements; i++)
cout << a[i] << endl;
}
int * copy( const int a[], unsigned els )
{
try
{
int * newArr = new int[els];
}
catch(const bad_alloc &)
{
die("Alloc Failure");
}
for (int i = 0; i < els; i++)
newArr[i] = a[i];
return newArr;
}
void die(const string & msg)
{
cerr << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
If you declare the variable inside the try block then it's only accessible there. You can work around this by moving the declaration outside of the block.
int *newArr;
try
{
newArr = new int[els];
}
catch(const bad_alloc &)
{
die("Alloc Failure");
}
for (int i = 0; i < els; i++)
newArr[i] = a[i];
Or by moving the rest of the code inside the try.
try
{
int *newArr = new int[els];
for (int i = 0; i < els; i++)
newArr[i] = a[i];
return newArr;
}
catch(const bad_alloc &)
{
die("Alloc Failure");
}
Define new array before the try, otherwise it is only defined inside the try block.
The whole point of exceptions is that you do not need to handle all conceivable errors right at the point where they happen, but rather in places of your choice where you are able to respond to them meaningfully. So be generous with the size of your try blocks:
int * copy(const int a[], unsigned els)
{
try
{
int * newArr = new int[els];
for (int i = 0; i < els; i++)
newArr[i] = a[i];
return newArr;
}
catch (const std::bad_alloc &)
{
die("Alloc Failure");
}
}
You can change your code to
int * copy( const int a[], unsigned els ) {
int * newArr = nullptr;
try {
newArr = new int[els];
}
catch(const bad_alloc &) {
die("Alloc Failure");
}
if(newArr) {
for (int i = 0; i < els; i++)
newArr[i] = a[i];
}
return newArr;
}
to overcome your problem. Just initialize newArr correctly.
I think the problem in your Copy-Function is, that the new Array is a local Variable. When you than return it to the caller, the pointer adresses aren't the right ones.
You might better use two arrays which you pass as Referneces into the copy-function, like this:
int& copy(int& newArray, const int& oldArray) { ... }
Here you get a discription about references in c++ if you don't know what they supposed to be: http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29

Changing around arrays

I'm doing a lot of different things with manipulating arrays without vectors, I was wondering if any one could help me with shifting elements in an array and expanding an array while initializing the new space with elements. I feel like I'm very close to completing this code, but I'm hitting a block.
#include <iostream>
using namespace std;
// Function prototypes
int *reverse(int *, int);
int *expand(int *, int);
int *shift(int *, int);
void display(int[], int);
void display2(int[], int);
void display3(int[], int);
int main()
{
int const SIZE = 5;
int myArray [SIZE] = {1, 2, 3, 4, 5};
int myArray2 [SIZE] = {1, 2, 3, 4, 5};
int myArray3 [SIZE] = {1, 2, 3, 4, 5};
int *arraPtr;
int *arraPtr2;
int *arraPtr3;
arraPtr = reverse(myArray, SIZE);
display(myArray, SIZE);
arraPtr2 = expand(myArray2, SIZE);
display2(myArray2, SIZE);
arraPtr3 = shift(myArray3, SIZE);
display3(myArray3, SIZE);
delete [] arraPtr;
delete [] arraPtr2;
delete [] arraPtr3;
return 0;
}
int *reverse(int *arr, int size)
{
int *copyArray;
int posChange;
if( size < 0)
return NULL;
copyArray = new int[size];
for (int index = 0; index < --size; index++)
{
posChange = arr[index];
arr[index] = arr[size];
arr[size] = posChange;
}
return copyArray;
}
int *expand(int *arr, int size)
{
int *newArray;
newArray = new int[size * 2];
memcpy( newArray, arr, size * sizeof(int));
for (int index = size; index < (size*2); index++)
newArray[index] = 0;
return newArray;
}
int *shift(int *arr, int size)
{
int *newArray;
newArray = arr;
newArray = new int [size + 1];
for (int index = 5; index > 0; index--)
newArray[index] = newArray[index - 1];
return newArray;
}
void display(int arr[], int size)
{
for (int index = 0; index < size; index++)
{
cout << arr[index] << " ";
}
cout << endl;
}
void display2(int arr[], int size)
{
for (int index = 0; index < size; index++)
{
cout << arr[index] << " ";
}
cout << endl;
}
void display3(int arr[], int size)
{
for (int index = 0; index < size; index++)
{
cout <<arr[index] << " ";
}
cout << endl;
}
There are only two compile error: int newArray; should be int* newArray; and #include <cstring> is missing (necessary for memcpy())
Also, the line display(myArray, SIZE); was probably meant to be display(arraPtr, SIZE); and likewise display2(myArray2, SIZE); -- otherwise you're only displaying the original arrays, not the results of your function calls.
However, this could benefit from the safer and more generic C++ algorithms, std::copy() and std::reverse_copy() at least:
int *reverse(int *arr, int size)
{
int *copyArray = new int[size];
std::reverse_copy(arr, arr+size, copyArray);
return copyArray;
}
int *expand(int *arr, int size)
{
int *newArray = new int[size * 2]();
std::copy(arr, arr+size, newArray);
return newArray;
}
int *shift(int *arr, int size)
{
int* newArray = new int [size + 1]();
std::copy(arr, arr+size, newArray+1);
return newArray;
}
full program: https://ideone.com/RNFiV
This is mostly C code but I'll try to give you some tips on the methods of what you're doing more than the syntax:
In your reverse function, you never actually put anything into the new array. Instead of doing some swapping in the for loop, you can just run through the original loop backwards putting the elements into the new array.
In the expand function it looks like you're trying to do two opposite things, copy the memory from the input array to the new one and then overwrite the new array with all zeros. If you want to copy the memory manually you need to have the loop only go the original array copying its values into the new array (and not go through double the size of the original array or else you'll walk off the end of it!). If you want to use memcpy then get rid of the for loop.
I'm not sure what you want the shift function to do but it pretty much just copies the array now.
I don't know exactly what did you want to accomplish but I think it was something like this:
#include <iostream>
#include <cstring> // Needed to compile on most compilers(memcpy), dunno in yours
using namespace std;
// Function prototypes
int *reverse(int *, int);
int *expand(int *, int);
int *shift(int *, int);
void display(int[], int);
void display2(int[], int);
int main()
{
int const SIZE = 5;
int myArray [SIZE] = {1, 2, 3, 4, 5};
int myArray2 [SIZE] = {1, 2, 3, 4, 5};
int myArray3 [SIZE] = {1, 2, 3, 4, 5};
int *arraPtr;
int *arraPtr2;
arraPtr = reverse(myArray, SIZE);
display(arraPtr, SIZE);
arraPtr2 = expand(myArray2, SIZE);
display2(arraPtr2, SIZE * 2);
delete [] arraPtr;
delete [] arraPtr2;
return 0;
}
int *reverse(int *arr, int size)
{
int *copyArray;
int posChange;
if( size < 0)
return NULL;
copyArray = new int[size];
for (int index = 0; index <= --size; index++)
{
posChange = arr[index];
copyArray[index] = arr[size];
copyArray[size] = posChange;
}
return copyArray;
}
int *expand(int *arr, int size)
{
int *newArray;
newArray = new int[size * 2];
memcpy( newArray, arr, size * sizeof(int));
for (int index = size; index < (size*2); index++)
newArray[index] = 0;
return newArray;
}
int *shift(int *arr, int size)
{
int *newArray;
newArray = new int [size + 1];
memcpy( newArray, arr, size * sizeof(int));
return newArray;
}
void display(int arr[], int size)
{
for (int index = 0; index < size; index++)
{
cout << endl << arr[index] << " ";
}
}
void display2(int arr[], int size)
{
for (int index = 0; index < size; index++)
{
cout << arr[index] << " ";
}
}
As a side note, if you have problems with this kind of stuff you should take a look at any good C resource that talks about pointers and pointer arithmetics, it will come in handy when you have to do low level C++ code.