Deallocating memory of an Array in a function

Jan 28, 2013 at 6:20pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>

using namespace std;


int* selectarray(int &pdoublesize);


int main()
{
	int* arraypointer;
	int doublesize = 0;


	arraypointer = selectarray(doublesize);
	
	for(int x = 0; x < doublesize; x++)
	{
		cout << arraypointer[x] << endl;	
    }
	
	
	
	cout << endl;
	system("pause");
	return 0;
}

int* selectarray(int &pdoublesize)
{
	int* thearray;
	int arraysize;
	int* newarray;	
	int* arraycontents;
	cout << "How many numbers do you want in the array?" << endl;
	cin >> arraysize;

	thearray = new int[arraysize];
	
	cout <<"Enter the "<< arraysize << " numbers."<< endl;
	
	for(int x = 0; x < arraysize; x++)
	{
		cin >> thearray[x];
	
    }
	
	const int twicesize = arraysize * 2;
	newarray = new int [twicesize];

	newarray = thearray;
	pdoublesize = twicesize;
	
	cout << endl;

	for(int x = arraysize; x < twicesize; x++)
	{
		newarray[x] = 0;
    }
	
	thearray = 0;
	delete[] thearray;
    
	
	arraycontents = newarray;
	
	
	
	delete[] thearray;
        thearray = 0;
	
	
	

	return arraycontents;
}

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.
Last edited on Jan 28, 2013 at 7:37pm
Jan 28, 2013 at 7:01pm
1
2
thearray = 0;
delete[] thearray;

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)
Jan 28, 2013 at 7:03pm
1
2
newarray = new int [twicesize];
newarray = thearray;


> deallocate memory at this address (0), which leads to segfault.
delete NULL does nothing
Last edited on Jan 28, 2013 at 7:06pm
Jan 28, 2013 at 7:30pm
1
2
3
	newarray = new int [twicesize];  // new memory allocated.

	newarray = thearray; // newly allocated memory leaked. 


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.

All memory allocated in this function is leaked.


Jan 28, 2013 at 7:41pm
Thank you all for the replies.
@ne555 and cire

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.
Last edited on Jan 28, 2013 at 7:43pm
Jan 28, 2013 at 7:52pm
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.

What are you trying to do?
Jan 28, 2013 at 7:58pm
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.

Last edited on Jan 28, 2013 at 7:58pm
Jan 28, 2013 at 8:20pm
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.
}



Jan 29, 2013 at 5:11am
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.
Topic archived. No new replies allowed.