1 / 15
文档名称:

CC++经典面试50题.docx

格式:docx   大小:24KB   页数:15页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

CC++经典面试50题.docx

上传人:s0012230 2018/7/7 文件大小:24 KB

下载得到文件列表

CC++经典面试50题.docx

相关文档

文档介绍

文档介绍:面试题3:sizeof和strlen的区别
sizeof和strlen有以下区别:
     sizeof是一个操作符,strlen是库函数。
     sizeof的参数可以是数据的类型,也可以是变量,而strlen只能以结尾为‘\0‘的字符串作参数。
    编译器在编译时就计算出了sizeof的结果。而strlen函数必须在运行时才能计算出来。并且sizeof计算的是数据类型占内存的大小,而strlen计算的是字符串实际的长度。
   数组做sizeof的参数不退化,传递给strlen就退化为指针了。
注意:有些是操作符看起来像是函数,而有些函数名看起来又像操作符,这类容易混淆的名称一定要加以区分,否则遇到数组名这类特殊数据类型作参数时就很容易出错。最容易混淆为函数的操作符就是sizeof。
strlen()与sizeof()考点,一步到位!
看下msdn的官方解释:
Strlen——Get the length of a string.
size_t strlen( const char *string );
Each ofthese functions returns the number of characters instring, notincluding the terminating null character.
函数返回string里的字符数,不包括终止字符’\0’。
sizeof Operator 运算符
sizeofexpression
The sizeofkeyword gives the amount of storage, in bytes, associated with a variable or atype (including aggregate types). This keyword returns a value of typesize_t.
//返回变量或类型(包括集合类型)存储空间的大小,
The expressionis either an identifier or a type-cast expression (a type specifier enclosed inparentheses).
When appliedto a structure type or variable,sizeof returns the actual size, whichmay include padding bytes inserted for alignment. When applied to a staticallydimensioned array,sizeof returns the size of the entire array. The sizeofoperator cannot return the size of dynamically allocated arrays or externalarrays.
//应用结构体类型或变量的时候,sizeof()返回事实的大小,包括为对齐而填充字节。当应用到静态数组时,sizeof()返回整个数组的大小。sizeof()不会返回动态分配数组或扩展数组的大小。
char str[] = “Hello”;
char *p = str ;
int n = 10;
请计算
sizeof (str) =  //5+1=6 ,注意1代表'\0'容易落下,代表结束符。 str所占据的存储空间大小
sizeof ( p )=  //4指针类型
sizeof ( n )=  //4整形占据的存储空间
void Func ( char str[100])
{
请计算
sizeof( str ) = //4此时str转化为指针大小仍为4
}
void *p = malloc( 100 );
请计算
sizeof ( p ) =  //4指针大小4
面试题5:C中的malloc和C++中的new有什么区别
malloc和new有以下不同:
(1)new、delete是操作符,可以重载,只能在C++中使用。
(2)malloc、free是函数,可以覆盖,C、C++中都可以使用。
(3)new可以调用对象的构造函数,对应的delete调用相应的析构函数。
(4)malloc仅仅分配内存,free仅仅回收内存,并不执行构造和析构函数
(5)new、delete返回的是某种数据类型指针,malloc、free返回的是void指针。
注意:malloc申请的内存空间要用free释放,而new申请的内存空间要用delete释放,不要混用。因为两者实现的机理不同。
面试题8: