Creating an array at runtime

Is it possible to create a pointer that can be modified into an array at runtime?

Or instead do you just have to create a normal array.<also, can normal arrays accept new objects at runtime?>

Or is it only an array of pointers which can be modified?
You can create dynamic arrays, but vectors and strings are much easier to use.
my assigned task was to create a vector
vector<int> someVectorObject;

How's that?
i was asked not to use the vector class, but learn how to define my own one instead
How has the term "vector" been defined in the question? For your purposes, what is a vector?
its creating a dynamically re-sizable array. Im doing this in the form of a fuction
@ Moschops: Best answer of the day!

@ OP: An array can be resized in runtime but it generally isn't a good idea. You could try crazy things like creating the index of the array as a variable, and redifining the array's size by declaring it in a function as a static variable. That's a great way to cause a Seg Fault by the way.

More likly though this is to practice using malloc(...) which can be found here: http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/

Last edited on
this is my task of creating a re sizable array. My issue is that its currently buggy and doesn't work.

Apparently, I haven't declared my object correctly
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
#include<iostream>






template <typename xdata>
class VectorDouble
{
public:
	VectorDouble()
	{
		for(int i = 0;i < 50; i++)
		{
			dptr [i] = new double 0.0;
		}
	}
	VectorDouble(xdata xpass)
	{
		dptr[] = new xpass;
	}

	void Add_New(xdata xpass)
	{
		dptr[] = new xpass;

	}
	~VectorDouble()
	{
		delete [];
	}

private:
	

	xdata * dptr[1];
};

int main()
{
	 VectorDouble <double> ccxcx;

	 ccxcx.Add_New(898.9);
	 ccxcx.~VectorDouble();
	return 0;
}
Change that '50' in your VectorDouble constructor to a variable that you can pass to it, add a copy constructor and you're on the right track.
closed account (zb0S216C)
delete [];

This line doesn't look complete.
its shows how tired I was at that point
the code still doesn't work, it seems that I still have big errors in it

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
#include<iostream>




template <class T, unsigned N>
void Print(T (&xArray)[N])
{
for (unsigned i=0; i<N; i++)
cout << xArray[i] << ' ';
cout << endl;
}





template <typename xdata>
class VectorDouble
{
public:
	VectorDouble()
	{
		for(int i = 0;i < 50; i++)
		{
			*dptr [i] = (xdata) 0;
		}
	}
	VectorDouble(xdata xpass)
	{
		dptr[] = new xpass;
	}

	void Add_New(xdata xpass)
	{
		dptr[] = new xpass;

	}
	~VectorDouble()
	{
		delete dptr[];
	}

private:
	

	xdata * dptr[1];
};

int main()
{
	 VectorDouble <double> ccxcx;

	 ccxcx.Add_New(898.9);
	 //ccxcx.~VectorDouble();
	return 0;
}
I see that dptr is an array of double pointers, of size 1, but on construction you seem to be filling in fifty elements. If you want to fill in fifty elements of an array, you need an array of at least size 50.

dptr is an array of double pointers. This means that it is to be filled with double pointers. This *dptr [i] = (xdata) 0; is an attempt to put an xdata object (i.e. a double) into the place pointed to by one of those double pointers. That's all well and good, but you never initalised those double pointers so they will be pointing randomly all over your memory space. This should give you an idea of what to try instead: dptr [i] =new (xdata) 0;
In this way, you are creating a new object of type xdata, and getting the pointer to it (new returns a pointer) and then you are putting that pointer into your array; this is fine, because you created an array of pointers to this type, so what you're putting into the array matches the type of object it is expecting to be an array of.

However, it doesn't make that much sense as a way to create your initial array. Why not make an array of the objects you want to store, rather than an array of pointers to them? How about something like this instead:

1
2
3
4
5
dprt = new double[50]; // Create an array of 50 doubles on the heap
for(int i = 0;i < 50; i++)
		{
			dptr [i] = 0.0; // Set them all to 0.0, just because I like to initialise things.
		}


where your dptr object would just be:

1
2
private:
	xdata* dptr;





I note that in your Add_New function, you are trying to create a new object of type xpass. Is xpass, for example, the value 898.9? If so, you can't create an object of type 898.9 as no such type exists.

Also, in that function, you seem to be taking your pointer named dptr, and trying to make it point at the single newly created object. This seems wrong; if you had it pointing to an array before, you've now pointed it somewhere else entirely and can never recover that previous array.

I think you should take a step back, forget about the code, and just think about what you actually want to do. For example, your Add_New functions might look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Add_New(xdata xpass)
	{
		// Check that there is enough space to add another object to the array, by comparing
                //   the size of the array with the number of objects I have already put in it. If it's not
                //   big enough, make it bigger.

                // Now, make the next empty slot equal to the value that was passed in.

                // Increment the count I am keeping of how many objects are in the array
                // Increment the pointer I am keeping that points at the next empty slot. NB I could
                //   move the code to check the size of the array and make it bigger to here.

                // return some value to indicate that the addition of the value succeeded or failed.

	}


Last edited on
Topic archived. No new replies allowed.