您的位置:首页 > 其它

几道笔试题

2007-04-20 22:01 211 查看
请写出BOOL flag 与“零值”比较的 if 语句
if(flag)
if(!flag)

请写出float x 与“零值”比较的 if 语句

const float fFlag = 0.00001;
if((-fFlag <= x)&&(x <= fFlag))

请写出char *p 与“零值”比较的 if 语句

if(p == NULL)
if(p !=NULL)

Windows NT下的32位C++程序,请计算sizeof的值

char *p = str ;
char str[] = “Hello”
sizeof (str ) = 6
sizeof ( p ) = 4
void Func ( char str[100])

{

请计算

sizeof( str ) = 4

}
void *p = malloc( 100 );

请计算

sizeof ( p ) = 4

头文件中的 ifndef/define/endif 干什么用?

防止头文件被重复定义,那样会加大系统开销。

const 有什么用途?(请至少说明两种)

const int i = 100; //保证 i 的值不被修改
class A
{
public:
void Fun()const; //保证传近来的对象不被修改
}
const int *pi = "Hello World"; //保证pi所指的内容不被修改
int *const pi = "Hello World"; //保证pi指向的地址不改变
const int *const pi = "Hello World"; //保证pi指向的地址和pi指向的内容都
//不会改变

请简述以下两个for循环的优缺点
for (i=0; i < 10; i++)
{
if (condition)
DoSomething();
else
DoOtherthing();
}

if (condition)
{
for (i = 0; i < 10; i++)
DoSomething();
}
else
{
for (i=0; i
DoOtherthing();
}
第一个可读性好,但是效率差
第二个可读性不好,但是效率高

void GetMemory(char *p)
{
p = (char *)malloc(100);
}

void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行Test函数会有什么样的结果?

GetMemory(char *p) 这里会为*p创建一个副本比如char* _p
编译器让 _p = p,在函数体内对于_p自身的修改都作用于 p 上
p = (char *)malloc(100); 这里只是改变了 _p 所指向的内容
因此也就不作用于 p 上,所以GetMemory(str);这里str 依然
为NULL.而且上面的代码会导致内存泄漏.

char *GetMemory(void)
{
char p[] = "hello world";
return p;
}

void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
请问运行Test函数会有什么样的结果?

GetMemory返回的是一个数组的首地址
这个首地址是在栈上申请的
因此函数结束后被销毁
它所指向的内容被系统回收

void GetMemory2(char **p, int num)

{

*p = (char *)malloc(num);

}

void Test(void)

{

char *str = NULL;

GetMemory(&str, 100);

strcpy(str, "hello");

printf(str);

}

GetMemory(&str, 100)会为 str 动态申请一个空间
这里可以正常输出但是,会造成内存泄漏
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: