1.The #undef Directive
As its name implies, the #undef directive removes (undefines) a name previously created with #define.
#undef identifier
The #undef directive removes the current definition of identifier. Consequently, subsequent occurrences of identifier are ignored by the preprocessor. To remove a macro definition using #undef, give only the macro identifier ; do not give a parameter list.
You can also apply the #undef directive to an identifier that has no previous definition. This ensures that the identifier is undefined. Macro replacement is not performed within #undef statements.
The #undef directive is typically paired with a #define directive to create a region in a source program in which an identifier has a special meaning. For example, a specific function of the source program can use manifest constants to define environment-specific values that do not affect the rest of the program. The #undef directive also works with the #if directive to control conditional compilation of the source program.
In the following example, the #undef directive removes definitions of a symbolic constant and a macro. Note that only the identifier of the macro is given.
#define WIDTH 80 #define ADD( X, Y ) (X) + (Y) . . . #undef WIDTH #undef ADD
Copies characters between buffers.
void *memcpy( void *dest, const void *src, size_t count ); wchar_t *wmemcpy( wchar_t *dest, const wchar_t *src, size_t count );
Parameters
-
dest
- New buffer. src
- Buffer to copy from. count
- Number of bytes to copy.
Return Value
The value of dest.
Remarks
Copies count bytes of src to dest. If the source and destination overlap, the behavior of memcpy is undefined. Use memmove to handle overlapping regions.
Security Note Make sure that the destination buffer is the same size or larger than the source buffer. For more information, see Avoiding Buffer Overruns.
Requirements
Routine | Required header | Compatibility |
---|---|---|
memcpy | <memory.h> or <string.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
wmemcpy | <wchar.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
Moves one buffer to another.
void *memmove( void *dest, const void *src, size_t count ); wchar_t *wmemmove( wchar_t *dest, const wchar_t *src, size_t count );
Parameters
-
dest
- Destination object. src
- Source object. count
- Number of bytes of characters to copy.
Return Value
The value of dest.
Remarks
Copies count bytes of characters from src to dest. If some regions of the source area and the destination overlap, memmove ensures that the original source bytes in the overlapping region are copied before being overwritten.
Security Note Make sure that the destination buffer is the same size or larger than the source buffer. For more information, see Avoiding Buffer Overruns.
Requirements
Routine | Required header | Compatibility |
---|---|---|
memmove | <string.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
wmemmove | <wchar.t> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_memcpy.c /* Illustrate overlapping copy: memmove * always handles it correctly; memcpy may handle * it correctly. */ #include <memory.h> #include <string.h> #include <stdio.h> char str1[7] = "aabbcc"; int main( void ) { printf( "The string: %s/n", str1 ); memcpy( str1 + 2, str1, 4 ); printf( "New string: %s/n", str1 ); strcpy( str1, "aabbcc" ); // reset string printf( "The string: %s/n", str1 ); memmove( str1 + 2, str1, 4 ); printf( "New string: %s/n", str1 ); }
Output
The string: aabbcc New string: aaaabb The string: aabbcc New string: aaaabb
The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.
Syntax
sizeof unary-expression sizeof ( type-name )
The operand is either an identifier that is a unary-expression, or a type-cast expression (that is, a type specifier enclosed in parentheses). The unary-expression cannot represent a bit-field object, an incomplete type, or a function designator. The result is an unsigned integral constant. The standard header STDDEF.H defines this type as size_t.
When you apply the sizeof operator to an array identifier, the result is the size of the entire array rather than the size of the pointer represented by the array identifier.
When you apply the sizeof operator to a structure or union type name, or to an identifier of structure or union type, the result is the number of bytes in the structure or union, including internal and trailing padding. This size may include internal and trailing padding used to align the members of the structure or union on memory boundaries. Thus, the result may not correspond to the size calculated by adding up the storage requirements of the individual members.
If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.
buffer = calloc(100, sizeof (int) );
This example uses the sizeof operator to pass the size of an int, which varies among machines, as an argument to a run-time function named calloc. The value returned by the function is stored in buffer
.
static char *strings[] ={ "this is string one", "this is string two", "this is string three", }; const int string_no = ( sizeof strings ) / ( sizeof strings[0] );
In this example, strings
is an array of pointers to char. The number of pointers is the number of elements in the array, but is not specified. It is easy to determine the number of pointers by using the sizeof operator to calculate the number of elements in the array. The const integer value string_no
is initialized to this number. Because it is a const value, string_no
cannot be modified.
Allocates an array in memory with elements initialized to 0.
void *calloc( size_t num, size_t size );
Parameters
-
num
- Number of elements. size
- Length in bytes of each element.
Return Value
calloc returns a pointer to the allocated space. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.
Remarks
The calloc function allocates storage space for an array of num elements, each of length size bytes. Each element is initialized to 0.
calloc calls malloc to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when calloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call
_set_new_mode(1)
early in your program, or link with NEWMODE.OBJ.
When the application is linked with a debug version of the C run-time libraries, calloc resolves to _calloc_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.
Requirements
Routine | Required header | Compatibility |
---|---|---|
calloc | <stdlib.h> and <malloc.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
Data Type Ranges
C/C++ recognizes the types shown in the table below.
Type Name | Bytes | Other Names | Range of Values |
int | * | signed, signed int | System dependent |
unsigned int | * | unsigned | System dependent |
__int8 | 1 | char, signed char | –128 to 127 |
__int16 | 2 | short, short int, signed short int | –32,768 to 32,767 |
__int32 | 4 | signed, signed int | –2,147,483,648 to 2,147,483,647 |
__int64 | 8 | none | –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
char | 1 | signed char | –128 to 127 |
unsigned char | 1 | none | 0 to 255 |
short | 2 | short int, signed short int | –32,768 to 32,767 |
unsigned short | 2 | unsigned short int | 0 to 65,535 |
long | 4 | long int, signed long int | –2,147,483,648 to 2,147,483,647 |
unsigned long | 4 | unsigned long int | 0 to 4,294,967,295 |
enum | * | none | Same as int |
float | 4 | none | 3.4E +/- 38 (7 digits) |
double | 8 | none | 1.7E +/- 308 (15 digits) |
long double | 10 | none | 1.2E +/- 4932 (19 digits) |