cout with pointers c++ - c++

I can not understand why cout does not work in this code:
#include<iostream>
using namespace std;
int main() {
int v = 65;
int* q = &v;
char** o = (char**)&q;
cout << o << endl; // output: 012FFCAC
cout << *o << endl; // output: A
cout << **o << endl; // output: A
printf("%c",*o); // cause an error
printf("%p",*o); // works and the output=&v
And cout does not work else in this code
#include<iostream>
using namespace std;
int main() {
char v = 65;
cout << &v << endl; // output: A╠╠╠╠▀?╞«4°O

Because an int is (usually) 4 bytes 65 will fit quite neatly into just the first byte and the rest of the memory allocated for the int will be 0 this byte pattern happens to match up very closely to how strings are stored in memory.
So when the memory is accessed through a char* it will print A most of the time even though most of the prints were ill formed
int v = 65; // Byte pattern on most machines [65,0,0,0]
int* q = &v;
char** o = (char**)&q; // *o now points to [65,0,0,0] ==> ['A','\0','\0','\0'] ==> "A"
std::cout << o << std::endl;
// input: char**
// printed as: pointer
// result: 012FFCAC
std::cout << *o << std::endl; // Undefined Behaviour
// input: char*
// printed as: null terminated string
// result: "A"
std::cout << **o << std::endl; // Undefined Behaviour
// input: char
// printed as: single character
// result: 'A'
printf("%c",*o); // %c expects a [char] type but is given [pointer to char] ERROR
printf("%p",*o); // %p expects a [pointer] type and is given a [pointer to char] OK
Since a char is (usually) 1 byte there is no null terminand to stop the printing once it starts and it keeps printing whatever is around in memory until it runs into a 0 or an access violation
char v = 65;
std::cout << &v << std::endl; // &v is not a well-formed null terminated string: Undefined Behaviour
// input: char*
// printed as: null terminated string
// result: "A<whatever other data is around v in memory>"

Related

How to read memcpy struct result via a pointer

I want to copy a struct content in memory via char* pc the print it back but here I have an exception (reading violation)
struct af {
bool a;
uint8_t b;
uint16_t c;
};
int main() {
af t;
t.a = true;
t.b = 3;
t.c = 20;
char* pc = nullptr;
memcpy(&pc, &t, sizeof(t));
std::cout << "msg is " << pc << std::endl; // here the exception
return 0;
}
then I want to recover data from memory to another structure of same type.
I did af* tt = (af*)(pc); then tried to access to tt->a but always an exception.
You need to allocate memory before you can copy something into it. Also, pc is already the pointer, you need not take the address of it again. Moreover, the byte representation is very likely to contain non-printable characters. To see the actual effect the following copies from the buffer back to an af and prints its members (note that a cast is needed to prevent std::cout to interpret the uint8_t as a character):
#include <iostream>
#include <cstring>
struct af {
bool a;
uint8_t b;
uint16_t c;
};
int main() {
af t;
t.a = true;
t.b = 3;
t.c = 20;
char pc[sizeof(af)];
std::memcpy(pc, &t, sizeof(t)); // array pc decays to pointer to first element
for (int i=0;i<sizeof(af); ++i){
std::cout << i << " " << pc[i] << "\n";
}
af t2;
std::memcpy(&t2, pc,sizeof(t));
std::cout << t2.a << " " << static_cast<unsigned>(t2.b) << " " << t2.c;
}
Output:
0
1
2
3
1 3 20
Note that I replaced the output of pc with a loop that prints individual characters, because the binary representation might contain null terminators and pc is not a null terminated string. If you want it to be a null-terminated string, it must be of size sizeof(af) +1 and have a terminating '\0'.

Why am I unable to obtain memory address of char or uint8_t variables in MSVS2019 / C++?

I need to obtain the memory address for some of the variables in my program. I have no issues getting addresses of 2 or 4 byte variables of type short, int, uint32_t, etc. However when I try to get address of 1 byte variable, I get a garbled text on the console, instead of numerical address.
Here is the code that I'm working with:
#include <iostream>
int main()
{
char test2[16] = { 'a','b','c','d','e','f' };
std::cout << "test2 memory address: " << &test2 << std::endl;
int a = 69;
uint8_t b = 69;
//int x = (int)&b;
char z = 0x33;
short z1 = 0x1551;
std::cout << "memory address: int a " << &a << std::endl;
std::cout << "memory address: uint8_t b " << &b << std::endl;
std::cout << "memory address: char z " << &z << std::endl;
std::cout << "memory address: short z1 " << &z1 << std::endl;
return 0;
}
Console output:
test2 memory address: 0075FE28
memory address: int a 0075FE04
memory address: uint8_t b E╠╠╠╠╠╠╠╠E
memory address: char z 3╠╠╠╠╠╠╠╠√²u
memory address: short z1 0075FDD4
Using built in "memory view" in debugging mode, I can see the 1 byte variables and they should have the following addresses:
b: 0075fdfb
z: 0075fde3
Can someone pinpoint what am I doing wrong?
Stream output will treat certain types as legacy C strings (such as char * for example) and therefore assume you want to print the string behind the pointer.
To fix this, just turn it into a type that the stream won't treat as a legacy C string, something like:
#include <iostream>
char xyzzy[] = {'h', 'e', 'l', 'l', 'o', '\0'};
int main() {
std::cout << xyzzy << '\n';
std::cout << reinterpret_cast<void*>(xyzzy) << '\n';
}
You'll see the first one prints out the entire string while the latter treats it as a pointer.

Conversion of string array element to int in c++ using stoi()?

I have a piece of code:
#include <bits/stdc++.h>
using namespace std;
int main() {
//ios_base::sync_with_stdio(false);
string s[5];
s[0] = "Hello";
s[1] = "12345";
cout << s[0] << " " << s[1] << "\n";
cout << s[0][0] << " " << s[1][1] << "\n";
int y = stoi(s[1]); //This does not show an error
cout <<"y is "<< y << "\n";
//int x = stoi(s[1][1]); //This shows error
//cout <<"x is "<< x << "\n";
return 0;
}
The output of this code is:
Hello 12345
H 2
y is 12345
But it shows an error when I uncomment
int x = stoi(s[1][0]);
cout <<"x is "<< x << "\n";
If in both the cases a string is being converted to int using stoi()
function then why do the later part of code gives an error?
I have tried the same using atoi(s[1][0].c_str()) but it also gives an error.
What is the alternative for this, if I want to convert the second type of elements to int?
s[1] is a std::string, so s[1][0] is a single char in that string.
Calling std::stoi() with a char as input doesn't work because it takes only a std::string as input, and std::string doesn't have a constructor that takes just a single char as input.
To do what you are attempting, you need to do this instead:
int x = stoi(string(1, s[1][0]));
Or
int x = stoi(string(&(s[1][0]), 1));
Your call to atoi() doesn't work because you are trying to call c_str() on a single char instead of the std::string it belongs to, eg:
int x = atoi(s[1].c_str());
stoi has as input a string not a char.
Try this:
string str(s[0][0]);
int y = stoi(str);

Pointer and char in struct

How to read the second letter in char with a pointer? I can read the whole message "carp" and first letter 'c' but I have no idea how to read second letter... Here is my example code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct list {
char name[20];
int length;
};
list first ={
"carp",
6,
};
list *p = &first;
cout << p->name << endl; // "carp"
cout << *p->name << endl; // "c"
p = p + 1;
cout << *p->name << endl; // Not working...How to read a?
return 0;
}
Incrementing p with p++ or p = p+1 moves you to the next instance of struct list, which is not what you want (and it's not even there).
Instead, you want to move to the second letter of name, which can be done in several ways:
Use index on the name - cout << p->name[1] << endl;
Make a pointer to p->name and increment it, i.e. char *np = p->name; np++; cout << *np
Use pointer arithmetic instead of indexing, i.e. cout << *(p->name+1) << endl;
You can use index on name to access any character:
p->name[1] // gives 'a'
p->name[2] // gives 'r'
Note that arrays begin with an index of 0. So p->name[0] would give 'c'.
p + 1 actually increments p, which is a pointer to list. This essentially moves to the next instance of list, which is not even initialized in your code.
Use the array subscript operator with an index of 1:
p->name[1];
If you want to output the second character using pointers without the subscript operator then you can just write
cout << *( p->name + 1 ) << endl;
It is the same as
cout << p->name[1] << endl;
Or you can introduce an intermediate pointer. For example
for ( const char *q = p->name; *q != '\0'; ++q )
{
cout << *q;
}
cout << endl;

Why does this code print 0

Why does the following code print 0? i.e. why does variable a is located right after variable d, even though pointer variable c is being declared between them?
#include<iostream>
using namespace std;
int main () {
unsigned int a = 100;
unsigned int &b = a;
unsigned int *c = &b;
unsigned int d = (unsigned int)(c);
int e = &d - c;
int &f = e;
e ++;
cout << e << " " << endl;
return 0;
}
Working backwards:
e ++;
cout << e << " " << endl;
If this prints 0, then the value of e before executing this code must have been -1.
int e = &d - c;
So the result of the above address subtraction must have been -1.
unsigned int a /* = whatever, the value of a doesn't matter */;
unsigned int &b = a;
unsigned int *c = &b;
unsigned int d /* = whatever, the value of d doesn't matter */;
b is a reference to a, so &b is equivalent to &a.
So &d - c is equivalent to &d - &a, and that subtraction yields -1.
Conclusion: the address of d is sizeof (unsigned int) bytes after the address of a. (Pointer subtraction is scaled by the size of the pointed-to type.)
Probably.
In fact, the behavior of subtracting pointers to two independently defined objects is undefined. The standard says literally nothing about what it should do.
In practice, a compiler will probably generate the simplest possible code for a pointer subtraction, and that simple code will probably treat unrelated pointers as if they were comparable, even though the language doesn't say they are.
It's likely, given your program's output, that b and d happen to be allocated next to each other. Nothing says that declared variables have to be allocated in the order in which you declare them. If you want objects to be allocated in memory in a define order, put them into a struct or make them elements of an array.
It's also likely that the same program will yield different results if you run it on a different system, or on the same system with a different compiler, or on the same system with the same compiler with different compiler options. In principle, it could even behave differently with everything the same but during a different phase of the moon.
And a compiler is permitted to assume that your code's behavior is well defined, and perform transformations that are valid only given that assumption. In effect, by subtracting two unrelated pointers, you have promised the compiler that they both point to elements of the same array object or just past the end of it (where a single object is treated as a 1-element array) (or that both are null pointers; that's one difference between C and C++). You have lied to the compiler, which means it is under no further obligation to you.
Don't do that.
Unless you explicitly place objects using your own memory management system, their relative positions in memory will be compiler- and system-dependent.
your line int e = &d - c; is substracting 2 unsigned int *.
In memory, &d is 8 bytes farther then c (it depend on your system, but we suppose that an int is 4 bytes). Effectively, you construct your stack in this way :
unsigned int a = 100; // &a is 0x0
unsigned int &b = a; // &b is 0x0 (it's just an alias)
unsigned int *c = &b; // &c is 0x4
unsigned int d = (unsigned int)(c); // &d is 0x8
An unsigned int use 4 bytes in memory.
So, when your are doing &d - c, it must return 2, because your are using pointer arithmetics with unsigned int* (4*2=8);
You can try with int e = (short*)&d - (short*)c result should be 4 because short size is 2 (2*4=8).
You can try with int e = (char*)&d - (char*)c result should be 8 because char size is 1 (1*8=8).
Try to print your variables and addresses to understand :
#include<iostream>
using namespace std;
int main () {
unsigned int a = 100;
unsigned int &b = a;
unsigned int *c = &b;
unsigned int d = (unsigned int)(c);
int e = (short*)&d - (short*)c;
//int &f = e;
//e ++;
cout << "&a: " << (unsigned int)&a << endl;
cout << "&b: " << (unsigned int)&b << endl;
cout << "&c: " << (unsigned int)&c << endl;
cout << "&d: " << (unsigned int)&d << endl;
cout << endl;
cout << " a: " << a << endl;
cout << " b: " << b << endl;
cout << " c: " << (unsigned int)c << endl;
cout << " d: " << d << endl;
cout << endl;
cout << " e: " << e << endl;
return 0;
}
Here, with int e = (short*)&d - (short*)c;, result is :
&a: 3220197356
&b: 3220197356
&c: 3220197360
&d: 3220197364
a: 100
b: 100
c: 3220197356
d: 3220197356
e: 4