Dynamic declaration concern

Hi, is this an unsafe way of being able to make a variable number of variables? I don't want to have to declare everything beforehand since there would be many variables and I'd like to be able to create and delete them based on user actions, but is it unsafe to do what I do below, using the same name for each new member and all? The test program I wrote appears to work though. Is there a better way of doing this?
Thanks

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
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;

int main()
{
    int input;
    std::vector<int*> list1;
    while(1)
    {
        cin>>input;
        if(input==1)
        {
            int *temp = new int;
            *temp=list1.size();
            list1.push_back(temp);
        }
        else if(input==0 && list1.size()>0)
        {
            delete list1[(list1.size())-1];
            list1.pop_back();
        }
        else if(input==0 && list1.size()<1)
        {
            cout<<"Error - No elements to delete."<<endl;
        }
        else
        {
            cout<< "invalid input"<<endl;
            continue;
        }

        for(int i=0;i<list1.size();i++)
        {
            cout<<*list1[i]<<' ';
        }
        cout<<endl;
    }
    return 0;
}
It is definitely unsafe, why are you using pointers at all? Do you understand how the code should work if you remove the asterisk on line 10?
Last edited on
I was using pointers because I wanted to be able to have a list of variables, which could be shortened or made longer based on user input, and i thought a vector of pointers would allow that. Else I thought i would have to have a long array pre-made, and i would have to put elements of that array into the vector when the user performs an action, and I tried that at first but the code got more complicated than I was hoping, and so I wanted to try something else.
I believe if I remove the asterisk on line ten I would be creating a vector of integers, rather than pointers to integers, and so I would have to adjust the rest of the code accordingly. Except can't you only use 'new' to create pointers?
Is there a better way of accomplishing this in-process declaration of variables, or should I just use a long pre-defined array?
Let me know if I'm not making sense.
Thanks
std::vector<int> is "a list of variables, which [can] be shortened or made longer based on user input".
Last edited on
I second L B's mentioning of std::vector. For the majority of cases that I've needed a dynamic array, vector has suited me just fine. (I've had some exceptions but few).

Vectors make anything from staying in bounds to memory management easier to do.
@Austin J: the OP was already using a vector, just improperly ;)
but.. I thought you could only add an element to a vector if the element existed already.
Like in order to have a vector of two integers you'd have to say
1
2
3
4
5
int x;
int y;
vector<int> vec1;
vec1.push_back(x);
vec1.push_back(y);

Can you make a vector without first having the elements exist elsewhere?
Or to refine that.. I just would rather not have to first declare a long list of variable and then add/subtract them into the vector whenever the user performs a command, but would my initial code be safe if I just didn't use pointers? As in, I initialize a temporary variable and add that to the vector, and then reuse the temporary variable again later to add another element to the vector? I feel like that shouldn't be ok though.


So either, can you reuse a variable to add elements to a vector, or is there a way to lengthen a vector without using push_back?
Last edited on
Ok asking these questions made me realize what I needed, found it here http://www.cplusplus.com/reference/vector/vector/resize/
Thanks:)
No, you don't need that function at all. push_back resizes the vector for you. How could you think that a vector of pointers was any different from a vector of non-pointers? ;)
Topic archived. No new replies allowed.