الخميس، مايو 04، 2006

C# Pointers

el salamo 3alikom wa ra7mato Allah wa baraktoh,

how are you everybody? fine e7l

Pointers is one of many other programming language capabilities that every big system developer will need,
Inheritance, Virtual functions, and alot of keywords that when we read about years ago, we said
"structure! , that's making working more complex, i should put some extra text and a dot!"
"making a function private!!, i should not make it at all then!"
"pointers!, why should a have the variable address, i'm dealing with it's value!"

ok, years passed, and someday will come to recognize how important those things are.
Now, i'm gonna talk about how important are pointers and how are they in C#

Pointers:
Assuming a have an electronic connection, node "A" is connected to node "B", and node "C" is connected to node "A" too.

the value of "A" is calculated somehow,,, when representing these nodes as ByValue variables in ur code, then each time u gotta Update values of B and C to have the new value of A!
Update(ref bool B, ref bool C)
{
B = A;
C = A;
}
Good, but what if I've initialized B and C with the address of A ?
bool* B;
bool* C;
B = &A;
C = &A;

any changes to A gonna be reflected to automatically and seen through B and C
Pointers are not only used for dynamic memory allocation ( i think that has not become a matter in C#, as its default is dynamic by reference allocation for the majority of classes).

What do u think the action taken by the C# compiler when you write code as above ? ... pointers in Unsafe scope!!... you should specifiy that part of code to be unsafe, rick region :D
you could specify the scope of function, class, etc... by preceding it's declarator with the keyword 'unsafe'

unsafe public void func(...)
{
//use pointers asif u r in C++, only one restriction! which is...........
}
u have to initialize the pointer with address of some variable (no just 'new' address allocated for the variable)
//Error: int* ptr = new IntPtr(0);

//Correct:
int init = 0;
int* ptr = &init;
//then work with 'ptr' freely from here on, inside ur unsafe scope

u could make objects of classes contining that variables in scopes that shouldn't be unsafe (it's enough for the declaration scope only to be unsafe)

Does the garbage collector affect our pointer ?
Yes, as the variable you have assigned it's address to that pointer is of no more use, then the pointer is aloso of no more use :D
that's a good point, u don't have to 'delete' the pointer,

u may make it 'fixed' ... how ?
fixed (int* ptr = &init)
{
//ur dealing with 'ptr' here
}
i've not used 'fixed' and don't know alot of details about it, u can simply ask Dr.Google about it :D

after you have written ur unsafe code, u have to enable unsafe code compilation
Project > [project name] Properties...> 'Configuration properties' --> 'Allow unsafe code blocks'=True

that's it for now :D , wish it's useful :)

el salamo 3alikom wa ra7mato Allah

هناك تعليقان (2):

Asmaa Magdi يقول...

Salamo 3likom
Nice post (Y), but I didn't really understand what "unsafe" supposed to do??!! y3nee ok, ba7otaha when I use pointers and that's because ... ??!!

Mohammed A.Aziz يقول...

u know, u may make a pointer that points to any location in memory,,, and almost we get "allocation" error...
using pointers 'manually' is an unsafe usage, .NET uses pointers entirely (that is when u make a 'new' object) and manages memory, all handled by .NET

so, when u make a pointer, that's not made by .NET, then it's unsafe.

may be the fixed pointer could reveal how pointers are unsafe, cuz now you are telling .NET not to put the pointer into consedration in garbage collecting!