




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、10、10、4、4四個數,怎么算出24點?
(10*10-4)/4=24
2、下列表達式在32位機器編譯環境下的值()[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?class
A
{
};
class
B
{
public:
B();
virtual
~B();
};
class
C
{
private:
#pragma
pack(4)
int
i;
short
j;
float
k;
char
l[64];
long
m;
char
*p;
#pragma
pack()
};
class
D
{
private:
#pragma
pack(1)
int
i;
short
j;
float
k;
char
l[64];
long
m;
char
*p;
#pragma
pack()
};
int
main(void)
{
printf("%d\n",sizeof(A));
printf("%d\n",sizeof(B));
printf("%d\n",sizeof(C));
printf("%d\n",sizeof(D));
return
0;
}
A、1、4、84、82
B、4、4、82、84
C、4、4、84、82
D、1、4、82、82
3、以下程序在32位機器下運行的結果是()[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#pragma
pack(4)
struct
info_t
{
unsigned
char
version;
unsigned
char
padding;
unsigned
char
extension;
unsigned
char
count;
unsigned
char
marker;
unsigned
char
payload;
unsigned
short
sequence;
unsigned
int
timestamp;
unsigned
int
ssrc;
};
union
info_u
{
unsigned
char
version;
unsigned
char
padding;
unsigned
char
extension;
unsigned
char
count;
unsigned
char
marker;
unsigned
char
payload;
unsigned
short
sequence;
unsigned
int
timestamp;
unsigned
int
ssrc;
};
#pragma
pack()
int
main(void)
{
printf("%d\n",sizeof(info_t));
printf("%d\n",sizeof(info_u));
return
0;
}
A、12
12
B、12
4
C、16
4
D、16
12
E、16
1
4、以下表達式result的值是()[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#define
VAL1(a,b)
a*b
#define
VAL2(a,b)
a/b--
#define
VAL3(a,b)
++a%b
int
a
=
1;
int
b
=
2;
int
c
=
3;
int
d
=
3;
int
e
=
5;
int
result
=
VAL2(a,b)/VAL1(e,b)+VAL3(c,d);
A、-2
B、1
C、0
D、2
5、請寫出以下程序的輸出(5分)[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?void
swap_1(int
a
,
int
b)
{
int
c;
c
=
a;
a
=
b;
b
=
c;
return
;
}
void
swap_2(int
&a
,
int
&b)
{
int
c;
c
=
a;
a
=
b;
b
=
c;
return
;
}
void
swap_3(int
*a
,
int
*b)
{
int
c;
c
=
*a;
*a
=
*b;
*b
=
c;
return
;
}
int
main(void)
{
int
a
=
100;
int
b
=
200;
swap_1(a
,
b);
printf("a
=
%d
,
b
=
%d\n",a
,
b);
swap_2(a
,
b);
printf("a
=
%d
,
b
=
%d\n",a
,
b);
swap_3(&a
,
&b);
printf("a
=
%d
,
b
=
%d\n",a
,
b);
return
0;
}
輸出結果:
a=100,b=200
a=200,b=100
a=100,b=200
6、下面的程序是否有問題,如有問題,請重構代碼(5分)[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?void
test_type(bool
b
,
const
char
*p
,
float
f)
{
if(!b)
{
return
;
}
else
if(!p)
{
return
;
}
else
if(!f)
{
return
;
}
}
修改如下:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?void
test_type(bool
b
,
const
char
*p
,
float
f)
{
if(!b)
{
return
;
}
else
if(!p)
{
return
;
}
else
if(f
>
-1e-10
&&
f
<
1e-10)
{
return
;
}
}
7、請指出以下程序有什么問題(5分)[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?void
test_mem()
{
char
*p
=
new
char[64];
delete
p;
p
=
NULL;
return
;
}
應該修改為delete[]p;
p指向的是一個字符型的數組空間,原來的代碼只是簡單的釋放了指向申請空間的指針,并沒有釋放申請的空間,容易造成內存崩潰。
回收用new分配的單個對象的內存空間的時候用delete,回收用new[]分配的一組對象的內存空間的時候用delete[]。
8、以下程序有什么問題,請指出。[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?char*
GetMem()
{
char
p[]
=
"hello";
return
p;
}
void
test_get_mem()
{
char
*p
=
GetMem();
printf(p);
return
;
}
GetMem函數中的p是一個在棧上的局部變量,當函數運行結束的時候,棧上的內容會自動釋放的,此處返回的值有可能會成為一個野指針,會出現一個意想不到的結果。
9、請寫出strcpy和memcpy的區別(5分)
答:strcpy和memcpy都是標準C庫函數,它們有下面的特點。
strcpy提供了字符串的復制。即strcpy只用于字符串復制,并且它不僅復制字符串內容之外,還會復制字符串的結束符。
strcpy函數的原型是:char*strcpy(char*dest,constchar*src);
memcpy提供了一般內存的復制。即memcpy對于需要復制的內容沒有限制,因此用途更廣。
memcpy函數的原型是:void*memcpy(void*dest,constvoid*src,size_tcount);
strcpy和memcpy主要有以下3方面的區別。
1、復制的內容不同。strcpy只能復制字符串,而memcpy可以復制任意內容,例如字符數組、整型、結構體、類等。
2、復制的方法不同。strcpy不需要指定長度,它遇到被復制字符的串結束符"\0"才結束,所以容易溢出。memcpy則是根據其第3個參數決定復制的長度。
3、用途不同。通常在復制字符串時用strcpy,而需要復制其他類型數據時則一般用memcpy。
10、請寫出以下程序的輸出結果[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?class
Base
{
public:
Base()
{
printf("I
am
Base()\n");
}
virtual
~Base()
{
printf("I
am
~Base()\n");
}
public:
virtual
void
SayHello()
{
printf("Hello
Base\n");
}
void
SayWorld()
{
printf("World
Base\n");
}
};
class
Derived
:
public
Base
{
public:
Derived()
{
printf("I
am
Derived()\n");
}
virtual
~Derived()
{
printf("I
am
~Derived()\n");
}
public:
void
SayHello();
void
SayWorld();
};
void
Derived::SayHello()
{
printf("Hello
Derived\n");
}
void
Derived::SayWorld()
{
printf("World
Derived\n");
}
int
main(void)
{
Base
*b1
=
new
Base;
Base
*b2
=
new
Derived;
Derived
*d
=
new
Derived;
b1->SayHello();
b1->SayWorld();
b2->SayHello();
b2->SayWorld();
d->SayHello();
d->SayWorld();
delete
d;
delete
b2;
delete
b1;
d=
NULL;
b2
=
NULL;
b1
=
NULL;
return
0;
}
輸出結果:
IamBase()
IamBase()
IamDerived()
IamBase()
IamDerived()
HelloBase
WorldBase
HelloDerived
WorldBase
HelloDerived
WorldDerived
Iam~Derived()
Iam~Base()
Iam~Derived()
Iam~Base()
Iam~Base()
11、閱讀以下程序并給出執行結果[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?class
Bclass
{
public:
Bclass(int
i
,
int
j)
{
x
=
i;
y
=
j;
}
virtual
int
fun()
{
return
0;
}
protected:
int
x
,
y;
};
class
lclass
:
public
Bclass
{
public:
lclass(int
i
,
int
j
,
int
k)
:
Bclass(i
,
j)
{
z
=
k;
}
int
fun()
{
return
(x+y+z)/3;
}
private:
int
z;
};
int
main(void)
{
lclass
obj(2,4,10);
Bclass
p1
=
obj;
cout<<p1.fun()<<endl;
Bclass
&p2
=
obj;
cout<<p2.fun()<<endl;
cout<<p2.Bclass::fun()<<endl;
Bclass
*p3
=
&obj;
cout<<p3->fun()<<endl;
return
0;
}
輸出結果:
0
5
0
5
12、如何減少頻繁分配內存(malloc或者new)造成的內存碎片?(10分)
13、請寫出strchr的實現(10分)
函數功能:找出在字符串str中第一次出現字符ch的位置,找到就返回該字符位置的指針(也就是返回該字符在字符串中的地址的位置),找不到就返回空指針(就是NULL)
constchar*strchr(constchar*str,charch)[cpp]\o"viewplain"viewplain\o"copy"copy\o"p
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論