okay I understand, the title sounds contradictory, but trust me, this concept exists.
So first, why pointer? the answer is simple, if you have to return multiple values from a function, the only way you can do it is, through pointer (noodle over it).
This can be done two ways, one is having the function return the pointer. Like...
int * ReturnSortedInteger(int *arrToSort).
Now the other way to do this is....
void ReturnSortedInteger(int *arrToSort, int *& SortedIntegerArray)...
Sunday, April 11, 2010
Wednesday, April 7, 2010
Access violation....
So in my last blog, I called a function without actually allocating memory to a pointer.
You must have found out that, surprisingly, it works. I will leave that exercise to you guys to figure out how that works ( don't think that i don't know, i have done that exercise).
But to add to it.... the moment you want to access any member "variable" in the function, it crashes.
It crashes with Access violation..... so what on earth is this access violation?...
Its the operating systems way of telling you that you are entering its territory.
On a 32-bit machine, a pointer can access 2 pow 32 locations ( why 32?) thats about 4 GB of memory space.... this is called the process address space....
But wait... not all is available for you.... Lower 2 GB is for you (0x00000000 through 0x7FFFFFFF).. upper 2GB is for the system (0x80000000 through 0xFFFFFFFF).
You must have found out that, surprisingly, it works. I will leave that exercise to you guys to figure out how that works ( don't think that i don't know, i have done that exercise).
But to add to it.... the moment you want to access any member "variable" in the function, it crashes.
It crashes with Access violation..... so what on earth is this access violation?...
Its the operating systems way of telling you that you are entering its territory.
On a 32-bit machine, a pointer can access 2 pow 32 locations ( why 32?) thats about 4 GB of memory space.... this is called the process address space....
But wait... not all is available for you.... Lower 2 GB is for you (0x00000000 through 0x7FFFFFFF).. upper 2GB is for the system (0x80000000 through 0xFFFFFFFF).
Tuesday, April 6, 2010
Using pointer without assigning memory.....
I have tried this before and it works.....
class CMyStupidExperiment
{
public:
void CallThisToProve()
{
printf("See I told you......");
}
}
void main()
{
CMyStupidExperiment *pPointerThatActuallyWorks = NULL;
pPointerThatActuallyWorks->CallThisToProve();
}
I say.... noodle over it.....
class CMyStupidExperiment
{
public:
void CallThisToProve()
{
printf("See I told you......");
}
}
void main()
{
CMyStupidExperiment *pPointerThatActuallyWorks = NULL;
pPointerThatActuallyWorks->CallThisToProve();
}
I say.... noodle over it.....
Monday, April 5, 2010
Allocating memory
"C++" I answered with all the confidence I had, when I was asked about my strength in my first interview. So the interviewer asks me how to allocate memory to an array in C++.
"double arr[10]" I wrote on paper. He asked me "are you sure?", I said "yes". He said "are you sure there isn't any other way?" .. I said "yes.. i am pretty much sure"... and I was rejected in the interview.
Well, for all those who are thinking that i might have been right ( actually I could have been given the benefit of the doubt ), there is another way of allocating memory. Called Dynamic memory allocation ( all the experienced people might be yawning but read on for some surprise).
unsigned int yourvalue = GetUserInputValue();
double *arr = new double[yourvalue]; --------> dynamic memory allocation
Now, for a question for experienced programmer, how do you dynamically allocate memory to a two dimensional array? trust me I have asked this question to people with 4 yrs of experience but seldom I have got the right answer.
double **arr;
*arr = new double*[10] -----------------> allocate the rows
for(int i =0; i <>
arr[i] = new double[10] -----------------> allocate the columns.
so this way we allocate, a two dimensional array of 10 rows and 10 columns.
Now a question so you guys can leave a comment here..... How would you delete the above allocated two dimensional array?
Subscribe to:
Posts (Atom)
Followers
Blog Archive
About Me
- PD
- I like to travel a lot, write a lot, adventure a lot, DREAM a lot