Wednesday, December 19, 2007

C Interview Questions - Part 13

7.1 Why doesn't this fragment work?

char *answer;
printf("Type something:\n");
gets(answer);
printf("You typed \"%s\"\n", answer);

7.2 I can't get strcat to work. I tried

char *s1 = "Hello, ";
char *s2 = "world!";
char *s3 = strcat(s1, s2);

but I got strange results.

7.3 But the man page for strcat says that it takes two char *'s as arguments. How am I supposed to know to allocate things?

7.3b I just tried the code

char *p;
strcpy(p, "abc");

and it worked. How? Why didn't it crash?

7.3c How much memory does a pointer variable allocate?

7.4 I'm reading lines from a file into an array, with this code:

char linebuf[80];
char *lines[100];
int i;

for(i = 0; i <>
char *p = fgets(linebuf, 80, fp);
if(p == NULL) break;
lines[i] = p;
}

Why do all the lines end up containing copies of the last line?

7.5a I have a function that is supposed to return a string, but when it returns to its caller, the returned string is garbage.

7.5b So what's the right way to return a string or other aggregate?

7.6 Why am I getting ``warning: assignment of pointer from integer lacks a cast'' for calls to malloc?

7.7 Why does some code carefully cast the values returned by malloc to the pointer type being allocated?

7.7b What's wrong with casting malloc's return value?

7.7c In a call to malloc, what does an error like ``Cannot convert `void *' to `int *''' mean?

7.8 I see code like

char *p = malloc(strlen(s) + 1);
strcpy(p, s);

Shouldn't that be malloc((strlen(s) + 1) * sizeof(char))?

7.9 I wrote a little wrapper around malloc, but it doesn't work:

#include
#include

mymalloc(void *retp, size_t size)
{
retp = malloc(size);
if(retp == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
}

7.10 I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code?

char *p;
*p = malloc(10);

7.10a What's wrong with this initialization?

char *p = malloc(10);

My compiler is complaining about an ``invalid initializer'', or something.

7.10b How can I shut off the ``warning: possible pointer alignment problem'' message which lint gives me for each call to malloc?

7.11 How can I dynamically allocate arrays?

7.12 How can I find out how much memory is available?

7.13 What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?

7.14 I've heard that some operating systems don't actually allocate malloc'ed memory until the program tries to use it. Is this legal?

7.15 malloc is returning crazy pointer values, but I did read question 7.6 and I have included the line

extern void *malloc();

before I call it.

7.16 I'm allocating a large array for some numeric work, using the line

double *array = malloc(300 * 300 * sizeof(double));

malloc isn't returning null, but the program is acting strangely, as if it's overwriting memory, or malloc isn't allocating as much as I asked for, or something.

7.17 I've got 8 meg of memory in my PC. Why can I only seem to malloc 640K or so?

7.18 My application depends heavily on dynamic allocation of nodes for data structures, and malloc/free overhead is becoming a bottleneck. What can I do?

7.19 My program is crashing, apparently somewhere down inside malloc, but I can't see anything wrong with it. Is there a bug in malloc?

7.19b

I'm dynamically allocating an array, like this:

int *iarray = (int *)malloc(nints);

malloc isn't returning NULL, but the code isn't working.

7.20 You can't use dynamically-allocated memory after you free it, can you?

7.21 Why isn't a pointer null after calling free?
How unsafe is it to use (assign, compare) a pointer value after it's been freed?

7.22 When I call malloc to allocate memory for a pointer which is local to a function, do I have to explicitly free it?

7.23 I'm allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer?

7.24 Must I free allocated memory before the program exits?

7.25 I have a program which mallocs and later frees a lot of memory, but I can see from the operating system that memory usage doesn't actually go back down.

7.26 How does free know how many bytes to free?

7.27 So can I query the malloc package to find out how big an allocated block is?

7.28 Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?

7.29 Having dynamically allocated an array (as in question 6.14), can I change its size?

7.30 Is it legal to pass a null pointer as the first argument to realloc? Why would you want to?

7.31 What's the difference between calloc and malloc? Which should I use? Is it safe to take advantage of calloc's zero-filling? Does free work on memory allocated with calloc, or do you need a cfree?

7.32 What is alloca and why is its use discouraged?

No comments: