dereference

Dereference a structure to get value of first member

浪子不回头ぞ 提交于 2020-01-05 07:12:13
问题 I found out that address of first element of structure is same as the address of structure. But dereferencing address of structure doesn't return me value of first data member. However dereferencing address of first data member does return it's value. eg. Address of structure=100, address of first element of structure is also 100. Now dereferencing should work in the same way on both. Code: #include <iostream> #include <cstring> struct things{ int good; int bad; }; int main() { things *ptr =

Dereference a structure to get value of first member

两盒软妹~` 提交于 2020-01-05 07:11:40
问题 I found out that address of first element of structure is same as the address of structure. But dereferencing address of structure doesn't return me value of first data member. However dereferencing address of first data member does return it's value. eg. Address of structure=100, address of first element of structure is also 100. Now dereferencing should work in the same way on both. Code: #include <iostream> #include <cstring> struct things{ int good; int bad; }; int main() { things *ptr =

Why can't one use scope resolution with member pointer dereference?

馋奶兔 提交于 2020-01-05 06:52:22
问题 Consider a simple example: struct FooParent { virtual void bar() { } }; struct Foo: FooParent { void bar() { } }; int main() { Foo foo; void (Foo::*foo_member)() = &FooParent::bar; //(foo.*FooParent::foo_member)(); foo.FooParent::bar(); } As you can see one can use scope resolution on the foo object when calling bar member function while there is no way to explicitly declare the scope for member function pointer. I accept that the syntax should be prohibited when using ->* as the operator can

How to use a 'subroutine reference' as a hash key

。_饼干妹妹 提交于 2020-01-04 16:57:11
问题 In Perl, I'm learning how to dereference 'subroutine references'. But I can't seem to use a subroutine reference as a hash 'key'. In the following sample code, I can create a reference to a subroutine ($subref) and then dereference it to run the subroutine (&$subref) I can use the reference as a hash 'value' and then easily dereference that But I cannot figure out how to use the reference as a hash 'key'. When I pull the key out of the hash, Perl interprets the key as a string value (not a

Address of a dereferenced InputIterator? The case of istream_iterator

喜欢而已 提交于 2020-01-03 05:34:06
问题 I have a legacy code in which the interface is defined for pointer. I am trying to adapt some functions to take iterators, e.g. forward iterators. Is one allowed to take the address of the element dereferenced by InputIterator such as istream_iterator ? The result is a temporary and has to be is somewhere in memory for the life of the call, but I am not sure. The following example uses double , but the type can be more complicated (large). #include<iostream> #include<iterator> #include

Why is calling non virtual member function on deleted pointer an undefined behavior?

那年仲夏 提交于 2019-12-29 01:43:25
问题 As, the title says: Why is calling non virtual member function on deleted pointer an undefined behavior? Note the Question does not ask if it is an Undefined Behavior, it asks Why it is undefined behavior. Consider the following program : #include<iostream> class Myclass { //int i public: void doSomething() { std::cout<<"Inside doSomething"; //i = 10; } }; int main() { Myclass *ptr = new Myclass; delete ptr; ptr->doSomething(); return 0; } In the above code, the compiler does not actually

C++ - Get value of a particular memory address

六月ゝ 毕业季﹏ 提交于 2019-12-28 05:55:08
问题 I was wondering whether it is possible to do something like this: unsigned int address = 0x0001FBDC; // Random address :P int value = *address; // Dereference of address Meaning, is it possible to get the value of a particular address in memory ? Thanks 回答1: You can and should write it like this: #include <cstdint> uintptr_t p = 0x0001FBDC; int value = *reinterpret_cast<int *>(p); Note that unless there is some guarantee that p points to an integer, this is undefined behaviour. A standard

Multiple Reference and Dereference in C

早过忘川 提交于 2019-12-25 18:28:37
问题 Can somebody clealry explain me the concept behind multiple reference and dereference ? why does the following program gives output as 'h' ? int main() { char *ptr = "hello"; printf("%c\n", *&*&*ptr); getchar(); return 0; } and not this , instead it produces 'd' ? int main() { char *ptr = "hello"; printf("%c\n", *&*&ptr); getchar(); return 0; } I read that consecutive use of '*' and '&' cancels each other but this explanation does not provide the reason behind two different outputs generated

Why dereferencing the main function does not show memory content?

半腔热情 提交于 2019-12-24 22:04:44
问题 I tried to get the memory content of the address pointed by the function names [both abc() and main()], but both printf() inside a function give same output, although I dereference the address pointed by the function name in the second printf() inside each function. Why is that? Why the memory addresses are printed by *(abc) and *(main) instead of the contents? #include<stdio.h> void abc(){ printf("\n%p \n ", abc); printf("\n%#x\n ",*(abc)); } void main(){ abc(); printf("\n%p \n ", main);

4 different ways of using 2 D arrays with pointers , which is right? An explanation would help a lot

我怕爱的太早我们不能终老 提交于 2019-12-24 20:15:40
问题 The four programs below input a 2 D array and then print it out. The first one prints out garbage values and also gives out a few warnings (Which I did not understand). The 2nd one works correctly , also it kinda makes sense as I believed that a 2 D array is stored linearly in memory. The 3rd one works correctly , but I have no idea why it works. The 4th one works as well. So it would be of great help if someone could explain how each of the methods work , cheers I'm afraid my understanding