NVIDIA Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
4
of 4 vote

Reference Parameters In C
We are bumping into a basic "feature" of the C language that changes to local parameters
are never reflected back in the caller's memory. This is a traditional tricky area of C
programming. We will present the traditional "reference parameter" solution to this
problem, but you may want to consult another C resource for further information.
We need Push() to be able to change some of the caller's memory — namely the head
variable. The traditional method to allow a function to change its caller's memory is to pass a pointer to the caller's memory instead of a copy. So in C, to change an int in the caller, pass a int* instead. To change a struct fraction, pass a struct
fraction* intead. To change an X, pass an X*. So in this case, the value we want to
change is struct node*, so we pass a struct node** instead. The two stars (**) are a little scary, but really it's just a straight application of the rule. It just happens
that the value we want to change already has one star (*), so the parameter to change it
has two (**). Or put another way: the type of the head pointer is "pointer to a struct
node." In order to change that pointer, we need to pass a pointer to it, which will be a
"pointer to a pointer to a struct node".
Instead of defining WrongPush(struct node* head, int data); we define
Push(struct node** headRef, int data);. The first form passes a copy of
the head pointer. The second, correct form passes a pointer to the head pointer. The rule
is: to modify caller memory, pass a pointer to that memory. The parameter has the word
"ref" in it as a reminder that this is a "reference" (struct node**) pointer to the
head pointer instead of an ordinary (struct node*) copy of the head pointer.

- Shail March 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

hxxp://cslibrary.stanford.edu/103/LinkedListBasics.pdf

- AlgoKing!! March 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why double pointer is needed ?
Answer: lets consider two prototypes:
(1) void myAllocate1(void* p, size_t sz);
Here you can pass NULL for p which is syntactically correct but actually incorrect. even if you do p=(void*) malloc(sz);, then this function call is like a call by value; *p=somevalue will be a call by pointer but p=somepointer is call by value;
(2) void myAllocate2(void** p, size_t sz);
Here you have both benefits; p actually is visible outside when function returns and you can not send NULL for p

- abhityagi85 January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

another way is
void* allocate( void* a, int len, int element_size)
{
a = malloc(element_size*len);
return a;
}

should be called like:
int* p;
p = allocate(p,10,sizeof(int));

- deepblue March 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

WHY NOT:
void* allocate( int len, int element_size)
{
void* a = malloc(element_size*len);
return a;
}

should be called like:
int* p;
p = allocate(10,sizeof(int));

- MS June 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think MS's answer is what they want.

void allocate(int len, int element_size, void ** p) is cumbersome. And some compiler would just complain about the castings.

- GhostArcher September 23, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More