Getline() keeps skipping lines in for loop - c++

I'm trying to print the value of variables, one of them is pointer and the second one is simply an int.
I did assign a value to a pointer - a, and than I assigned pointer to variable. I'm trying to output the value of variable which meant to show the value of pointer, but I got nowhere.
Since the compiler shows the following warning:
'a' is used unitialized in this function
and eventually after compilation proccess is done I get the error while running program, windows pop-up tells me that:
"The instruction at 0x00401359 referenced memory at 0x00417c7e. The memory could not be written. Click on OK to terminate the program."
What is wrong with this piece of code ?
#include <iostream>
using namespace std;
int main(void)
{
int *a;
int b;
*a = 5;
b = *a;
cout << b << " " << *a;
}

a is a pointer and it should point somewhere. If you do not initialize it, it points just anywhere, thus your assignment *a=5 tries to write into a random memory address.
#include <iostream>
using namespace std;
int main(void)
{
int c; // reserves some space for a to point to
int *a = &c; // NOW a is initialized and it points to c
int b;
*a = 5; // writes 5 into the variable c
b = *a;
cout << b << " " << *a;
}

#include <iostream>
using namespace std;
int main(void)
{
int *a;
int b;
a=&b;
*a = 5;
cout << b << " " << *a;
}
Your pointer a does not have a variable address on which it can save this value.

Related

I am getting a issue in copying a pointer to another pointer in c++

I am getting segmentation fault in first code but the second code is running fine, don't know how?
How can I copy a pointer and save it to another pointer?
#include <iostream>
using namespace std;
int main() {
int *p;
int *p1;
*p1=7;
p=p1;
cout<<*p<<" "<<p;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int *p1;
*p1=7;
int *p=p1;
cout<<*p<<" "<<p;
return 0;
}
//7 0x7ffeea73db70
Both cases invoke undefined behaviour, in both cases you make use of uninitialized pointer p1, the fact that the second case "works" for you is a matter of sheer luck, as you can see here.
For your code to be valid you need to make it point to a valid memory address either by allocating memory manually:
int *p1 = new int; //raw pointer, (better to use smart pointers* but let's not get ahead of ourselves).
Or by assigning it the address of a valid int variable:
int i = 5;
int *p1 = &i;
How can I copy a pointer and save it to another pointer?
A pointer is essentially a variable like any other, you can copy it like you do a normal primitive variable, in fact you do just that when you do p = p1, these are two different pointers that will now contain the same value, the address of the variable they point to.
This code exemplifies this
#include <iostream>
using std::cout;
using std::endl;
int main() {
int *p1 = new int;
*p1 = 7;
int *p = p1;
cout<< "Value stored in the address p points to: " << *p << endl
<< "Value stored in the address p1 points to: " << *p1 << endl
<< "Address where p points to: " << p
<< " " << endl << "Address where p1 points to: "<< p1
<< endl << "Address of p: " << &p << endl << "Address of p1: "<< &p1;
return 0;
}
The output:
Value stored in the address p points to: 7
Value stored in the address p1 points to: 7
Address where p points to: 0x804150
Address where p1 points to: 0x804150
Address of p: 0x7ffc9447e220
Address of p1: 0x7ffc9447e228
*What is a smart pointer and when should I use one?
When you dereference an int* pointer, you promise that there is an int object at that address. C++ believes you, often without questioning. But you never wrote int a; p1=&a; or any other code that made sure p1 points to an actual int.
In fact, p1 isn't even a null pointer. The only thing you can do with p1 is to assign it a legal value. That is to say, it must appear at the left-hand side of an assignment first. It can't be used on the right hand side in int* p = p1;

Can I directly assign an address to a pointer? If so, how to do that?

int main()
{
int a = 2; // address is 0x7ffeefbff58c
int *b = &a;
std::cout << "address of a: " << b << std::endl;
return 0;
}
I have my int variable a at address 0x7ffeefbff58c, but can I directly assign int* b with 0x7ffeefbff58c?
I tried int * b = 0x7ffeefbff58c;
But there is an error says "cannot initialize a variable of type 'int *' with an rvalue of type 'long'", so do I have to use the address of a (&a) to initialize the pointer? or there is other way to do it?
can I directly assign int* b with 0x7ffeefbff58c?
Technically, yes.
If so, how to do that?
With reinterpret cast.
But do realise that there is absolutely no guarantee in general that a would be in the address 0x7ffeefbff58c. As such, there isn't much that you can do with such integer reinterpreted as a pointer. Doing this with a local variable would be pointless.
A case where interpreting integer as a pointer is useful is some embedded systems that reserve some constant memory addresses for special purposes.
Heere is an example:
#include <iostream>
int main()
{
int *b = (int*) 0x7ffeefbff58c;
std::cout << "b: " << b << std::endl;
return 0;
}
after compilation and execution you will see output:
b: 0x7ffeefbff58c

Why does the value change based on whether or not I print a value in the constructor?

Given the following C++ code segment, the behavior and output is as expected:
#include <iostream>
using namespace std;
class A {
public:
int n;
int *p;
A(int n);
};
A::A(int n) {
this->n = n;
this->p = &n;
cout << *(this->p) << endl;
}
int main(int argc, char *argv[]) {
A a(55);
cout << a.n << endl;
cout << *(a.p) << endl;
}
The output is:
55
55
55
But when the print line in the constructor is commented out, this is the result:
#include <iostream>
using namespace std;
class A {
public:
int n;
int *p;
A(int n);
};
A::A(int n) {
this->n = n;
this->p = &n;
// cout << *(this->p) << endl;
}
int main(int argc, char *argv[]) {
A a(55);
cout << a.n << endl;
cout << *(a.p) << endl;
}
Output:
55
32767
I realize that 32767 is not an arbitary number, as it is (2^15)-1, but why does the value printed by the final cout statement of the main method change based on whether that line in the constructed is commented out or not?
this->p = &n;
n here resolves to the parameter to the constructor. This sets p to point to the argument to the constructor, and not the class member. When the constructor terminates, p points to a destroyed object (the constructor parameter value), and dereferencing p is undefined behavior.
This is one argument against using the same names for the constructor's parameters as the names of class members that are getting initialized. Makes it easy to unintentionally create undefined behavior.
Change this to:
this->p = &this->n;
to get the expected results.
This line:
this->p = &n;
means "set this->p to point at the local variable n". Which is perfectly fine, except that the local variable goes out of scope immediately afterward, after which point any attempt to dereference p invokes "undefined behavior" (because it points to memory that is no longer properly allocated). This means that the program is allowed to do absolutely anything, up to and including growing a fist and shaking it at you menacingly. (Fortunately, no major compilers support that kind of biotechnology yet.)
If you run the same experiment on a different compiler, you will likely see a different result, as it's quite random.
I'm guessing that you instead meant to write
this->p = &(this->n);
so that it instead points to the member n?

Why does my code produce a segmentation fault?

#include <iostream>
using namespace std;
int main ()
{
int **a;
int b[5] = {3,4,5,6,1};
*a=b;
cout << *((*a)+0) << endl;
return 0;
}
According to my understanding *((*a)+0) is equivalent to (*a)[0]. Am I wrong? How can I make the above code print the first element of the array?
And why does this code work?
#include <iostream>
using namespace std;
int main ()
{
int *a;
int b[5] = {3,4,5,6,1};
a=b;
cout << *(a+0) << endl;
return 0;
}
When I replace a with *a everywhere, why is it wrong?
You access an uninitialized pointer in
*a=b;
At this point a points to a random location, and as is the rule with undefined behavior you can't predict what will happen. For you it seems to be a location that you can't write to, and so you get a crash.
The second variant works because then you make a point to b, you don't write to an uninitialized pointer, you actually initialize the pointer with the location of the first item in b.

segmentation fault for the code

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
union type{
int a;
char b;
int *p;
char *s;
int arr[10];
};
int fn(union type *exp){
exp->p = exp->p+1;
cout << *(exp->p);
cout << "\n";
return 0;
}
int main(){
union type *str;
str->a = 10;
str->b = 'n';
str->p = &(str->a);
cout << (str->p);
cout << "\n";
fn(str);
cout << str->p;
cout << "\n";
return 0;
}
This code is giving me segmentation fault. Is it because i need to allocate memory to the union explicitly using malloc?? I am new to coding and trying to learn c++.
This code is giving me segmentation fault. Is it because i need to
allocate memory to the union explicitly using malloc??
Right. Your str pointer isn't pointing to valid memory location, it even not initialized. So, before writing str->a you need to set str to something.
You are declaring a pointer to a union, but the pointer is not pointing to any valid memory, which you need to either malloc/new. What it is pointing to is undefined (garbage pointer).