-3

I have a vector aVector. It starts at some memory address, let it be 0x00cff87f in this case.

I also have a double D.

Now, when the program accesses the D above, it accesses some other address, of course.

What I need is that when the program accesses D above, to be pointed to address 0x00cff87f, the start of that array, and take the first sizeof(double) bytes as a double.

I tried passing pointer to D to a function and switch it, but that just changed where that pointer was pointing at, once I exited the function, D remained unchanged.

Basically, I need some way to tell the program that four bytes of memory starting at 0x00cff87f are a double and that when I ask for a double named D to get me the number at that address.

I have an array in memory that needs to be decomposed to basic types, but instead of copying everything unnecessarily, I'd rather just tell the program where it already is.

How do I do that?

EDIT:

I have a vector of unsigned chars that I want to read into other types. Something that C# BinaryReader would do with MemoryStream. I don't know how to do it in c++. There are only fstreams, there isn't one that deals with (binary) files already in memory.

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • ... why would you want that? – spectras May 26 '17 at 22:16
  • There is no guarantee that will work as the compiler and link/loader would randomize the address of the code at run time so no two addresses guaranteed to be different on each run. You are inducing weirdness and undefined behaviour in that case. – t0mm13b May 26 '17 at 22:19
  • I have a vector of unsigned chars that I want to read into other types. Something that C# BinaryReader would do with MemoryStream. I don't know how to do it in c++. There are only fstreams, there isn't one that deals with (binary) files already in memory. – Karlovsky120 May 26 '17 at 22:20
  • @Karlovsky120 You were given all the information when you were asking this last time. Why did you ask again? – πάντα ῥεῖ May 26 '17 at 22:22
  • @Karlovsky120 Where did you get that data from? A socket or a file? – πάντα ῥεῖ May 26 '17 at 22:24
  • From a file. But I got 1024 of those from a single file and I don't want to process them when I'm processing file. I also don't want to store them back to disk just so I could read them. – Karlovsky120 May 26 '17 at 22:26
  • @Karlovsky120 Binary file formats may have the same endianess problems as data retrieved from the network. You should really get informed about that de-/serialization stuff. – πάντα ῥεῖ May 26 '17 at 22:29

1 Answers1

1
double *p = (double *) &aVector;

I'm not sure why you want to do it, but it seems very likely that there is a better way to do what you're trying to do, because breaking type safety and directly accessing memory can lead to lots of weird problems.

smead
  • 1,768
  • 15
  • 23
  • Hmm, that will work as is, but what if I want to do it in another function? Will I have to use ** pointers? – Karlovsky120 May 26 '17 at 22:18
  • No, just pass `p` to the other function. – smead May 26 '17 at 22:19
  • 1
    I really would suggest though that you share what exactly you're trying to do, because it sounds like you are going about it the wrong way. – smead May 26 '17 at 22:20
  • @smead That won't work properly in all cases. Read the duplicates. – πάντα ῥεῖ May 26 '17 at 22:26
  • @πάνταῥεῖ Yeah, but it's hard to know without more specific details of the use case. – smead May 26 '17 at 22:42
  • @smead Sure the question isn't very clear about that and very vague. That's probably the reason why it was downvoted. – πάντα ῥεῖ May 26 '17 at 22:44
  • I may be a little late to the party. When I asked the question, I was trying to write a parser for a binary data stream. I had a binary file that had to be read as a bunch of structure. Since I was trying to read files that I didn't create myself, I had no control over the structure of the file, all I could do is read it as is. I did edit this into the question, but I might've been too vague about it. – Karlovsky120 Aug 09 '19 at 19:43