6

I am learning for my CPP exam and one of the questions will be like this: "How to get variable address and are there any variables whose address cannot be obtained"?

So first one is easy, you just use "&" operator, but are there any variables (mind you that question only concerns variables!) whose address cannot be accessed with ampersand?

Any help would be appreciated

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Derek Johnson
  • 717
  • 1
  • 5
  • 9

3 Answers3

10

are there any variables whose address cannot be obtained?

You cannot get addresses of member variables of structs that are bit-fields.

From the C++11 Standard:

The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
5

but are there any variables (mind you that question only concerns variables!) whose address cannot be accessed with ampersand?

I think the question in your content is different from the one from the title. I assume the one in your content is what you want.

There are variables whose address can't be obtained by ampersand, because you can overload that operator.

The code below, &a won't give you the address of a.

#include <iostream>

struct foo {
    int operator &() {
        return 900;
    }
};

int main() {
    foo a;
    std::cout << (&a) << "\n";
}

NOTE: Such variable's address can be obtained by other methods. Basically the principle is erasing the type so that the overloaded operator has no effect. std::addressof implemented this functionality.

llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • 1
    [`std::addressof`](http://en.cppreference.com/w/cpp/memory/addressof) gets around this. – NathanOliver Feb 08 '18 at 18:58
  • 1
    This answer is technically right, because the question asks specifically about `operator&` *("whose address cannot be accessed with ampersand?")*. But you can use [`std::addressof`](http://en.cppreference.com/w/cpp/memory/addressof) in that case. – François Andrieux Feb 08 '18 at 18:58
  • @NathanOliver Note in OP's description in the **content**, it's "**cannot be accessed with ampersand**". – llllllllll Feb 08 '18 at 18:59
3

So as an example. The smallest thing you can address in C++ is a byte, and so attempting to access for example any of the 1 bit uint8_t's inside this bitField is not legal.

#include <iostream>
#include <cstdint>

struct bitField {
    uint8_t n0 : 1;
    uint8_t n1 : 1;
    uint8_t n2 : 1;
    uint8_t n3 : 1;
    uint8_t n4 : 1;
    uint8_t n5 : 1;
    uint8_t n6 : 1;
    uint8_t n7 : 1;
};

int main() {
    bitField example;

    // Can address the whole struct
    std::cout << &example << '\n'; // FINE, addresses a byte

    // Can not address for example n4 directly
    std::cout << &example.n4; // ERROR, Can not address a bit

    // Printing it's value is fine though
    std::cout << example.n4 << '\n'; // FINE, not getting address

    return 0;
}

As TheDude mentioned in the comment section however, the STL has a class std::bitset<N> which offers a workaround for this. It basically wraps an array of bools. Still, the end result is indexing bytes, not bits.

Carl
  • 2,057
  • 1
  • 15
  • 20