Issues with Dynamic Allocation of Arrays

I am having trouble growing the allocated memory of an array using pointers. There is a Grow() function in my program that is meant to double the capacity of the array. A requirement for this program is the use of three global variables to represent the array. Does anyone have any pointers on how to make this work? My current code is the following:

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
#include <iostream>    
#include <string> 
using namespace std;

//global variables
int size=0;
int capacity=0;
double* v=NULL;
double vect[0];
//vector which may move to somewhere else


//initialize function
void Initialize(){
	//initialize three global variables
	size=0;
	capacity=2;
	v=vect;
}

//Finalize function
void Finalize(){
	//clears memory associated with vector
	delete[] v;
	v=NULL;
}

//doubles size of vector
void Grow(){
	//capacity of v
	int cap=capacity;
	capacity=cap*2;
	int el;
	//create new array
	double* nv=new double[capacity];
	//copy elements of v
	for(int i=0;i<cap;i++){
		el=v[i];
		nv[i]=el;
	}
	//free allocated memory of old vector
	delete[] v;
	v=nv;
	cout<<cap<<" "<<capacity;
}
int main(){
	Initialize();
	Grow();
	Finalize();
return 0;
}
1
2
3
4
5
6
7
8
	int el;
	//create new array
	double* nv=new double[capacity];
	//copy elements of v
	for(int i=0;i<cap;i++){
		el=v[i];
		nv[i]=el;
	}


Why are you coping a double (v[i]) into an int (el) and then back into a double (nv[i])?
Last edited on
you can NOT resize C style arrays.

pointer memory blocks that act like arrays can be resized sort of. you can use C's memory function realloc or you can make new block, copy old block into it, delete old block to resize it.

double vect[0]; //you also should not make a zero sized array.

You may not delete memory you do not own.

this is an error:
int x[10];
int * barf = x;
delete[] barf; //error, this is not under your control to delete it.
x = new int[20]; //error, you cannot do this. x isnt really a pointer, but it can ACT like one in some ways ... in almost every way that does not involve NEW or DELETE actually. Trying to fool the compiler with an intermediate value may compile, but you will never produce code that does what you want it to do here, even if it compiles.

compilers may accept, even compile and run some of these kinds of mistakes, but that does not make them have any value. They may even work, at least some times, esp in debug compiled code. This is not reliable. They are errors and you must not do these things.

- you may only delete memory that you created with new. (if returned from a library expecting you to delete it, for the purpose of this rule, you created that memory by calling the library function).
- you may only free memory that you created with a malloc or related command.
- you may not mix new and free or malloc and delete. they do not work together.
- you may not allocate 0 sized memory blocks.

pointers are full of places where you need to know all these details and more or you will make mistakes that are extremely hard to find and fix. This is why we recommend using containers like vector instead of trying to do it yourself. Its a good thing to learn, but not used a lot in modern c++ coding.
Last edited on
In addition to above issues, finalize() has to reset size and capacity as the allocated memory is freed. Also note that if a pointer is nullptr then using delete/delete [] is OK. Global variables are default initialised (same as static ones) - so initially size, capacity are 0 and v is nullptr. You can't have an initial capacity of 2 without having allocated memory. Given using global variables, initialize() and finalize() currently perform the same function (although this may change depending upon requirements).

Consider:

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
#include <iostream>
using namespace std;

//global variables
int noelems;
int capacity;
double *v;

//initialize function
void Initialize() {
	//initialize three global variables
	noelems = 0;
	capacity = 0;
	delete [] v;
	v = nullptr;
}

//Finalize function
void Finalize() {
	//clears memory associated with vector
	Initialize();
}

//doubles size of vector
void Grow() {
	//capacity of v
	const int oldcap = capacity;

	capacity = oldcap == 0 ? 1 : capacity * 2;

	//create new array
	double *nv = new double[capacity];

	//copy elements of v
	for (int i = 0; i < oldcap; ++i) {
		nv[i] = v[i];
	}

	//free allocated memory of old vector
	delete[] v;
	v = nv;
	cout << oldcap << " " << capacity;
}

int main() {
	Initialize();
	Grow();
	Finalize();
	return 0;
}


Also note that having a variable called size when also having using namespace std is not correct as namespace std also has size.
Topic archived. No new replies allowed.