Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 30 March 2013 - 03:13 AM

2- The same doubt about structures: if, else, for, etc (I think we call them structures). What are they? How do they work? Is there a source code?

Typically called 'statements' (if statement, while statement, etc...). Structures are something else.

They boil down to assembly language, and work like this in the CPU:
Line 54: if(x) JumpTo (line 55), otherwise jump to (line 58) 
Line 55:      stuff
Line 56:      more stuff
Line 57:      other stuff
Line 58: Back to normal
Except instead of jumping to 'lines', it's actually jumping to locations in memory when the next assembly instruction is.

The assembly has built-in commands/op-codes/instructions that the CPU provides in hardware (or in software from the OS). The instructions contain some simple stuff like addition, subtraction, binary AND, OR, XOR, etc... and a little bit of program flow, like if-then-goto.
 
jump-if-less-than
jump-if-greater-than
jump-if-equal-to

C++ if() statements are compiled down to these assembly instructions.
Same with for() and while() and other statements.

for(int x = 0; x < 10; x++) is basically:
0x00F133: set var 'x' to 10
0x00F134: if x is not less than 10, jump to 0x00F138
0x00F135:      <inside of the for() loop>
0x00F136:      set var 'x' to (x + 1)
0x00F137:      jump back to 0x00F134 again
0x00F138: ...code that is after the for() statement ends...

3- Is it possible to make a pointer point to a specific memory address?

In most situations it's not very useful, but in some low-level situations on specific hardware you might know for sure that at a certain address is a certain resource that is updated by the hardware or operating system. On PCs, however, our programs run in virtual memory where the addresses we see aren't the actual addresses in memory, because the OS is protecting our program from accidentally writing to areas it shouldn't be.

4- Is it possible to know what type of variable the memory address 0x00FF32 is containing? What other info I can get about this address?

Not in C++. Memory is memory. It's how you interpret the memory that matters, so your code needs to tell the computer how to interpret the memory.

Example:
int main()
{
    int myInt = 357;
    std::cout << myInt << std::endl;
    
    float *myFloat = (float*)(&myInt);
    std::cout << *myFloat << std::endl;
    
    return 0;
}
'myInt' is just a bunch of 1s and 0s. The data has no meaning outside of how int interprets the 1s and 0s.
If I pretend it's a float, the 1s and 0s are treated entirely differently when float interprets it.

#1Servant of the Lord

Posted 30 March 2013 - 03:11 AM

2- The same doubt about structures: if, else, for, etc (I think we call them structures). What are they? How do they work? Is there a source code?

Typically called 'statements' (if statement, while statement, etc...). Structures are something else.

They boil down to assembly language, and work like this in the CPU:

Line 54: if(x) JumpTo (line 55), otherwise jump to (line 58) 
Line 55:      stuff
Line 56:      more stuff
Line 57:      other stuff
Line 58: Back to normal

Except instead of jumping to 'lines', it's actually jumping to locations in memory when the next assembly instruction is.

The assembly has built-in commands/op-codes/instructions that the CPU provides in hardware (or in software from the OS). The instructions contain some simple stuff like addition, subtraction, binary AND, OR, XOR, etc... and a little bit of program flow, like if-then-goto.

C++ if() statements are compiled down to these assembly instructions.
Same with for() and while() and other statements.

for(int x = 0; x < 10; x++) is basically:

0x00F133: set var 'x' to 10
0x00F134: if x is not less than 10, jump to 0x00F138
0x00F135: <inside of the for() loop>
0x00F136: set var 'x' to (x + 1)
0x00F137: jump back to 0x00F134 again
0x00F138: ...code that is after the for() statement ends...

3- Is it possible to make a pointer point to a specific memory address?

In most situations it's not very useful, but in some low-level situations on specific hardware you might know for sure that at a certain address is a certain resource that is updated by the hardware or operating system. On PCs, however, our programs run in virtual memory where the addresses we see aren't the actual addresses in memory, because the OS is protecting our program from accidentally writing to areas it shouldn't be.

4- Is it possible to know what type of variable the memory address 0x00FF32 is containing? What other info I can get about this address?

Not in C++. Memory is memory. It's how you interpret the memory that matters, so your code needs to tell the computer how to interpret the memory.

Example:

int main()
{
    int myInt = 357;
    std::cout << myInt << std::endl;
    
    float *myFloat = (float*)(&myInt);
    std::cout << *myFloat << std::endl;
    
    return 0;
}

'myInt' is just a bunch of 1s and 0s. The data has no meaning outside of how int interprets the 1s and 0s.
If I pretend it's a float, the 1s and 0s are treated entirely differently when float interprets it.


PARTNERS