1

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 = new things;
    ptr->bad = 3;
    ptr->good = 7;
    std::cout << *(&(ptr->good)) <<" " << &(ptr->good) << std::endl;
    std::cout << "ptr also print same address = " << ptr << std::endl;
    std::cout << "But *ptr does not print 7 and gives compile time error. Why ?" << *ptr << std::endl;
    return 0;
}
Unimportant
  • 2,076
  • 14
  • 19
pythonRcpp
  • 2,042
  • 6
  • 26
  • 48
  • `*&some_type` should always give you `some_type` as long as you do not have any wacky operator overloading. – NathanOliver Jan 06 '17 at 13:30
  • If you dereference a pointer you get the value of what the pointer is pointing to. If the pointer is pointing to a structure the "value" is the *structure*. – Some programmer dude Jan 06 '17 at 13:31
  • Also, while the address of the first member *may* be the same as the address of the structure, a pointer to the structure is *not* a pointer to its first member. The types are different to begin with. – Some programmer dude Jan 06 '17 at 13:33
  • 1
    Put a snippet of the relevant code in the question, not a link. – jacknad Jan 06 '17 at 13:34
  • You must do a cast of the *pointer to Struct*, to a *pointer to the first element* of the struct so the compiler knows what size and alignment to use to collect the value from memory – Fernando Pérez Jan 06 '17 at 13:41

4 Answers4

1

*ptr returns to you an instance of type of things, for which there is no operator << defined, hence the compile-time error.

A struct is not the same as an array. That is, it doesn't necessarily decay to a pointer to its first element. The compiler, in fact, is free to (and often does) insert padding in a struct so that it aligns to certain byte boundaries. So even if a struct could decay in the same way as an array (bad idea), simply printing it would not guarantee printing of the first element!

I mean a C-Style array like int[]

These boundaries are implementation-dependent and can often be controlled in some manner via preprocessor statements like pragma pack

AndyG
  • 39,700
  • 8
  • 109
  • 143
1

Try any of these:

#include <iostream>
#include <cstring>

struct things{
    int good;
    int bad;
};

int main()
{
    things *ptr = new things;
    ptr->bad = 3;
    ptr->good = 7;

    std::cout <<  *(int*)ptr << std::endl;
    std::cout <<  *reinterpret_cast<int*>(ptr) << std::endl;

    int* p = reinterpret_cast<int*>(ptr);
    std::cout <<  *p << std::endl;
    return 0;
}
Laszlo
  • 769
  • 9
  • 19
0

You can do a cast of the pointer to Struct, to a pointer to the first element of the struct so the compiler knows what size and alignment to use to collect the value from memory. If you want a "clean" cast, you can consider converting it to "VOID pointer" first.
_ (Struct*) to (VOID*) to (FirstElem*) _
Also see: Pointers in Stackoverflow
Hope it helps!!

  • 1
    casting from `X*` to `void*` and then back to anything other than `X*` makes a program ill-formed. – Richard Hodges Jan 06 '17 at 13:58
  • @RichardHodges Not if you handle it correctly. Even Microsoft uses it in their [Driver Development Framework Code](https://github.com/Microsoft/Windows-driver-samples) – Fernando Pérez Jan 06 '17 at 14:06
  • @Fernando: Because Microsoft is doing something in code does not mean it's correct. In general this violates strict aliasing rules. Edit: I'm sure Microsoft is handling it correctly somehow, via enforcing no padding in the struct before the first element, it's just not a good recommendation to give to OP. – AndyG Jan 06 '17 at 14:11
  • "Even Microsoft uses it" - a sure signal that it's wrong... ;-) – Richard Hodges Jan 06 '17 at 14:26
  • @RichardHodges hahah, yes, they are quite the patch-wizards. :) – Fernando Pérez Jan 06 '17 at 14:33
  • @AndyG Im sure his question is purely academical, because I see no scenario that would strictly require such computations, I just provided him with a solution. Of course no such operations should be of consistent use. Now i feel curious about how it would look on some IR, maybe LLVM's clang's IR. – Fernando Pérez Jan 06 '17 at 14:36
0

I found out that address of first element of structure is same as the address of structure.

Wherever you found this out, it wasn't the c++ standard. It's an incorrect assumption in the general case.

There is nothing but misery and pain for you if you continue down this path.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142