- Home
- Interview Questions
- C Programming
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.
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().
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.
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.
(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.
No. You can give a header file any name you want. It is standard practice to use the .h extension.
When including header files, can I use an explicit path?
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.
To avoid unwanted side effects by ensuring that complex expressions passed as arguments to the macro are fully evaluated first.
Compared to a function, a macro results in faster program execution but larger program size.