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 am not getting why the below is executing but not when tried in other form:
struct ListNode* &ptr2 = ptr; //This is executing
struct ListNode* ptr2; // This is not executing
&ptr2 = ptr;
Can anyone explain about the code, How it works?
In the first line of code, struct ListNode* &ptr2 = ptr; , a reference to a pointer variable ptr is created and assigned to another pointer variable ptr2. This means that ptr2 is simply an alias for ptr and any changes made to ptr2 will also affect ptr.
In the second line of code, struct ListNode* ptr2; , a pointer variable ptr2 is declared but not initialized. This means that ptr2 does not currently point to any memory location.
In the third line of code, &ptr2 = ptr; , you are attempting to assign the address of ptr to ptr2. However, ptr2 is not a reference to a pointer variable, so this line of code will not compile.
In the first, the type of ptr2 is struct ListNode* & – a reference to a pointer – and it is initialized as a reference to ptr.
The names "ptr" and "ptr2" refer to the same object.
It is the same situation as
int x = 0;
int& y = x;
In the second, the type of ptr2 is struct ListNode* and you then attempt to assign the value of ptr to the address of ptr2, which is impossible.
This is the same situation as
int x = 0;
int* y;
&y = x;
Related
This question already has answers here:
c++ delete pointer issue, can still access data [closed]
(6 answers)
C++ pointer array is still accessible after delete[] is called [duplicate]
(1 answer)
Closed 1 year ago.
What is the diffrence between
{p = NULL ;}
and
{Delete p; }
When I executed this code:
int *p ;
p = new int;
* p = 4;
cout << *p; // = 4
delete p ;
cout << *p; //
In the last line, I expected to get a garbage value because the pointer is not pointing at the same location anymore, right? But instead I got the same value which is 4, how is this possible?
p = NULL; merely sets p to NULL, but does nothing to the memory that p was pointing at.
delete p; frees the memory that p is pointing at, provided that memory was allocated with new (as it is in your example).
In your example, trying to access *p after calling delete p; is undefined behavior, so anything can happen. p is still pointing at the same address it was before, delete does not alter p itself in any way, but the memory that p is pointing at is no longer valid. You happen to be seeing the old data that was stored in that memory, but only because it simply hasn't been overwritten yet. But it is still not permissible to access invalid memory for any reason. The actual memory be still be physically allocated by the underlying memory manager, but is logically inaccessible to your code, until it is reallocated again.
This question already has answers here:
why std::unique_ptr vector gets invalid pointer exception
(2 answers)
Closed 7 years ago.
I have simple (I suppose) problem with my C++ code. Each time I initialize unique_ptr through constructor I'm getting the same problem:
munmap_chunk(): invalid pointer: 0x00007ffc40ba6bf0
Here's the code:
1) Private part of the class:
template<typename T>
class Node{
typedef std::unique_ptr<Node> ptr;
T value;
ptr left, right;
2) Constructor initializing ptr left and ptr right:
explicit Node(const T value, Node* left = nullptr, Node* right = nullptr):
value{value}, left{left}, right{right} {}
What is the problem here? Thanks for clarification or pointing me in the right direction.
From your comment Example initizalization:
Node<int> node(12);
Node<int> node2(15, &node);
will obviously crash, since you pass the address of an automatic variable (node) into a unique_ptr<>, which upon its destruction will try to delete it, even though it was never allocated with new.
Use unique_ptr<> only for objects on the heap!*
(Another lesson from this exercise: always put a MCVE, which is guaranteed to contain the problem, while your code didn't.)
* unless you know what you're doing, i.e. if delete is overloaded for your type or if you pass an appropriate deleter to unique_ptr.
I'm trying to implement a BST in C++, and I came across these two ways of creating a node:
node* z = new node();
z->key = d;
z->left = NULL;
z->right = NULL;
and then this:
node* y = NULL;
node* x = root;
node* parent = NULL;
What is the difference in calling the new operator or not?
EDIT
so for example, whats the difference between:
node* q = new node();
and
node* q;
and why would you choose one way or the other? Is there an advantage for one way or the other?
To answer the question in the comment, yes there is a difference.
When you do node* q = new node() you declare a pointer to a node object, and then at runtime dynamically allocate memory enough for one node object, and calls the default constructor (alternatively, value initializes the object, it depends on the class/struct), and finally you assign the pointer to the newly allocated object to q.
When you do node* q; you just declare a pointer to a node object, and that's it. What the pointer will be pointing to depends on where you declare the pointer: If you declare it as a global variable, it is initialized to zero, and becomes a null pointer; If you declare it as a local variable its value is indeterminate, and will in reality seem to be random. Neither is a valid pointer, and dereferencing it will lead to undefined behavior. There is also a third alternative, and that's where you declare node* q; as a member variable in a class or struct, then the initial value depend on how the class/structure is created and initialized (for example if the structure is value initialized, if there's a constructor that initializes it, etc.).
I'm pretty sure (Someone correct me if i'm wrong), using the new operator allocates dynamic memory for that variable. The variables will most likely already have some garbage value assigned to them that gets overridden when you assign a new value to them. Whereas assigning it NULL just assigns it whatever the NULL value is (probably 0000000).
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
Consider the following code:
struct A {
int someInt;
};
A *a = new A;
A *b;
delete a; // ok
delete b; // crash
It crashes, but I don't know why. I thought deleting null pointers is a no-op.
Do I even need to delete structs? Is it bad practice to create structs on the heap? Is it better to create them on the stack?
Deleting null pointers is a no-op, yes, so if b were a null pointer then you'd be fine.
But it's not. It's an uninitialised variable with an indeterminate value.
Here's a null pointer:
A* b = nullptr;
(If you write A* b; at global scope, or if you write static A* b; in a function, or if you are defining a static class member, then b has static storage duration and is automatically zero-initialised for you; this is the one exception to the rule.)
And, yes, you should avoid dynamic allocation where possible — using it for no good reason complicates your code for, well, for no good reason.
b is a uninitialized pointer. There is nothing to delete.
The basic knowledge i have about pointers is that they point to a memory location and that they can be used for changing and extracting the values stored at that address.
for eg.
int a=5;
int *b;
b=&a;
Now here b points to the memory location of a.
cout<<*b; //gives the output 5
cout<<b; //outputs the address of a.
Basically for the purpose and passing the values by reference in functions and in data structures such as linked list and trees.
main()
{
node *head=NULL;// A pointer head of type node structure
add(&head,2); //adds the value to linked list
}
void add(node**head,int data)
{
//adds value into node
}
Can any one tell me why the &head has been received as **head in the add() function.
Also, what difference would it have made had it been received as *head?
when i print the value of **head it gives me a compiler error.
type of head is node*, when you get address of node* (&head), type of it is node**.
Difference between node** and node* is same as int* and int. One of them defines a pointer to other type.
add method gets a pointer because it might change the value of head. if *head is NULL, it should create a new node and assign head to address of this new node. if it inputted just node head, assigning head = malloc(...) wouldn't work since c is pass by value and it just assigns the "head" defined within the function, not head in the main. but if gets the address of the head in the main, it can freely alter it by accessing it like *head = malloc(...)
head is a pointer so you can't really print its value (well, you can if you cast it but it wouldn't be anything meaningful). You probably need to cast it
int *p; // p -> is a pointer to an int, *p -> integer contained in address pointed by p.
similarly
int **p2; // p2 -> a pointer to a pointer to an it, *p2 -> pointer to a int, **p2 int.
If you are confused, you can remember it as a covering rule. If you want to know what *p is, just cover *p ( we have int so it is an int ), if you to know what *p2 is, just cover it ( we have int * meaning it is a pointer to an int ).
This is my answer to another question: https://stackoverflow.com/a/20818342/2805305
The question was about an error the op got, but in my answer I explain exactly what you need to know.