Home
121.
What’s the advantage of dynamic memory allocation? What can’t I just declare the storage space I need in my source code?

If you declare all your data storage in your source code, the amount of memory available to your program is fixed. You have to know ahead of time, when you write the program, how much memory will be needed. Dynamic memory allocation lets your program control the amount of memory used to suit the current conditions and user input. The program can use as much memory as it needs, up to the limit of what’s available in the computer.

122.
What happens if I reuse a string without calling realloc()?

You don't need to call realloc() if the string you're using was allocated enough room. Call realloc() when your current string isn't big enough. Remember, the C compiler lets you do almost anything, even things you shouldn't! You can overwrite one string with a bigger string as long as the new string's length is equal to or smaller than the original string’s allocated space. However, if the new string is bigger, you will also overwrite whatever came after the string in memory. This could be nothing, or it could be vital data. If you need a bigger allocated section of memory, call realloc().

123.
What's the advantage of the memset(), memcpy(), and memmove() functions? Why can't I just use a loop with an assignment statement to initialize or copy memory?

You can use a loop with an assignment statement to initialize memory in some cases. In fact, sometimes this is the only way to do it—for example, setting all elements of a type float array to the value 1.23. In other situations, however, the memory will not have been assigned to an array or list, and the mem...() functions are your only choice. There are also times when a loop and assignment statement work, but the mem...() functions are simpler and faster.

124.
When would I use the shift operators and the bitwise logical operators?

The most common use for these operators is when a program is interacting directly with the computer hardware—a task that often requires specific bit patterns to be generated and interpreted. Even if you never need to manipulate hardware directly, you can use the shift operators, in certain circumstances, to divide or multiply integer values by powers of two.

125.
Describe the difference between the results of the following two expressions:
(01010101 ^ 11111111 )
( ~01010101 )

These two expressions evaluate to the same result. Using exclusive OR with 11111111 is the same as using the complement operator: Each bit in the original value is reversed.

126.
Do header files need to have an .h extension?

No. You can give a header file any name you want. It is standard practice to use the .h extension.

127.
When including header files, can I use an explicit path?

When including header files, can I use an explicit path?

128.
What is meant by dynamically allocating memory?

Dynamically allocated memory is allocated at runtime—while the program is executing. Dynamic memory allocation lets you allocate exactly as much memory as is needed, only when it is needed.

129.
When you define a macro, why should each argument be enclosed in parentheses?

To avoid unwanted side effects by ensuring that complex expressions passed as arguments to the macro are fully evaluated first.

130.
What are the pros and cons of using a macro in place of a regular function?

Compared to a function, a macro results in faster program execution but larger program size.