0

I recently moved from windows to linux i was used to learn c++ with Visual-studio, now i try to write something in linux and it behaves different for example:

#include <iostream>
#include <iomainip>

int main(){
char a {'a'};
char* b { &a };
size_t count {16};

for(size_t i{};i < count;i++){
    std::cout << std::hex;
    std::cout << b << ": " // This line need to show me the pointer address but it 
    << *(b+=i)   << "   "  // output the value in linux 
    << *(b+=i+1) << "   "
    << *(b+=i+2) << "   "  // if i use &b it show me the address, but i remember that 
    << *(b+=i+3) << "   "  // for values not for pointers
    << std::endl;          // and *(b+=i) not change the address why?

}

return 0 ;

}

the out put is :

a: a
: X �
: B
: X �
: �
: Y �
: � �
��: �
: @
:
: �
: $ � �
: �
: � �
: � �
: � �

i just want to print memory to the console in format of " Address: Val1 Val2 Val3 Val4 "

  • 3
    You're causing undefined behavior. Each system and compiler is allowed to behave differently. It's not specified in the standard how your program should behave. Your program could even erase your disk and order pizza. – Thomas Sablik Oct 27 '20 at 12:26
  • 2
    `b` is only pointing at a single variable. Dereferencing `b+1`, `b+2`, etc is UB. – cigien Oct 27 '20 at 12:26
  • 2
    why do you expect incrementing `b` to have any meaning? What is the code supposed to do? It has ub on windows and linux btw – 463035818_is_not_an_ai Oct 27 '20 at 12:26
  • 1
    If you want adresses why are you dereferencing them? – Quimby Oct 27 '20 at 12:27
  • 1
    see: [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – underscore_d Oct 27 '20 at 12:28
  • "this line need to show me the pointer address" says who? It's a `char*`. The pointer should be pointing to a null-terminated character array, or the behaviour is undefined. – n. m. could be an AI Oct 27 '20 at 12:30
  • 1
    btw it is not clear what "Val1 Val2 Val3 Val4" should be in your ouput. You have a single `char` and one pointer to that, what are the 4 values you want to print? – 463035818_is_not_an_ai Oct 27 '20 at 12:34
  • @Quimby the format i want is " Address : val1 val2 val3 val4 " i start with show the address and then show the values. –  Oct 27 '20 at 12:38
  • You are probably trying to do something like: https://wandbox.org/permlink/ISxaF4w6Bz9hmQJJ – Thomas Sablik Oct 27 '20 at 12:38
  • just to see what values in this memory location –  Oct 27 '20 at 12:39
  • @underscore_d thenks it help –  Oct 27 '20 at 12:43
  • What bytes are in memory isn't actually useful to a majority of programmers, though, who really care about the numbers/strings/objects/whatever those bytes represent. And even if you had a clear statement of what you want this code to do, it isn't evident here. – underscore_d Oct 27 '20 at 12:44

1 Answers1

2

You are probably trying to do some like:

#include <iostream>
#include <iomanip>

int main(){
    char a {'a'};
    char* b { &a };
    std::cout << static_cast<void*>(b) << ": " << *b;
}

// Output: 0x7fff41d27787: a

You have to cast a char pointer otherwise cout will handle it as a C-string. Dereferencing *(b + i) causes undefined behavior for i > 0.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62