NVIDIA Interview Question for Software Engineer / Developers






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

#define my_sizeof(type) \
({typeof(type) x; (char *)(&x+1)-(char*)(&x);})

- GhostArcher September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Works like a charm! Tested on Linux Mint 13 (64 bit edition)

- rationalist August 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good .. works for me ..on linux 3.0

- Manish March 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)

- CaptainObvious April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

#define my_sizeof(type) \
( (char*)(((typeof(type)*)0)+1) - (char*)((typeof(type)*)0))

- orion.zhangle April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

PERFECT!

- n00bie August 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its the perfect answer, as it works for every data type that you would pass.
The best part is you there is no variable declaration using typeof, which helps retrieve the size of 'void' type as well (=1).

- anon August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#define __SIZEOF(x) \
({ \
struct { \
typeof(x) y; \
char c[0]; \
} __attribute__((__packed__)) s; \
(size_t) (&((typeof(s)*) 0)->c); \
})

// __SIZEOF(void) gives an error

- pratik July 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yup, this works perfectly.
who cares about __SIZEOF(void) ?

- anon July 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

plz explain the last line
"(size_t) (&((typeof(s)*) 0)->c)"

and what does "__atribute__((__packed__)) s" do? is it related with padding?

- akash June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"(size_t) (&((typeof(s)*) 0)->c)" gives the offset of char c[0] in the structure s. Effectively, the offset of char c[0] in structure s is the sizeof(y) i.e. the required size.

"__atribute__((__packed__)) s" is actually not required. But if put, the sizeof structure s will be exactly equal to the sum of sizes of the members in that structure without any memory alignment.

- anon July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define sizeof(type) (int)(type+1) - (int)type

type *ptr
sizeof(ptr)

- camSun April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void main()
{
int x;
char/int/float a;
x=sizeof(a);
print a
}

- shri May 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well this also works but for only when you pass "data type".

#define SIZE(type) ((type*)0+1)

- unknown June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

with the below solution #define my_sizeof(type) (char *)(&type+1)-(char*)(&type)

if i write the code like this,
int i=0;
printf("\n%d",my_sizeof(int));
compiler gives an error any idea how to resolve from this problem?

- Anonymous June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer given by amit5624 works.
But Can someone please elaborate it?

- Coder July 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No it doesnt work.

- anon July 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it works

- dipal February 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
From anonymous is correct, however, i'd suggest the use of void pointers

- Swap August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Size of a data type varies from architecture to archotecture. The easiest way is to declare an array of two elements and see the difference in their address. This will give the exact size of the data type !

- Praveen March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Size of a data type varies from architecture to archotecture. The easiest way is to declare an array of two elements and see the difference in their address. This will give the exact size of the data type !

- Praveen March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sizeof (char* type){
void *p;
map <char[ ], int> my_map ;
my_map["int"] = 1;
my_map["char"] = 2
my_map["float"] = 3;
.
.
.
switch(my_map[type]){
case 1 :
{
 p = (int*) malloc(1);
 return ((int*) &(p+1) - (int*) &p) ;
}
case 2 :
{
 p = (char*) malloc(1);
 return ((char*) &(p+1) - (char*) &p);
}
.
.
.
.
and so on
            
}

- dss.chandra March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(type) \
( (char*)(((typeof(type)*)0)+1) - (char*)((typeof(type)*)0))

- orion April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(type) ((type *)0+1)

This is simpler

- orzgodlo February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typeof is a non standard extension of c which return the static type of the variable. So if we wish to answer this question in native C domain I suppose better way is to us size_t which is a native block data type.

#define sizeof(var)  size_t((&var)+1) - size_t(&var)

this should work.

- Delpiero May 28, 2013 | Flag Reply


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