msdn随手笔记(一)-#undef;memcpy, wmemcpy;memmove, wmemmove;sizeof();calloc;Data Type Ranges

本文详细介绍了C语言中的预处理指令#undef,以及内存操作函数memcpy, wmemcpy, memmove, wmemmove的参数、返回值和注意事项。同时,讲解了sizeof运算符的用法以及calloc函数在动态内存分配中的作用,讨论了不同数据类型的范围。" 122531292,11145364,Selenium入门教程:Python环境下的Chrome自动化,"['selenium', 'python', 'web自动化', 'chrome', '自动化测试']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

2.memcpy, wmemcpy

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
RoutineRequired headerCompatibility
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

3.memmove, wmemmove

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
RoutineRequired headerCompatibility
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

4.The sizeof Operator

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.

5.calloc

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
RoutineRequired headerCompatibility
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 NameBytesOther NamesRange of Values
int*signed,
signed int
System dependent
unsigned int*unsignedSystem dependent
__int81char,
signed char
–128 to 127
__int162short,
short int,
signed short int
–32,768 to 32,767
__int324signed,
signed int
–2,147,483,648 to 2,147,483,647
__int648none–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
char1signed char–128 to 127
unsigned char1none0 to 255
short2short int,
signed short int
–32,768 to 32,767
unsigned short2unsigned short int0 to 65,535
long4long int,
signed long int
–2,147,483,648 to 2,147,483,647
unsigned long4unsigned long int0 to 4,294,967,295
enum*noneSame as int
float4none3.4E +/- 38 (7 digits)
double8none1.7E +/- 308 (15 digits)
long double10none1.2E +/- 4932 (19 digits)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值