# Help on VS debug

Mark44
Mentor
Do you mean when I do cout << *(pInt + index), compiler know each integer is 4 byte so it knows jump by 4 every time index++ and display correctly.
The compiler always knows the size of an int, a char, a float, and so on.
But compiler knows characters are ONE BYTE. So when I do Cr[ind] = *(pChar + ind) will only go to the next location. Since the content of memory is 0x31 0x00 0x00 0x00 0x32 0x00 0x00... So it read as {1, 0, 0, 0, 2}?( as shown in the first 5 highlighted bytes).
In C or C++,
Pointer arithmetic is scaled by the size of the type pointed to.

This means that the result of any valid arithmetic expression involving a pointer variable will depend completely on the type of thing being pointed to.

Suppose that pChar, pShort, pFloat, and pDouble are all declared as pointers to the type that is part of their name. I.e., char* pChar; and so on. All four types being pointed to are different sizes: 1 byte, 2 bytes, 4 bytes, and 8 bytes, respectively.
C++:
int Arr[] = {1, 2, 3, 4, 5, 6}
char* pChar = reinterpret_cast<char*>(Arr;)
short* pShort = reinterpret_cast<short*>(Arr);
float* pFloat = reinterpret_cast<float*>(Arr);
double* pDouble = reinterpret_cast<double*>(Arr);
Notice that I have to include a cast on each of the definition statements above. The compiler would flag each line with an error without the cast because I can't assign the address of an int to, say, a float pointer.

For the sake of argument, suppose that the array starts at location 0x1000. (That's not where it will be, but bear with me.)
Consider the expressions pChar + 1, pShort + 1, pFloat + 1, pDouble + 1.
The value of pChar + 1 would be 0x1001.
The value of pShort + 1 would be 0x1002.
The value of pFloat + 1 would be 0x1004.
The value of pDouble + 1 would be 0x1008.

This is related to what pbuk was talking about in post #23 above. If you add 1 to a pointer, the resulting address will be higher than the value in the pointer by as many bytes as the type being pointed to.
In other words, Pointer arithmetic is scaled by the type being pointed to.

The same idea holds if you add any integer value to a pointer, subtract any integer value from a pointer, of subtract one pointer from another (same types of pointers).
But, when I do cout << (pChar + 4*ind); it will not display the address like cout << (pInt + index).
They are different. Still missing something.
With cout <<, pChar is considered to be a string, and pChar + 4*ind is likely the address of a null character or a non-printing ASCII code, so no output. When the debugger is running, you can evaluate any expression that involves variables that are in scope using the Immediate window.
Debug --> Window --> Immediate
Just type the expression you want to evaluate, and its value will be shown in the window.

Last edited:
yungman
The compiler always knows the size of an int, a char, a float, and so on.
In C or C++,
Pointer arithmetic is scaled by the size of the type pointed to.

This means that the result of any valid arithmetic expression involving a pointer variable will depend completely on the type of thing being pointed to.

Suppose that pChar, pShort, pFloat, and pDouble are all declared as pointers to the type that is part of their name. I.e., char* pChar; and so on. All four types being pointed to are different sizes: 1 byte, 2 bytes, 4 bytes, and 8 bytes, respectively.
C++:
int Arr[] = {1, 2, 3, 4, 5, 6}
char* pChar = reinterpret_cast<char*>(Arr;)
short* pShort = reinterpret_cast<short*>(Arr);
float* pFloat = reinterpret_cast<float*>(Arr);
double* pDouble = reinterpret_cast<double*>(Arr);
Notice that I have to include a cast on each of the definition statements above. The compiler would flag each line with an error without the cast because I can't assign the address of an int to, say, a float pointer.

For the sake of argument, suppose that the array starts at location 0x1000. (That's not where it will be, but bear with me.)
Consider the expressions pChar + 1, pShort + 1, pFloat + 1, pDouble + 1.
The value of pChar + 1 would be 0x1001.
The value of pShort + 1 would be 0x1002.
The value of pFloat + 1 would be 0x1004.
The value of pDouble + 1 would be 0x1008.
Now I got this one, that when putting +1 to the pointer, it depends on the size of the variable type that pointer is for. eg, for pInt, each number is 4 bytes, so (pInt + 1) = 0x1000 + 0x4 = 0x1004. (pDouble + 1) = 0x1000 + 0x8 = 0x1008.
Like in my case pChar = reinterpret<char*>(integer), in order point to the next integer:
Cr[ind] = *(pChar + ind*
sizeof(Cr[0]))

This is related to what pbuk was talking about in post #23 above. If you add 1 to a pointer, the resulting address will be higher than the value in the pointer by as many bytes as the type being pointed to.
In other words, Pointer arithmetic is scaled by the type being pointed to.

The same idea holds if you add any integer value to a pointer, subtract any integer value from a pointer, of subtract one pointer from another (same types of pointers).

With cout <<, pChar is considered to be a string, and pChar + 4*ind is likely the address of a null character or a non-printing ASCII code, so no output. When the debugger is running, you can evaluate any expression that involves variables that are in scope using the Immediate window.
But pCar = pInt = 0x1000 as in your example. We know in integer array
int Cw[]={0x31, 0x32, 0x33, 0x34, 0x35}. So if you want pChar to point to the next integer 0x32, pChar has to equal to 0x1004. It is not a null character.

It should be the same as cout << (pInt + 1) where (pInt + 1) = (0x1000 + 0x4) = 0x1004. same as above.

Debug --> Window --> Immediate
Just type the expression you want to evaluate, and its value will be shown in the window.
I follow Debug--> Window --> Immediate, I have blank window when I step through the program. What am I missing?

I think I have pretty good idea of reinterpret_cast and binary files......at least to the expectation of the chapter 12. I think it's time to move onto the random file, the seekp, seekg, tellp, tellg to finish up the chapter.

Next chapter is Classes, then More Classes and finally Inheritance and Polymorphism. Can you tell me which one is tricky and difficult so I can know to read slower and be more careful? I get used to the earlier chapters and went through very fast, too fast the speed for difficult chapters. So far, the two parts that are hard are pointers and binary files with reinterpret_cast. But what I learned on these two with help from you and others are quite a bit deeper than in the book already. Gaddis really doesn't go very deep into these two subjects. The professor in my grandson's C++ class SKIPPED pointers completely!! No wonder he think C++ is not hard. he never get to chapter 12 advance files, binary files and all that. I am planning to finish all 15 chapters of Gaddis. You think that's enough as a start into C++.

After C++, I am thinking about learning something more practical, something like say graphics, something to do with windows etc. I have not make up my mind yet. I don't want to keep going at C++ unless I can do something with it. So far, it's like all I can see is displaying something in cmd mode, nothing of real life use. If C++ can get me there, I don't mind learning deeper. Any advice?

thanks

Last edited by a moderator:
Staff Emeritus
I follow Debug--> Window --> Immediate, I have blank window when I step through the program. What am I missing?
The manual.

Staff Emeritus
After C++, I am thinking about learning something more practical, something like say graphics, something to do with windows etc
If you want to interface with Windows, you need to use the System Developers Kit, which while technically language agnostic, tends to fit best with C and derivatives. Technically, this is language-agnostic, but C and its derivatives were what the developers had in mind.

Mark44
Mentor
But, when I do cout << (pChar + 4*ind); it will not display the address like cout << (pInt + index).
When I replied earlier, you didn't have (or I didn't see) any additional context.
Here's a short program that supplies some context.
C++:
#include <iostream>
using std::cout;

int main()
{
int Arr[] = { 1, 2, 3, 4, 5 };
char* pChar = NULL;

pChar = reinterpret_cast<char*>(Arr);
for (int idx = 0; idx < 5; idx++)
{
cout << pChar + 4 * idx << "\n";
}
}
pChar does contain the address of the array Arr. In the first iteration of the loop, the contents of the memory at location in pChar are the bytes 0x1 0x00 0x00 0x00. The next four bytes contain 0x2 0x0 0x0 0x0.
Since pChar's type is char*, the stream insertion operator << will treat the bytes as a string of characters, which means that it will attempt to print the characters starting from that address until it hits a null character. The character with ASCII code 1 (0x1) gets displayed as a small happy face, not as a normal printing character. ASCII 2 shows up as a different happy face, and ASCII 3 and ASCII 4 show up as the symbols for card suits.
cout << will not display an address if the type of the address is char *.

If you want to display the addresses, you need to cast the pointer expression to something other than char*.

Either of these works for the output statement in the example code above.
Old-style cast:
C++:
cout << (void*)(pChar + 4 * idx) << "\n";
or
reinterpret_cast:
C++:
cout << reinterpret_cast<void*>(pChar + 4 * idx) << "\n";
Don't ask about the void* bit -- I don't think you're ready for an explanation. Just take it as something that works.
I follow Debug--> Window --> Immediate, I have blank window when I step through the program. What am I missing?
You glossed over what I wrote, and didn't read it all. Here is from post #26, with underline added.
Debug --> Window --> Immediate
Just type the expression you want to evaluate, and its value will be shown in the window.
If you try something out that one of us here recommends, and it doesn't seem to work, go back to what we wrote, and read it again.
There have been many instances in your threads where you glossed over something that one of us wrote and missed something important.

.......................... When the debugger is running, you can evaluate any expression that involves variables that are in scope using the Immediate window.
Debug --> Window --> Immediate
Just type the expression you want to evaluate, and its value will be shown in the window.
I thought you mean to type in the program, now I know type in the Immediate window.

Thanks

Mark44
Mentor
I thought you mean to type in the program, now I know type in the Immediate window.
If you think about this a minute, it doesn't make any sense. To be using the debugger, you must already have typed in the program. Plus, I said the type the expression you want to evaluate. That doesn't mean "type the program."

If you think about this a minute, it doesn't make any sense. To be using the debugger, you must already have typed in the program. Plus, I said the type the expression you want to evaluate. That doesn't mean "type the program."
Yeh, but ALL the windows opened in debugger were just show what's in it, It's NOT for me to do anything. Don't blame me thinking not to type in the window. I did type in the program and tried. It's not as if I don't read your suggestion.