Hi all,
I am still in the process of lerning how to write decent C++ code. I will appreciate any good advice or corrections. I have two questions, a technical one and one for advice for how to design my code.
1. I have read somewhere (can't remember, must have been some tutorial page) that objects allocated on the heap can only be accessed through pointers. Thus, for example
Here v_ptr points to a vector of 100 integers living on the heap.
I can access the entries of the vector via "v_ptr->at(4)" or "(*v_ptr)[4]" . (Is there another way to access the entries?)
I could try to fill a local vector variable with heap contents like
but if I understand correctly, this will first create a vector object with 100 integers on the heap, take its pointer, dereference it, and then construct ANOTHER vector object on the stack (named v) from the one on the heap using the copy constructor. Thus, this would lead to an immediate memory leak, since there is no way to call delete on that pointer any more. (Am I understanding this correctly?)
Now, my question: What will happens if I type
Is there any copying going on here that I can't see? Or have I really constructed only one vector on the heap, which I can now access like "v[4]" , and which I can delete later on with "delete &v;" ?
2. While the first question was just about understanding the code I have another one concerning my way of code design. I have read the following advice:
Now, I am in the situation that I have to write functions that receive a number of vectors (normally of complex numbers) and produce several of them. And these vectors can have a pretty large size. I need my code to work with vectors with 1,000,000 entries.
Now my questions:
I would greatly appreciate anything useful you would have to say.
~<><~~~~~~~~~ presencia
I am still in the process of lerning how to write decent C++ code. I will appreciate any good advice or corrections. I have two questions, a technical one and one for advice for how to design my code.
1. I have read somewhere (can't remember, must have been some tutorial page) that objects allocated on the heap can only be accessed through pointers. Thus, for example
Code:
vector<int> * v_ptr = new vector<int>(100);
I can access the entries of the vector via "v_ptr->at(4)" or "(*v_ptr)[4]" . (Is there another way to access the entries?)
I could try to fill a local vector variable with heap contents like
Code:
vector<int> v = *(new vector<int>(100));
Now, my question: What will happens if I type
Code:
vector<int> v& = *(new vector<int>(100));
2. While the first question was just about understanding the code I have another one concerning my way of code design. I have read the following advice:
- Don't allocate variables on the heap unless you have to (in case you need the data outside of the scope it was created in, or in case there is not enough space on the stack).
- If you allocate memory using the "new" operator, then follow the RAII principle, make use of some wrapper objects (like std::auto_ptr) on the stack that will do the deleting for you, so that there will be no memory leaks.
Now, I am in the situation that I have to write functions that receive a number of vectors (normally of complex numbers) and produce several of them. And these vectors can have a pretty large size. I need my code to work with vectors with 1,000,000 entries.
Now my questions:
- Is it the right choice to use the heap (create vectors using "new") in this case, even if I need the objects just for the scope I am in?
- If I want a function to return more than one vector object, I would create one before calling the function and than call it providing it with a reference (or pointer) to that vector so that it can write to it. How do I do this best when using "std::auto_ptr" s ? I can't just give the auto_ptr as a parameter to the function because then the vector will be deleted on return.
I would greatly appreciate anything useful you would have to say.
~<><~~~~~~~~~ presencia
Comment