memory-address

Why is taking the address of a temporary illegal?

半城伤御伤魂 提交于 2019-11-27 02:46:06
问题 I know that the code written below is illegal void doSomething(std::string *s){} int main() { doSomething(&std::string("Hello World")); return 0; } The reason is that we are not allowed to take the address of a temporary object. But my question is WHY? Let us consider the following code class empty{}; int main() { empty x = empty(); //most compilers would elide the temporary return 0; } The accepted answer here mentions "usually the compiler consider the temporary and the copy constructed as

How does 32-bit address 4GB if 2³² bits = 4 Billion bits not Bytes?

自古美人都是妖i 提交于 2019-11-26 23:24:39
问题 Essentially, how does 4Gb turn into 4GB? If the memory is addressing Bytes , should not the possibilities be 2 (32/8) ? 回答1: It depends on how you address the data. If you use 32 bits to address each bit , you can address 2 32 bits or 4Gb = 512MB. If you address bytes like most current architectures it will give you 4GB. But if you address much larger blocks you will need less bits to address 4GB. For example if you address each 512-byte block (2^9 bytes) you can address 4GB with 23 bits.

GCHandle to get address(pointer) of .net object

倖福魔咒の 提交于 2019-11-26 23:24:22
问题 I managed to get the address of a .net object by GCHandle objHandle = GCHandle.Alloc(obj,GCHandleType.WeakTrackResurrection); int address = GCHandle.ToIntPtr(objHandle).ToInt32(); and I can recall the object by Object obj = GCHandle.FromIntPtr(IntPtr(address)).Target; Well, the purpose is to store the address in a native class and have an information of which native object is releated to which .net object. AFAIK the address does not change because of allocing, is it true or does anyone have a

Swift, Strings and Memory Addresses

不想你离开。 提交于 2019-11-26 22:59:10
There is something I am not understanding about how Swift manages memory address of String(s) 1. Reference types Here foo and boo are 2 pointers to the same memory location . class Foo { } let foo = Foo() let boo = foo unsafeAddressOf(foo) // "UnsafePointer(0x7FCD13719BE0)" unsafeAddressOf(boo) // "UnsafePointer(0x7FCD13719BE0)" Good. 2. Value types let word0 = "hello" let word1 = word0 Now word0 and word1 are value types but here the copy on write mechanism is involved. [...] However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages

What does “DS:[40207A]” mean in assembly?

*爱你&永不变心* 提交于 2019-11-26 22:33:12
0040103A CALL DWORD PTR DS:[40207A] USER32.MessageBoxA What does DS: mean? It means the instruction is referencing memory in the Data Segment - and can pretty much be ignored on modern OSes, since they run with a flat address space model (code, data and stack segments all refer to the same memory range, and memory protection is handled with paging). EDIT: A little elaboration - note that, to keep things simple, this is in the context of 32bit protected mode running Windows. A segment register (CS,DS,SS,ES,FS,GS) holds a selector pointing to a descriptor . There's two descriptor tables: global

C++ Double Address Operator? (&&)

谁说我不能喝 提交于 2019-11-26 21:22:19
I'm reading STL source code and I have no idea what && address operator is supposed to do. Here is a code example from stl_vector.h : vector& operator=(vector&& __x) // <-- Note double ampersands here { // NB: DR 675. this->clear(); this->swap(__x); return *this; } Does "Address of Address" make any sense? Why does it have two address operators instead of just one? aschepler This is C++11 code. In C++11, the && token can be used to mean an "rvalue reference". Lexseal Lin && is new in C++11. int&& a means "a" is an r-value reference. && is normally only used to declare a parameter of a function

Why can't I treat an array like a pointer in C?

和自甴很熟 提交于 2019-11-26 20:44:13
I see this question a lot on SO. Maybe not in so many words... but time and again there is confusion on how arrays are different from pointers. So I thought I would take a moment to Q&A a few points about this. For purposes of this Q&A we're going to assume a 32-bit system and the following have been declared: char * ptr = "hello"; char arr[10] = "hello"; int iarr[10] = {0}; Here's a list of questions that surmise the confusion I see on SO. As I see new ones I'll add to my list of Q&A (others feel free to as well, and correct me if you see any mistakes!) Isn't a pointer and an array basically

How can I reliably get an object's address when operator& is overloaded?

廉价感情. 提交于 2019-11-26 19:24:15
Consider the following program: struct ghost { // ghosts like to pretend that they don't exist ghost* operator&() const volatile { return 0; } }; int main() { ghost clyde; ghost* clydes_address = &clyde; // darn; that's not clyde's address :'( } How do I get clyde 's address? I'm looking for a solution that will work equally well for all types of objects. A C++03 solution would be nice, but I'm interested in C++11 solutions too. If possible, let's avoid any implementation-specific behavior. I am aware of C++11's std::addressof function template, but am not interested in using it here: I'd like

Is it possible to store the address of a label in a variable and use goto to jump to it?

家住魔仙堡 提交于 2019-11-26 19:01:34
问题 I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I'm not looking for "don't do that" as an answer, I understand your reservations, and understand why I am using them anyway). So far they have been fantastic, but I want to expand the functionality in such a way that requires me to essentially be able to store pointers to the labels, then go to them later. If this code worked, it would represent the type of

Access memory address in python

為{幸葍}努か 提交于 2019-11-26 18:52:15
My question is: How can I read the content of a memory address in python? example: ptr = id(7) I want to read the content of memory pointed by ptr. Thanks. Mark Tolonen Have a look at ctypes.string_at . Here's an example. It dumps the raw data structure of a Python 3 integer. Hopefully you're only doing this as an exercise. No reason to do this with pure Python. from ctypes import string_at from sys import getsizeof from binascii import hexlify a = 0x7fff print(hexlify(string_at(id(a), getsizeof(a)))) Output b'02000000d8191e1e01000000ff7f' In Python, you don't generally use pointers to access