Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
given class (for example in name of X) , I want to allocate array like that:
X** array=new X*[20];
Let's look about the following function:
void Func(){
X** array=new X*[2];
X[0]=& X(5);
X[1]=& X(3);
}
Is it OK to do it like that or that I must to do it with new?
The thing is, when you do something like this :
X foo(5);
X[0] = &foo;
(wich i suppose is what you want to say)
X[0] will take the adress of foo, wich is a local variable who will only exist in the scope of your function. And if i do not tell bullshit, does not compile. (-fpermissive)
So after the last instruction of your function, the usage of this pointer will be Undefined
You can't be sure after any allocation that the content of your data is equal to nullptr (if not initialise yet). Initialisation syntax have way different behavior depending language, compiler, stars position etc (just kidding for the last one) so make sure to read the documentation about it.
However you can be sure to init something with null Value by using a zero-initialisation. Link here
If you don't want to dynamicly allocate your array of pointer make sure that those pointer still valid during the lifetime of your object
Another solution could be to dynamicly allocate them.
X[0] = new X(5);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I often use references to avoid unnecessary copying that comes with creating a new variable and assigning existing data to it.
Recently I was thinking about the example of a reference being assigned with an expression and whether there are really any advantages to using a reference for that. To give an example, let's say I have two vector object instances and I subtract one from the other. In the example below I am defining a new object instance to which the expression (subtraction) will be assigned to.
VectorClass vector1{// Instantiate with data};
VectorClass vector1{// Instantiate with data};
VectorClass vector3 = vector2 - vector1;
In my understanding, the expression will be evaluated, stored on the stack and copied to the new vector object (vector3).
What exactly does happen in the case of:
VectorClass vector1{// Instantiate with data};
VectorClass vector1{// Instantiate with data};
VectorClass& vector3 = vector2 - vector1;
My guess is that the expression will be evaluated, stored on the stack and the reference &vector3 references this memory, until they both go out of scope and get destroyed.
My question is: Am I correct, and if so, is the only advantage to the second example the fact that the copying when assigning value to vector3 doesn't happen?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
After calling and using the function, I can access the parameters through the pointer. And I think the parameters are kept in memory. I'm trying to delete the parameters after using the code below.
int MyFunction(int p1,int p2)
{
int total = p1 + p2;
delete& p1;
delete& p2;
return total;
}
and using this
int a = MyFunction(15, 30);
(my project name) initiated a breakpoint.I get an error. How do I delete parameters ?
Your function parameters are passed by value, so they are allocated automatically, not dynamically. You cannot delete them. Being automatically allocated, they will also be automatically deallocated when the function returns, so just skip trying to delete them and go straight to returning.
You can only delete objects allocated on heap (via new).
The parameters to the function are allocated on stack before function call and the runtime "removes" them once the function exits.
(The removal just means that the stack pointer is moved to look as if the parameters no longer exist)
How to delete function parameter?
In general, you shouldn't delete arbitrary variables. You should only delete pointers that you explicitly allocated with new somewhere else in your code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int tenTimes(int n) {
return 10*n;
}
void tenTimesVector3(int vector[3]) {
vector[0] = tenTimes(vector[0]);
vector[1] = tenTimes(vector[1]);
vector[2] = tenTimes(vector[2]);
}
Here we are passing one element to the function but using that they are accessing all elements.
If you worry about the mutability of your buffer, then in general yes, it's not "safe" to pass a non-const reference to your buffer into a function. As you noted, the function can modify any array element.
If the function is const correct (it accepts a const reference or pointer), then you can be fairly certain it won't modify your array by accident. A compiler will catch any attempt at modification. Of course, the function can cast away the const and do nasty things, const here will offer some protection from Murphy, not from Machiavelli.
The only way to be absolutely positive a function won't modify your data is to not let it reference it. If you are genuinely concerned, you can just pass it a copy:
std::vector<int> data_we_want_to_keep(3);
// ...
std::vector<int> copy_of_data = data_we_want_to_keep;
tenTimesVector3(copy_of_data.data());
Now the function will not be able to modify data_we_want_to_keep itself, despite needing access to whatever values in that buffer.
Quite often though, code is written to be well behaved. If a function accepts a const reference or pointer to your data, you can be reasonably assured it will only read it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Lets assume I have the following class:
class A
{
public:
A() {}
};
I need to declare a global instance of class A (required by spec.), what works better?
A a; // is the constructor even called here?
or
A* pa; // and allocate later a new instance
Thanks in advance
"A a; - is the constructor even called here?" - yes, the constructor is called.
"* pa; - and allocate later a new instance" - if you actually assign something to this pointer variable before using it, then it can work. But do you?
In general I'd say that the need for global variables indicate a flaw in the design. But if you really have to have them, then make sure they are constructed before you use them.
If A a is used, there would not be a need to release memory allocated to a. When the main() function exits, the destructor for A will be automatically called.
If A *pa is used, ideally pa should be explicitly deleted at some point in the code with delete pa and care should be taken to not use pa after it has been deleted.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a linked list with a set of data and I'm wondering if there's a way to make a variable's address change. Something like:
for(int i=array.size()-1;i>=0;i--)
{
Node *previous_ptr;
Node *current_ptr = new Node(array[i], previous_ptr);
previous_ptr=current_ptr;
delete current_ptr;
}
So I want current_ptr's address to change (allocate a different spot of memory) so the linked list gets constructed if that's possible. And I can't use alloc for it only new and delete.
Calls of new produce different addresses every time you call it, unless you call delete, in which case the memory becomes reusable, so operator new can return it again.
Your code creates hanging pointers: when you call delete on line 3
Node *current_ptr = new Node(array[i], previous_ptr);
previous_ptr=current_ptr;
delete current_ptr;
the value of current_ptr and previous_ptr immediately become invalid. At the same time, the pointer returned by new becomes eligible for reuse, so you get the same pointer when you call new again.
You should not call delete on anything that you plan to use again.
In addition, previous_ptr declared inside a for loop is not useful, because its value is thrown away on each iteration. If you would like to keep that pointer between iterations, declare it outside the loop.
As far as I know, you cannot change a variables address and it doesn't make any sense to me, if you think about what is happening in the background.
If you need a different address for your data, allocate new memory, store your data a 2nd time and delete the old data.