Few misc questions

Joined
Aug 6, 2009
Messages
2,148
Reaction score
439
After viewing lectures I got few misc questions, plus some not related to it.

1. Pointers
Richard Buckland in his lecture about pointers noted that when you pass something to a function from main function, it get's copied in memory and technically you with with copy of that variable/object/etc inside function. With pointers you can pass pointer and work with the object/variable/etc from main function being inside sub function. Then he mentioned that sometimes computer will decide wheather or not to pass a copy or pass an address to a function.

Question. How can I determine wheather it's a copy or actual object? How to prevent it from passing original instead of making copy?

Video: YouTube - 21:Everything u need 2 know about pointers -Richard Buckland

2. Pointers in C#
In C# when you pass an object like following
Code:
MyServer serverObj = new Server();
doSomething(serverObj);
Technically this is pointer, correct?

3. Pointers again
Let's say I have programs Foo1 and Foo2. Foo1 does one thing, Foo2 does another thing. And let's say they both share same class Bar. Since both programs are running on same computer, can I pass address of Foo1's Bar object address to Foo2 program, so then Foo2 can interact with Foo1's Bar object directly from memory.
Would such thing work?

4. XORing
Okay so when you xor byte with another byte
Code:
byte output = input ^ 0xAC;
Technically 0xAC byte can be a number up to 255. What if I pass a number instead of byte and number is bigger than 255? Hows xoring done in that way?
input 10111011
0xAC 10101100
output 00010111
That works but then xoring by two bytes
input 10111011
0x01C5 111000101
output ??????
 
Last edited:
1. He probably means compiler optimization.
Let's say I have a Customer struct, and a function (useless function but as an example):
PHP:
int GetCustomerAge(Customer c)
{
    return c.age;
}

This means that when you call GetCustomerAge, a copy of c will be made and the function will return [copied c].age. Imagine your Customer struct holds a lot of data. Then it's a lot of memory wasted.
A "smart compiler" would notice c isn't being modified in the function, thus it would optimize it this way:

PHP:
int GetCustomerAge(Customer& c)
{
    return c.age;
}

Or in C-style (no references):
PHP:
int GetCustomerAge(Customer* c)
{
    return c->age;
}

2. I don't know C#

3. You can, if you make a DLL, then you can access the hooked process' memory easily.
If not, you can do something like this (when using windows API):

Prog1.cpp
PHP:
#include <stdlib.h>
#include <windows.h>

int main(int argc, char* argv[])
{
	int* MyVar = (int*)malloc(sizeof(int));
	printf("MyVar = ");
	scanf("%d", MyVar);
	printf("Process id: %d\nMyVar address: %x", GetCurrentProcessId(), MyVar);
	system("pause");
	return 0;
}

Prog2.cpp
PHP:
#include <stdlib.h>
#include <windows.h>

int main(int argc, char* argv[])
{
	unsigned int address = NULL, processId = NULL;
	int MyVar;
	printf("Process id? ");
	scanf("%d", &processId);
	printf("Address of myVar? ");
	scanf("%x", &address);
	HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, NULL, processId);
	VirtualProtectEx(process, (LPVOID)address, sizeof(int), PAGE_READWRITE, NULL);
	ReadProcessMemory(process, (LPCVOID)address, (LPVOID)&MyVar, sizeof(int), NULL);
	printf("myVar = %d\n", MyVar);
	system("pause");
	return 0;
}

4.
Code:
Input = 10111011
0x1C5 = 111000101

It's like with decimal numbers, when you say 2, 02, 00002, you're talking about the same number.

So
Code:
Input = [B][COLOR="Red"]0[/COLOR][/B]10111011
0x1C5 = 111000101
->xor = 101111110
 
After viewing lectures I got few misc questions, plus some not related to it.

1. Pointers
Richard Buckland in his lecture about pointers noted that when you pass something to a function from main function, it get's copied in memory and technically you with with copy of that variable/object/etc inside function. With pointers you can pass pointer and work with the object/variable/etc from main function being inside sub function. Then he mentioned that sometimes computer will decide wheather or not to pass a copy or pass an address to a function.

Question. How can I determine wheather it's a copy or actual object? How to prevent it from passing original instead of making copy?

Video: YouTube - 21:Everything u need 2 know about pointers -Richard Buckland

2. Pointers in C#
In C# when you pass an object like following
Code:
MyServer serverObj = new Server();
doSomething(serverObj);
Technically this is pointer, correct?

3. Pointers again
Let's say I have programs Foo1 and Foo2. Foo1 does one thing, Foo2 does another thing. And let's say they both share same class Bar. Since both programs are running on same computer, can I pass address of Foo1's Bar object address to Foo2 program, so then Foo2 can interact with Foo1's Bar object directly from memory.
Would such thing work?

4. XORing
Okay so when you xor byte with another byte
Code:
byte output = input ^ 0xAC;
Technically 0xAC byte can be a number up to 255. What if I pass a number instead of byte and number is bigger than 255? Hows xoring done in that way?
input 10111011
0xAC 10101100
output 00010111
That works but then xoring by two bytes
input 10111011
0x01C5 111000101
output ??????

If you want to understand pointers, you cant understand them from the high level point (well you do, but not fully). A pointer its just an address to an abstract object of unknowned size. You define the size by limitating the pointer address.

If you have a pointer wich address is 0x400000, and you know there is a variable inside of 30 byte sizes, then the end of pointer will be 0x400000+30.

Pointers as an individual definition doesnt exist. A pointer by itself doesnt exist neither. In old langs you dont talk about pointers, you talk about offsets (address). Pointers definition was made for high level langs as a good method to explain that you actually "point" to some data memory.

When you pass a pointer to a function the pointer is stored in stack (stack works as normal memory, only that its rehusable). So you can use a pointer to stack; The "pointer" word have no limits, you can say Pointer to stack, pointer to function, pointer to class, struct, object, string, etc, etc.

Also pointers are releated to memory space in the program, they cant be shared among other programs by normal resources. Your example of Foo1 and Foo2 wont work. Each program you execute its mapped by OS into memory space and have a limit of 07FFEFFFh memory size (if i remember well). So if you want to share memory resources between programs you must use a call to some API that dispatch execution to kernel land and manage to share memory resources between 2 programs.

About XOR, if an operand is bigger than the other then the bits extra are ignored by CPU example (That means extra bytes can be seted to 0):

11111111 (FFh)
[1111]11111111 (FFFh)
-----------------------
[1111]00000000

Bits between [] where not used, or if you set to 0 its the same.

[0000]11111111 (FFh)
[1111]11111111 (FFFh)
-----------------------
[1111]00000000

Hope ive helped you.
 
Last edited:
There is the like/share function, maybe youre bugged?

Ill clear you out some stuff about c#.

There are reference types and value types. Any class, interface, array and delegate are REFERENCES. This means that no data is stored in the variable you create, just a reference to the object (i assume it works like a number). Value types are ints, chars, bytes etc, with which variables you directly access the DATA.

So, what happens when you pass an argument to a method/function? The data (the value) the variable has is copied and used inside the block. So, when you copy the reference, it might be a different reference but it references the SAME object. So anything you do with it will affect the object outside the block. Whereas when you copy the value of a value type, whatever you do it wont affect the variable outside the method/function (they are different variables). Thats why pointers are here: they pass a number which says where the variable's first byte is IN MEMORY. So you can access its value by writing/reading to this block of memory. In c# use pointers in unsafe context.

NOTE: reference types memory is managed by the garbage collector, so you cant (and theres no point to) mess up with its memory. Tho it must be possible to pass references but idk how if they are in defferent programms without one being dll of the other. Some short of shared memory for sure, but for objects (microsoft must have some kind of support for this). I suppose youll get an object and then cast it to whatever you want.

As for your fourth point, tell me, is it possible for the cpu to do operations using different types of variables? Like xoring 32 bits with 8 bits? No. Or is it possible to add a floating point to an integer? No. Its like trying to add meters to kilos :blink:. So, the compliler implicity converts the variables to int (or double for floating point arithmetic). So if you do this:
Code:
byte b = 0xFF;
int i = 0xFFFFFF00;
int r1 = i ^ b;
byte r2 = (byte)(i ^ b); //i think this cast is neccesary, not 100% sure

Its more or less the same to this:
Code:
byte b = 0xFF;
int i = 0xFFFFFF00;
int r1 = i ^ (int)b; //where (int)b = 0x000000FF
byte r2 = (byte)(i ^ (int)b);

So the r1 is obviously 0xFFFFFFFF which is -1 if you consider it as signed (int), whereas r2 is 0xFF which is 255 (as byte, or -1 as sbyte).

PS: read the CSharp Language Specification by Microsoft, ive never dfound somethings missing from there tho its not exactly easy to read/understand.
 
Last edited:
Another question.

Let's say in Main.cs I have two objects MyServer and MyLog.
MyServer handles new connections. Each new connection creates new MyConnection object and adds it to list inside server object. Now how would I access MyLog object from MyConnection?
 
Back