lists and dynamic variables

Hello again. I am still learning some basic C++. I am trying to write a short algorithm that creates a set of integers at run time and inputs them into a list. As far as I can tell, you can only dynamically create pointers not variables themselves, which is awkward if you want a list of integers as an end-product. ( I swear this was far easier to do in C#).

Is there a way to get around it or do you have to work with a list of pointers instead?
Your terminology might be a bit "off". A pointer is a variable that stores an address.

Actually, a pointer is an object, just like all data types. Objects can be either in statically allocated memory (aka variables) or in dynamically allocated memory. The location (address) of dynamically allocated memory is determined during execution of the program and should be stored, or the object in location cannot be used. Pointer stores address and has dereferencing operator. A pointer is usually not a dynamic object.

Dummy example:
1
2
3
4
5
6
7
8
9
10
int foo = 7; // static variable
int ** bar; // static variable
bar = new int*; // dynamic object. Unnamed, but lets call it "gaz"
// The address of gaz is stored in bar

*bar = &foo; // The address of foo is stored in gaz

**bar = 42;
assert( 42 == foo );
delete bar; // deallocate gaz 

Pointers are a powerful and versatile utility far beyond mere dynamic memory allocation. However, with power comes tediousness and danger.


There is encapsulation to combat those two. A more complex class can implement the tedious bits with pointers, but provide a simple interface for the users of the class.


You didn't describe your "list" in detail. Are you aware of std::list? Does your algorithm really need a list, or would a dynamic array (std::vector) suffice?
Thanks for the reply. I think i didn't word my question properly. My issue is not understanding pointers or lists. I would like to be able to create integers during run time and store them in a list. eg

std:: list <int> integer_list ;

while (count < Max_count) {

if ( count %10==0)
{
int integer = new int () ;
integer_list.add(integer);
}

count++;
}


something of the sort......
(I know syntax and grammar isn't correct but I hope you're getting an idea of what I'm trying to learn)

You can achieve this very easily in C# but, as far as I can tell, you cannot get around this in C++ without resorting to pointers somehow (which is frustrating)
Last edited on
1
2
3
4
5
6
7
8
9
10
//vector is the best fit for container in your case until proved otherwise
std::vector<int> integer_list; 
constexpr int max_count = 111;
for(int count = 0; count < max_count; ++count) {
    if(count % 10 == 0)
        //Adds a new integer to the end of integer_list container
        integer_list.push_back(count); 
}
for(int i : integer_list)
    std::cout << i << ' ';
0 10 20 30 40 50 60 70 80 90 100 110 
http://coliru.stacked-crooked.com/a/67d92e9442494bfb
Last edited on
thank you for the reply.

But I am looking to physically create a new integer during run time. This is purely an exercise for understanding different aspects of the language. I am not looking to write anything efficient.
I am looking to physically create a new integer
It physically creates a new integer when you add one in container. Define what you mean by "new".
The only difference in these lines
1
2
int i;
int* p = new int;
is that first one creates a new integer in automatic ("managed") memory and gives some guarantees anout time of it destruction. Second line creates an unnamed integer in free store and stores pointer to it in another variable. You now have to handle it lifetime yourself.
OK. So there is no way of having a statement like

int p = new int ;

??

This exercise is a precursor to defining a new class which I will use to classify electrons. My aim is to be able to create a new electron during run time and add it to a list.

Will i be able to do something like this ?

electron particle = new electron () ;

list.add(particle) ;

MiiNiPaa's code does create new integers during runtime (because of the vector copying count), but they are created on the stack. If you insistent on using new, then I think this would work.
1
2
3
4
5
6
7
8
9
10
//vector is the best fit for container in your case until proved otherwise
std::vector<int*> integer_list; 
constexpr int max_count = 10;
for(int count = 0; count < max_count; ++count) {
    int* a = new int(count*10);
    integer_list.push_back(a); 
}
for(int i : integer_list)
    std::cout << *i << ' ';
    delete i;

This is untested, but I'm pretty sure it should work, although there is basically no reason to ever do it like this.
thank you all for your patience. I can see why you are thinking that it is a convoluted way of going about it. However i find it necessary to play around with integers first before i start defining my own classes and using instances of those classes instead.
but they are created on the stack.
Technically vector stores data in free store by default.

electron particle = new electron () ;

list.add(particle) ;
No. Operator new returns pointer to unnamed object in free store.
Also, you should not do this. C++ is not Java. It is not C#. You should not use C++ as you use these languages. They are different and uses different approaches. There is no need to use new each time you need a new object. In fact, good C++ code does not use new at all. Even if you really need object to outlive its scope, use smart pointer to do so. Storing pointers in containers is need only in specific circumstances.
Last edited on
Topic archived. No new replies allowed.