Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is address of char data not displayed?

People also ask

How do I print a char address?

Use static_cast<void*>(&b) to print it as a an address.

How do you declare a char value?

To declare a char variable in C++, we use the char keyword. This should be followed by the name of the variable. The variable can be initialized at the time of the declaration. The value of the variable should be enclosed within single quotes.

What is the function of char data type?

The char type is used to store single characters (letters, digits, symbols, etc...).

Does char work with numbers?

The CHAR data type stores any string of letters, numbers, and symbols. It can store single-byte and multibyte characters, based on the database locale. The CHARACTER data type is a synonym for CHAR.


When you are taking the address of b, you get char *. operator<< interprets that as a C string, and tries to print a character sequence instead of its address.

try cout << "address of char :" << (void *) &b << endl instead.

[EDIT] Like Tomek commented, a more proper cast to use in this case is static_cast, which is a safer alternative. Here is a version that uses it instead of the C-style cast:

cout << "address of char   :" << static_cast<void *>(&b) << endl;

There are 2 questions:

  • Why it does not print the address for the char:

Printing pointers will print the address for the int*and the string* but will not print the contents for char* as there is a special overload in operator<<. If you want the address then use: static_cast<const void *>(&c);

  • Why the address difference between the int and the string is 8

On your platform sizeof(int) is 4 and sizeof(char) is 1 so you really should ask why 8 not 5. The reason is that string is aligned on a 4-byte boundary. Machines work with words rather than bytes, and work faster if words are not therefore "split" a few bytes here and a few bytes there. This is called alignment

Your system probably aligns to 4-byte boundaries. If you had a 64-bit system with 64-bit integers the difference would be 16.

(Note: 64-bit system generally refers to the size of a pointer, not an int. So a 64-bit system with a 4-byte int would still have a difference of 8 as 4+1 = 5 but rounds up to 8. If sizeof(int) is 8 then 8+1 = 9 but this rounds up to 16)


When you stream the address of a char to an ostream, it interprets that as being the address of the first character of an ASCIIZ "C-style" string, and tries to print the presumed string. You don't have a NUL terminator, so the output will keep trying to read from memory until it happens to find one or the OS shuts it down for trying to read from an invalid address. All the garbage it scans over will be sent to your output.

You can probably get it to display the address you want by casting it, as in (void*)&b.

Re the offsets into the structure: you observed the string is placed at offset 8. This is probably because you have 32-bit ints, then an 8-bit char, then the compiler chooses to insert 3 more 8-bit chars so that the string object will be aligned at a 32-bit word boundary. Many CPUs/memory-architectures need pointers, ints etc. to be on word-size boundaries to perform efficient operations on them, and would otherwise have to do many more operations to read and combine multiple values from memory before being able to use the values in an operation. Depending on your system, it may be that every class object needs to start on a word boundary, or it may be that std::string in particular starts with a size_t, pointer or other type that requires such alignment.


Because when you pass a char* to std::ostream it will print the C-style (ie: char array, char*) string it points to.

Remember that "hello" is a char*.


The address of char is being treated as a nul-terminated string and is displaying the contents of that address, which is probably undefined, but in this case an empty string. If you cast the pointers to void *, you will get the results you desire.

The difference between something2 and something being 8 is due to aligned and ability of the compiler to decide for itself where in the stack the variables are declared.


For the second issue - the compiler by default will pad structure members. The default pad is to the sizeof(int), 4 bytes (on most architectures). This is why an int followed by a char will take 8 bytes in the structure, so the string member is at offset 8.

To disable padding, use #pragma pack(x), where x is the pad size in bytes.


Your syntax should be

cout << (void*) &b