SavantStrike
October 2nd, 2010, 03:41 PM
So I'm currently stumped on a programming assignment that I just cannot seem to make any headway on, and things are getting desperate as it's due soon and there are other problems on the assignment which are more complicated (and build on this one).
The prof wants me to create an int[] array in memory, referenced by a pointer, and controlled by an integer variable "size". I've spent hours trying to get this to work, and it just doesn't want to work.
The problem is I need to be able to add to this array. Either I need to make it larger than it should be so I can add to it, or I need to do a deep copy to copy it to another array, deallocate it, and then create it again, adding the data back in plus an extra integer.
The two offending functions are as follows
void List::printall(){
int counter = 0;
while (counter <= size){
cout << (ptr + counter) << " ";
counter++;
}
cout << endl;
}
void List::add(int x){
if (ptr == NULL){
ptr = new int[size];
ptr[size] = x;
}
else{
int *tmpPointer = new int[size];
//deep copy values in pointer to tmpPointer
for(int i=0; i<size; i++){
*(tmpPointer + i) = *(ptr + i);
}
delete [] ptr;
size++;
//reallocate pointer to point to a larger array
ptr = new int[size];
for(int i=0; i<size; i++){
ptr[i] = tmpPointer[i];
}
ptr[size] = x;
cout << "pointer array from inside add function: ";
for(int i=0; i<=size; i++){
cout << *(ptr + i) << " ";
}
cout << endl;
}
}
It runs like this, but not right. The integer's are added to the array, and printing the pointers without the * dereference operator shows that there are memory addresses being pointed to. However, when I try to use * in the printall function to print human readable text, I get a segmentation fault.
Any suggestions? I would be the first suggestion would be not to use pointers to an array in memory, but it's required ](*,).
Thanks in advance!
The prof wants me to create an int[] array in memory, referenced by a pointer, and controlled by an integer variable "size". I've spent hours trying to get this to work, and it just doesn't want to work.
The problem is I need to be able to add to this array. Either I need to make it larger than it should be so I can add to it, or I need to do a deep copy to copy it to another array, deallocate it, and then create it again, adding the data back in plus an extra integer.
The two offending functions are as follows
void List::printall(){
int counter = 0;
while (counter <= size){
cout << (ptr + counter) << " ";
counter++;
}
cout << endl;
}
void List::add(int x){
if (ptr == NULL){
ptr = new int[size];
ptr[size] = x;
}
else{
int *tmpPointer = new int[size];
//deep copy values in pointer to tmpPointer
for(int i=0; i<size; i++){
*(tmpPointer + i) = *(ptr + i);
}
delete [] ptr;
size++;
//reallocate pointer to point to a larger array
ptr = new int[size];
for(int i=0; i<size; i++){
ptr[i] = tmpPointer[i];
}
ptr[size] = x;
cout << "pointer array from inside add function: ";
for(int i=0; i<=size; i++){
cout << *(ptr + i) << " ";
}
cout << endl;
}
}
It runs like this, but not right. The integer's are added to the array, and printing the pointers without the * dereference operator shows that there are memory addresses being pointed to. However, when I try to use * in the printall function to print human readable text, I get a segmentation fault.
Any suggestions? I would be the first suggestion would be not to use pointers to an array in memory, but it's required ](*,).
Thanks in advance!