rioji
September 8, 2020, 3:43am
1
#include <stdio.h>
void f(int **ptr){
int y=25;
*ptr=&y;
}
int main(void) {
int s=3;
int * ptr = &s ; // ptr is address of s(3) , *ptr=3
f(&ptr); // &ptr is the adress of ptr // pointer to pointer
printf("%d" ,*ptr);
return 0;
}
Why this code is printing 0 can anyone explain? It should print 25?
for me it is printing 25 .
aon_23
September 8, 2020, 6:15am
4
Time of execution is 0 sec. Is that valid ?
rioji
September 8, 2020, 6:16am
5
Is there any problem in codechef compiler?
ssjgz
September 8, 2020, 6:21am
6
Using the contents of a stack frame after exiting that stack frame is Undefined Behaviour.
I get a different result every time I run it.
[simon@simon-laptop][07:13:54]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling rioji-blah2.cpp
+ g++ -std=c++14 rioji-blah2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
rioji-blah2.cpp: In function βint main()β:
rioji-blah2.cpp:17:11: warning: βyβ is used uninitialized in this function [-Wuninitialized]
printf("%d" ,*ptr);
~~~~~~^~~~~~~~~~~~
+ set +x
Successful
[simon@simon-laptop][07:13:56]
[~/devel/hackerrank/otherpeoples]>./a.out
22062
5 Likes
rioji
September 8, 2020, 6:30am
7
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void insertAfter(Node* prev_node, int new_data)
{
if (prev_node == NULL)
{
cout<<"the given previous node cannot be NULL";
return;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
void printList(Node *node)
{
while (node != NULL)
{
cout<<" "<<node->data;
node = node->next;
}
}
int main()
{
Node* head = NULL;
append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
cout<<"Created Linked list is: ";
printList(head);
return 0;
}
But in linked list we use the same concept of double pointer , but it does not create any issue why?
ssjgz
September 8, 2020, 6:32am
8
The issue has nothing to do with using a double-pointer and everything to do with using the contents of an expired stack frame.
2 Likes
rioji
September 8, 2020, 6:44am
9
so can we correct this code , so i could get the required o/p as 25 using double pointer.
Actually i want to say without exiting stack frame , and avoiding undefined behaviour.
#include <stdio.h>
void f(int **ptr){
int y=25;
*ptr=&y;
}
int main(void) {
int s=3;
int * ptr = &s ; // ptr is address of s(3) , *ptr=3
f(&ptr); // &ptr is the adress of ptr // pointer to pointer
printf("%d" ,*ptr);
return 0;
}
ssjgz
September 8, 2020, 6:45am
10
You can make y
outlive the stackframe by making it static
or global or somesuch.
[simon@simon-laptop][07:46:39]
[~/devel/hackerrank/otherpeoples]>cat rioji-blah2.cpp
#include <stdio.h>
void f(int **ptr){
static int y=25;
*ptr=&y;
}
int main(void) {
int s=3;
int * ptr = &s ; // ptr is address of s(3) , *ptr=3
f(&ptr); // &ptr is the adress of ptr // pointer to pointer
printf("%d" ,*ptr);
return 0;
}
[simon@simon-laptop][07:46:45]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling rioji-blah2.cpp
+ g++ -std=c++14 rioji-blah2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][07:46:50]
[~/devel/hackerrank/otherpeoples]>./a.out
25
3 Likes