memory-address

Returning an address to a local variable vs returning a pointer to a local variable

泄露秘密 提交于 2019-12-02 05:12:58
I have this in my testing.cpp: class Supp{ public: virtual Supp* add(Supp& val) = 0; }; class SubA : public Supp{ public: int val; SubA(int a){ val = a; } int getVal(){ return val; } Supp* add(Supp& value){ SubA& a = dynamic_cast<SubA&>(value); int tempVal = a.getVal(); int sum = val + tempVal; SubA b =SubA(sum); return &b; } }; and the lines SubA b = SubA(sum); return &b; gives and error because itreturns the address to a local variable which is very bad to do, so i changed it to SubA* b =new SubA(sum); return b; and it works fine with no errors, But is this not basically the same thing? why

Standard way to find base address of struct from a member

做~自己de王妃 提交于 2019-12-02 02:48:29
struct Data { int a; std::string b; float c; }; std::string* allocateDataAndGetString() { Data* dataPtr(someAllocator.allocate<Data>()); return &dataPtr.b; } Data* getBaseDataPtrFromString(std::string* mStringMember) { // ??? } int main() { std::string* stringPtr(allocateDataAndGetString()); Data* dataPtr(getBaseDataPtrFromString } I have a Data instance allocated on the heap, and a pointer to its std::string b; member. How do I get the base address of the Data instance the string is a member of, taking into account offsets and padding, in a standard way? I've tried subtracting sizeof(int) and

Does the address of the array equal to that of its first element in C++?

旧时模样 提交于 2019-12-02 01:15:36
问题 This can be guaranteed in C because of the following sentence in WG14/N1570: 6.2.5/20 ... An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. But in WG21/N4527, i.e. in C++, the corresponding sentence becomes 8.3.4/1 ...An object of array type contains a contiguously allocated non-empty set of N subobjects of type T. while the word "describes" is changed to "contains", which cannot guarantee that the address

When could 2 virtual addresses map to the same physical address?

会有一股神秘感。 提交于 2019-12-02 00:57:23
问题 An operating system/computer architecture question here. I was reading about caches, about how virtually indexing the cache is an option to reduce address translation time. I came across the following: "Virtual cache difficulties include: Aliasing Two different virtual addresses may have the same physical address." I can't think of a scenario when this can occur. It's been a while since my O/S days and I'm drawing a blank. Could someone provide an example? Thanks 回答1: Two processes might have

The address of character type variable

不想你离开。 提交于 2019-12-01 21:27:45
Here is just a test prototype : #include <iostream> #include <string> using namespace std; int main() { int a=10; char b='H'; string c="Hamza"; cout<<"The value of a is : "<<a<<endl; cout<<"The value of b is : "<<b<<endl; cout<<"The value of c is : "<<c<<endl<<endl; cout<<"address of a : "<<&a<<endl; cout<<"address of b : "<<&b<<endl; cout<<"address of c : "<<&c<<endl; return 0; } Why the address of variable 'b', which is of character type, not printing? The << operator in your code is overloaded in C++ 11. It doesn't conflict with any of other types like int or string , but it takes pointer

Can I access random data with random memory Addresses outside of my C++ Program

痞子三分冷 提交于 2019-12-01 20:32:04
If 2 programs are running, and one program stores a number at a memory address, and if I know that memory address, and hard code it into the 2nd program and print out the value at the address, would it actually get that info? Does C++ allow programs to access any data stored in RAM no matter if it is part of the program or not? On system with no virtual memory management and no address space protection this would work. It would be undefined behavior from the point of view of the C standard, but it would produce the behavior that you expect. Bad news is that most computer systems in use these

Keeping address in C++ hacking game code? [closed]

丶灬走出姿态 提交于 2019-12-01 20:11:15
I have this code that edits addresses in a game to get unlimited ammo and what not, and I found out that the addresses are different for every computer, sometimes every time you restart the game, so how would I manage making this work still even though they change. If you get the address you're looking for, and then search for that address in memory to find the address of the pointer to that data, and then search for that address in memory so you can find the address of the pointer to it, and so on, you may eventually find an address that does not change. Then, at runtime, you can use that as

Assignment of address to integer variable

自古美人都是妖i 提交于 2019-12-01 17:59:00
How come you can assign an address to an integer variable like this,the complier will not give an error. i always though you can only assign integer values to a integer variable int a=0x28ff1c You can do the same for a char variable, the complier will not give a error char b=0x28ff1c It will output on the console screen rubbish value for char b and a random value for int a cout<<b <<endl; cout<<a; Can someone explain to me why there is a difference in the output for char b and int a. Can someone aslo explain to me why a char variable and integer variable can have addresses assign to it The

Inquiry about class variable declarations in C++

旧街凉风 提交于 2019-12-01 17:51:05
I have a class to represent a 3D vector of floats: class Vector3D { public: float x, y, z; float * const data; Vector3D() : x(0.0), y(0.0), z(0.0), data(&x) {} } My question is: are x, y, and z going to be allocated sequentially in memory such that I can assign the address of x to data and later use the subscript operator on data to access the vector components as an array? For example, sometimes I may want to access the vector components directly: Vector3D vec; vec.x = 42.0; vec.y = 42.0; vec.z = 42.0; And sometimes I may want to access them by offset: Vector3D vec; for (int i = 3; i--; ) vec

Assignment of address to integer variable

谁都会走 提交于 2019-12-01 17:49:21
问题 How come you can assign an address to an integer variable like this,the complier will not give an error. i always though you can only assign integer values to a integer variable int a=0x28ff1c You can do the same for a char variable, the complier will not give a error char b=0x28ff1c It will output on the console screen rubbish value for char b and a random value for int a cout<<b <<endl; cout<<a; Can someone explain to me why there is a difference in the output for char b and int a. Can