Interview Question


Country: United States




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

Above code will segfault on IA-64, because you missed to include prototype of "malloc()" function. Include "stdlib.h" header file.

IA-32 is ILP-32 bit model i.e. integer, long and pointers are 32 bit, but IA-64 is LP-64 bit model i.e. long and pointer is 64 bit. In C if prototype of function is not declared , then compiler assumes return value of function is as "int". "malloc" returns "void*" i.e. some address to allocated memory. On IA-32 integer and pointer of same length that's why it will work. But on IA-64 integer is 32-bit and pointer is 64-bit that why in absence of prototype we will get truncated address from malloc() , which is invalid and when we will try to deference that address, result is segmentation fault.

Check below simple program.


#include <errno.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp;

fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
return errno;
}

printf("File exist\n");

fclose(fp);

return 0;
}

This code will check whether file given from command line is exist or not. Here i haven't included "string.h" header file(needed for strerror() function). That's why this program will segfault on IA-64, but on IA-32 it will work fine.

- Narendra. July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

As per my knowledge, there is no such falut in code for which it ll be crash.

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

geeksforgeeks.org/archives/21814

- rachit July 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Code independent of 64 Bit or 32 Bit architecture. Only sizeof(p) will be 8 bytes in 64 bit and 4 bytes in 32 bit compiler. There is no statement where in the code would crash. I am not sure if the code you posted is correct?

- Indra Chatterjee July 02, 2012 | 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