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?
Search Your Question
Monday, May 19, 2008
C Interview Questions - Part 13
Subscribe to:
Post Comments (Atom)
Archives
-
▼
2008
(992)
-
▼
May
(474)
- Turtle 1.0 Interview question
- DotNet Framework & IIS installation order Intervi...
- Camps & Medicines are needed more than anything el...
- Executable Jar File in JAVA. Interview quations
- AJAX : Todays Buzz Interview question
- Executable Jar File in JAVA. Interview question
- AJAX : Todays Buzzword Interview question
- AJAX : Todays Buzzword Interview question
- AJAX : Todays Buzzword Interview question
- AJAX : Todays Buzzword Interview question
- What is .Resx file in Dot Net? Interview question
- How to use Microsoft Indexing Service? Interview q...
- Posting data to another ASPX Page Interview question
- Virtual Karma: Complete List of Web 2.0 Applicatio...
- Some Useful Web Links Interview question
- Home automation in the Netherlands Interview question
- Speed Up your web sites with HTTP Compression Inte...
- ASP.NET 2.0 Magic: Asynchronous Web Pages Intervie...
- RSS Puller in ASP Interview question
- ASP.NET: Interview Questions
- Java Interview Questions
- Java Interview Questions
- ASP.net Interview Questions
- PHP Interview Questions
- C Interview Questions
- C++ interview Questions
- ABAP Interview Questions
- SAP Interview Questions
- Resume Preparation Guidelines Interview question
- Salary Negotiation Interview question
- Points to remember Interview question
- Basic .NET Framework Interview question
- Software Testing Interview question
- SQL Server Interview question
- Database Interview question
- JAVA Interview Questions
- .NET Interview Questions
- SQL Server Interview Questions
- C++ Interview Questions
- Investment Management Interview Questions
- SAP-ABAP interview questions
- C Interview Questions Interview question
- Software Testing Interview Questions - Load Runner...
- Software Testing Interview Questions - Win Runner
- Software Testing Interview Questions - Manual
- ASP Interview Questions
- Oracle Interview Questions - Part 1
- VB Interview Questions
- Java Interview Questions
- J2EE Interview Questions
- C Interview Questions
- .net Interview Questions
- Java Interview Questions
- Accounting Interview Questions
- Behavioral Interview Questions Interview question
- Finance Interview Questions Interview question
- Technical &Quantitative Interview Questions Inter...
- ABAP / 4 Interview Questions Interview question
- HR Interview Questions Interview question
- C interview Questions Interview question
- Interview Concepts Interview question
- Oracle Interview Questions - Part 2 Interview que...
- Oracle Interview Questions - Part 3 Interview qu...
- Oracle Interview Questions - Part 4 Interview que...
- Oracle Interview Questions - Part 6 Interview que...
- Oracle Interview Questions - Part 7 Interview que...
- PHP Interview Questions - Part 1 Interview question
- Oracle Interview Questions - Part 8 Interview que...
- Oracle Interview Questions - Part 9 Interview que...
- Oracle Interview Questions - Part 10 Interview qu...
- Oracle Interview Questions - Part 11 Interview qu...
- Oracle Interview Questions - Part 12 Interview qu...
- Interview Tips Interview question
- PHP Interview Questions - Part 2 Interview question
- Dot Net Interview Questions - Part 1 Interview qu...
- J2ME Interview Questions Interview question
- CCNA Interview Questions Interview question
- Dot Net Interview Questions - Part 2 Interview qu...
- Dot Net Interview Questions - Part 4 Interview qu...
- Dot Net Interview Questions - Part 5 Interview qu...
- Dot Net Interview Questions - Part 6 Interview qu...
- Dot Net Interview Questions - Part 7 Interview qu...
- Dot Net Interview Questions - Part 8 Interview qu...
- Dot Net Interview Questions - Part 9 Interview qu...
- Dot Net Interview Questions - Part 10 Interview q...
- Dot Net Interview Questions - Part 11 Interview q...
- Dot Net Interview Questions - Part 12 Interview q...
- Dot Net Interview Questions - Part 14 Interview q...
- Dot Net Interview Questions - Part 15 Interview q...
- PHP Interview Questions - Part 3 Interview question
- PHP Interview Questions - Part 1 Interview question
- Oracle Interview Questions - Part 8 Interview ques...
- Oracle Interview Questions - Part 9 Interview ques...
- Oracle Interview Questions - Part 10 Interview que...
- Oracle Interview Questions - Part 11 Interview que...
- Oracle Interview Questions - Part 12 Interview qu...
- Interview Tips Interview question
- PHP Interview Questions - Part 2 Interview question
- Dot Net Interview Questions - Part 1 Interview que...
- J2ME Interview Questions Interview question
-
▼
May
(474)
No comments:
Post a Comment