NVIDIA Interview Question
InternsTeam: System Software
Country: United States
Interview Type: Phone Interview
Yes. Actually, my question is when we free it.
The array would be freed when out of scope. So we have to do this manually when exceed the scope,right?
Your are funny dude..(assuming you were the one who commented on the other question too) do you always go to each question and say its a stupid question? There is a difference between 'simple' pointer to a memory and an array. Do a sizeof operator on an array and a 'simple' pomoter to a memory.anyway this is not relavant to this question..
2 things that an array gives that i can think of is memory space and random access. We can get memory space from malloc. For the random access since we dont hv the compilers help we will not be avle to use [] operator. We will have to implement a function which will do pointer arithmetic and give the value at a gicen poisiton. So instead of a[10] we will be calling get_value(a,10), here a is the pointer to the malloced space.
Ofcourse there will be a question of freeing the memory space and stuff. We should thank the compilers for having given us array..
nope, dude, I do not write for each question: only for those which are stupid ))
what you said is right but well operator [] is a syntactic construct to do pointer arithmetic, It has nothing do to with a "data structure", e.g. you can write a[10] and 10[a] which are the same for compiler.
On a similar note, you can implement sizeof operator using pointer to char*
oh mighty compiler thanks for giving us an array ))
To implement integer array of size array_size
- Punit Jain May 11, 2012int *p = (int *) malloc (sizeof(int) * array_size);
now you can assign values using p[i] = something // i =0 to array_size-1
and and access them using same;