0% found this document useful (0 votes)
39 views

Memory Allocation Questions

The document discusses memory allocation in C and provides answers to common questions about pointers, dynamically allocating memory using functions like malloc(), free(), and realloc(), and properly using strings and character arrays. Key points covered include that pointers need to point to valid allocated memory, malloc allocates memory for the pointer but not what it points to, memory allocated with malloc needs to be freed, and strings cannot be directly assigned but must use functions like strcpy().

Uploaded by

Manisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Memory Allocation Questions

The document discusses memory allocation in C and provides answers to common questions about pointers, dynamically allocating memory using functions like malloc(), free(), and realloc(), and properly using strings and character arrays. Key points covered include that pointers need to point to valid allocated memory, malloc allocates memory for the pointer but not what it points to, memory allocated with malloc needs to be freed, and strings cannot be directly assigned but must use functions like strcpy().

Uploaded by

Manisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Section 7.

Memory Allocation

7.1: Why doesn't the code "char *answer; gets(answer);" work?

A: The pointer variable answer has not been set to point to any valid
storage. The simplest way to correct this fragment is to use a local array,
instead of a pointer.

7.2: I can't get strcat() to work. I tried "char *s3 = strcat(s1, s2);" but I
got strange results.

A: Again, the main problem here is that space for the concatenated result is
not properly allocated.

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?

A: In general, when using pointers you *always* have to consider memory


allocation, if only to make sure that the compiler is doing it for you.

7.3b: I just tried the code "char *p; strcpy(p, "abc");" and it worked. Why
didn't it crash?

A: You got "lucky".

7.3c: How much memory does a pointer variable allocate?

A: Only enough memory to hold the pointer itself, not any memory for the
pointer to point to.

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.

A: Make sure that the pointed-to memory is properly (i.e. not locally)
allocated.

7.5b: So what's the right way to return a string?

A: Return a pointer to a statically-allocated buffer, a buffer passed in by


the caller, or memory obtained with malloc().

7.6: Why am I getting "warning: assignment of pointer from integer lacks a


cast" for calls to malloc()?

A: Have you #included <stdlib.h>?

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

A: Before ANSI/ISO C, these casts were required to silence certain warnings.


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

A: It means you're using a C++ compiler.

7.8: Why does so much code leave out the multiplication by sizeof(char) when
allocating strings?

A: Because sizeof(char) is, by definition, exactly 1.


7.11: How can I dynamically allocate arrays?
A: See questions 6.14 and 6.16.
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?

A: It's hard to say.

7.16: I'm allocating a large array for some numeric work, but malloc() is
acting strangely.

A: Make sure the number you're trying to pass to malloc() isn't bigger than a
size_t can hold.

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

A: Under the segmented architecture of PC compatibles, it can be difficult to


use more than 640K with any degree of transparency. See also question 19.23.

7.19: My program is crashing, apparently somewhere down inside malloc.

A: Make sure you aren't using more memory than you malloc'ed, especially for
strings (which need strlen(str) + 1 bytes).

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

A: No. Some early documentation implied otherwise, but the claim is no longer
valid.

7.21: Why isn't a pointer null after calling free()?

A: C's pass-by-value semantics mean that called functions can never


permanently change the values of their arguments.

7.22: When I call malloc() to allocate memory for a local pointer, do I have
to explicitly free() it?

A: Yes.

7.23: When I free a dynamically-allocated structure containing pointers, do I


also have to free each subsidiary pointer?
A: Yes.

7.24: Must I free allocated memory before the program exits?

A: You shouldn't have to.

7.25: Why doesn't my program's memory usage go down when I free memory?

A: Most implementations of malloc/free do not return freed memory to the


operating system.

7.26: How does free() know how many bytes to free?


A: The malloc/free implementation remembers the size of each block

as it is allocated.

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

A: Not portably.

7.30: Is it legal to pass a null pointer as the first argument to realloc()?

A: ANSI C sanctions this usage, although several earlier implementations do


not support it.

7.31: What's the difference between calloc() and malloc()?

A: calloc() takes two arguments, and initializes the allocated memory to all-
bits-0.

7.32: What is alloca() and why is its use discouraged?

A: alloca() allocates memory which is automatically freed when the function


which called alloca() returns. alloca() cannot be written portably, is
difficult to implement on machines without a stack, and fails under certain
conditions if implemented simply.

Section 8. Characters and Strings

8.1: Why doesn't "strcat(string, '!');" work?

A: strcat() concatenates *strings*, not characters.

8.2: Why won't the test if(string == "value") correctly compare string
against the value?

A: It's comparing pointers. To compare two strings, use strcmp().

8.3: Why can't I assign strings to character arrays?


A: Strings are arrays, and you can't assign arrays directly. Use strcpy()
instead.

8.6: How can I get the numeric (character set) a character?

A: In C, if you have the character, you have 8.9: Why is sizeof('a') not 1?
A: Character constants in C are of type int.

You might also like