-1

I have the following lines of code: (where a is int64_t* and i is a simple int counter)

   uintptr_t p = *a + (i * 4);
   int64_t value = *reinterpret_cast<int64_t *>(p); //***

I have followed the following stack overflow post to get this syntax: C++ - Get value of a particular memory address

The problem is I keep getting a segmentation fault on the indicated line. I think this may have something to do with pointer arithmetic but I am not completely sure. Does anyone know why I could be getting a seg fault here?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
cosmicluna
  • 539
  • 1
  • 5
  • 16
  • Obviously the calculated address is wrong. – Henri Menke Jul 22 '18 at 22:05
  • I'm not so sure thats obvious as I checked and it is the desired address. @HenriMenke – cosmicluna Jul 22 '18 at 22:06
  • Is the value stored at this address of type `int64_t`? It's either the wrong address or the wrong type. Otherwise it should work. – Henri Menke Jul 22 '18 at 22:07
  • 1
    What does `a` actually point to? –  Jul 22 '18 at 22:08
  • How do you know that there is an int in this address? – Dennis Vash Jul 22 '18 at 22:09
  • @NeilButterworth a is the address of the first of n 64 bit words which are ints. Here I am just trying to access the first one and get a set fault right away – cosmicluna Jul 22 '18 at 22:10
  • "Here I am just trying to access the first one " - I am obviously missing something - if `a` is a `int64_t*`, what is wrong with `*a`? –  Jul 22 '18 at 22:15
  • 1
    i think I share others thoughts when i say 'What are you actually trying to do?' – pm100 Jul 22 '18 at 22:29
  • 2
    maybe u mean `p = a + (i * 4)` – pm100 Jul 22 '18 at 22:30
  • 1
    i*4 seems really suspicious. Did you know that int64_t's are 8 bytes long? Maybe you should use sizeof(int64_t)? – Wyck Jul 23 '18 at 00:48
  • @Wyck although if the intent was to add to `a` then it would be `a + i`. And all of this will need a reinterpret cast to `uintptr_t`. – M.M Jul 23 '18 at 01:06
  • _a is the address of the first of n 64 bit words which are ints. Here I am just trying to access the first one_ So did you just mean: `value = a[i]` – Wyck Jul 23 '18 at 03:42

1 Answers1

1

You probably don't want to dereference a in your first line of code.

Also please keep in mind what §3.10/10 says about aliasing:

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

the dynamic type of the object,

a cv-qualified version of the dynamic type of the object,

a type similar (as defined in 4.4) to the dynamic type of the object,

a type that is the signed or unsigned type corresponding to the dynamic type of the object,

a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object, an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),

a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,

a char or unsigned char type.

std::byte was added to that list with C++17.

Swordfish
  • 12,971
  • 3
  • 21
  • 43