Answer the question
In order to leave comments, you need to log in
munmap_chunk() error: invalid pointer Process finished with exit code -6. What to do?
I wrote a code that copies the values of one dynamic array to another. The code compiled correctly, but gave some kind of error. What to do?
#include <iostream>
using namespace std;
int FillArray(int* const arr, const int size){
for (int i = 0; i < size; i++)
{
arr [i] = rand()%10;
}
}
int ShowArray(const int* const arr, const int size){
for(int i = 0; i < size; i++)
{
cout<< arr[i] << "\t";
}
cout << endl;
}
int main()
{
int size = 10;
int *firstarray = new int(size);
int *secondarray = new int(size);
FillArray(firstarray, size);
FillArray(secondarray, size);
cout<< "firstarray: \t";
ShowArray(firstarray, size);
cout<< "secondarray:\t";
ShowArray(secondarray, size);
cout<<"==============================="<<endl;
delete[] firstarray;
firstarray = new int[size];
for(int i=0;i<size;i++)
{
firstarray[i]=secondarray[i];
}
cout<< "firstarray: \t";
ShowArray(firstarray, size);
cout<< "secondarray:\t";
ShowArray(secondarray, size);
delete[] firstarray;
delete[] secondarray;
return 0;
}
Answer the question
In order to leave comments, you need to log in
int *firstarray = new int(size);
So you create a pointer not to an array, but to an int with a value of size. Most likely you wanted to do this:
int *firstarray = new int[size];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question