Hey all, I needed some aid with figuring out the program I have written to practice using pointers with arrays and allocating and De-allocating memory.
In this program, everything seems to run smoothly, and when I use small numbers for my new array size, I don't always get an error at the end. When I use larger numbers such as a number greater than 7, I usually do get errors at the end.
The error comes at the end of running the program. Recommendations and corrections would be greatly appreciated. Okay, so the error comes in when the first array size is designated to number 7. When the size is 7 or greater, I get the error at the end. If it's below then there is no issue.
The objective of this program is this:
Write a function that accepts a dynamic array of integers and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the new array with 0. After the new array is created and initialized, the function should deallocate the memory used by the argument array. Finally the function should return a pointer to the new array.
You make thearray points to 0 and then trying to deallocate memory at this address (0), which leads to segfault.
Swap those two lines. (lines 62 and 63; 69 and 70)
newarray points to where thearray points now, which is to say it points at a dynamically allocated area with space for arraysize elements. However, you're trying to assign twicesize elements.
Thank you for pointing that out, I have fixed that however I still get an error and this time the error occurs earlier while running the program..
@cire
Appreciate the feedback. So what I am transferring in that is not only the contents but the address as well. Any tips on how I should go about fixing that leak. I am suppose to transfer array elements, and I do not know how to go about it in any other way that does not deal with using the pointers.
Note: Without the delete command towards the end of the function, I still acquire the error.
Appreciate the feedback. So what I am transferring in that is not only the contents but the address as well.
I am not understanding what you're trying to accomplish. selectArray isn't exactly descriptive. In the function, you allocate memory for an array and populate that array with input from stdin, then allocate another array that is twice the size and initialize it's elements to 0. Discard the contents of the first array. Return the second.
This is simply a practice assigned to me by a professor. This is practice to learn how to manipulate dynamic arrays.
Let me re-post the objective of the assignment(It has no other purpose than simply practice):
"Write a function that accepts a dynamic array of integers and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the new array with 0. After the new array is created and initialized, the function should deallocate the memory used by the argument array. Finally the function should return a pointer to the new array."
------------------------------------------------------------------------------------------------------------------
"I am not understanding what you're trying to accomplish. selectArray isn't exactly descriptive. In the function, you allocate memory for an array and populate that array with input from stdin, then allocate another array that is twice the size and initialize it's elements to 0. Discard the contents of the first array. Return the second.
What are you trying to do?"
That's pretty much all I am trying to do but I also need to delete the first array in the process so that I can free up memory space while keeping the values in the second array and staying with that.
That's pretty much all I am trying to do but I also need to delete the first array in the process so that I can free up memory space while keeping the values in the second array and staying with that.
You don't have the first array as indicated by the instructions.
Write a function that accepts a dynamic array of integers and the arrays' size as arguments. ... the function should return a pointer to the new array.
Picking a suitable name: int* reallocate( int* array, unsigned& capacity ); is the prototype/declaration you're looking for.
1 2 3 4 5 6 7 8 9 10
int* reallocate(int* array, unsigned& capacity)
{
Calculate the capacity for the new array.
allocate the memory for the new array.
copy all elements in the old array to the new array
set unused elements in the new array to 0.
free the old array
set the capacity.
return the new array.
}
Sorry for the late response, but thank you for the help. Got it. It would have been wiser for me to have posted the program with comments to make it faster for people to break it apart.