c编程常见问题

来源:互联网 发布:centos iso系统 编辑:程序博客网 时间:2024/05/16 15:43
1 #i nclude “filename.h”和#i nclude<filename.h> 的区别?
答:对于#i nclude <filename.h>编译器从标准库开始搜索filename.h
对于#i nclude “filename.h”编译器从用户工作路径开始搜索filename.h
2 头文件的作用是什么?
答:一、通过头文件来调用库功能。在很多场合,源代码不便(或不准)向用户公布,只要向用户提供头文件和二进制的库即可。用户只需
要按照头文件中的接口声明来调用库功能,而不必关心接口怎么实现的。编译器会从库中提取相应的代码。
二、头文件能加强类型安全检查。如果某个接口被实现或被使用时,其方式与头文件中的声明不一致,编译器就会指出错误,这一简单的规
则能大大减轻程序员调试、改错的负担。
3 C++函数中值的传递方式有哪几种?
答:C++函数的三种传递方式为:值传递、指针传递和引用传递。
4 内存的分配方式的分配方式有几种?
答:一、从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量。
二、在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。
  三、从堆上分配,亦称动态内存分配。程序在运行的时候用malloc或new申请任意多少的内存,程序员自己负责在何时用free或delete释放内存。动态内存的生存期由我们决定,使用非常灵活,但问题也最多。

5 实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数;
答:双向链表删除一个节点P
template void list::delnode(int p)
{
int k=1;
listnode *ptr,*t;
ptr=first;
while(ptr->next!=NULL&&k!=p)
{
ptr=ptr->next;
k++;
}
t=ptr->next;
cout<<"你已经将数据项 "<data<<"删除"<
ptr->next=ptr->next->next;
length--;
delete t;
}
在节点P后插入一个节点:
template bool list::insert(type t,int p)
{
listnode *ptr;
ptr=first;
int k=1;
while(ptr!=NULL&&k{
ptr=ptr->next;
k++;
}
if(ptr==NULL&&k!=p)
return false;
else
{
listnode *tp;
tp=new listnode;
tp->data=t;
tp->next=ptr->next;
ptr->next=tp;
length++;
return true;
}
}

6 写一个函数,将其中的/t都转换成4个空格。
void change(char* pstr)
{
  while(*pstr++ != '/0')
{
  if (*pstr == '/t')

}
}
7 Windows程序的入口是哪里?写出Windows消息机制的流程.
答:Winmain(),它定义窗口界面和消息循环。设置全局资源->登记实例->调入全局资源->初始化应用->消息循环(解释消息,分发消息)
8 如何定义和实现一个类的成员函数为回调函数?
1). 不使用成员函数,直接使用普通C函数,为了实现在C函数中可以访问类的成员变量,可以使用友元操作符(friend),在C++中将该C函数说明为类的友元即可。这种处理机制与普通的C编程中使用回调函数一样。
2). 使用静态成员函数,静态成员函数不使用this指针作为隐含参数,这样就可以作为回调函数了。
9 C++里面是不是所有的动作都是main()引起的?如果不是,请举例.
答:在运行c++程序时,通常从main()函数开始执行。因此如果没有main(),程序将不完整,编译器将指出未定义main()函数。
  例外情况:如, 在windows编程中,可以编写一个动态连接库(dll)模块,这是其他windows程序可以使用的代码。由于
DLL模块不是独立的程序,因此不需要main().用于专用环境的程序--如机器人中的控制器芯片--可能不需要main().但常规的
  独立程序都需要main().
10 C++里面如何声明const void f(void)函数为C程序中的库函数?
11 下列哪两个是等同的
int b;
A const int* a = &b;
B const* int a = &b;
C const int* const a = &b;
D int const* const a = &b;

12 内联函数在编译时是否做参数类型检查

13 三个float:a,b,c
问值
(a+b)+c==(b+a)+c
  (a+b)+c==(a+c)+b
  14 把一个链表反向填空
void reverse(test* head)
{
test* pe = head;
test* ps = head->next;
while(ps)
{
  pe->next = ps->next;
ps->next = head;
head = ps;
ps = pe->next;
}
}

15 设计一个重采样系统,说明如何anti-alias
16 某个程序在一个嵌入式系统(200M的CPU,50M的SDRAM)中已经最化了,换到另一个系统(300M的CPU,50M的SDRAM)中运行,还需要优化吗?
17. 下面哪种排序法对12354最快
a quick sort
  b.buble sort
  c.merge sort
18. 哪种结构,平均来讲,获取一个值最快
a. binary tree
  b. hash table
  c. stack

19 请问C++的类和C里面的struct有什么区别?
  答:c++的类的成员默认情况下是私有的,c的struct的成员默认情况下是公共的.
20 请讲一讲析构函数和虚函数的用法和作用?
答:析构函数的作用是当对象生命期结束时释放对象所占用的资源。 析构函数用法:析构函数是特殊的类成员函数
它的名字和类名相同,没有返回值,没有参数不能随意调用也没有重载。只是在类对象生命期结束时有系统自动调用。
虚函数用在继承中,当在派生类中需要重新定义基类的函数时需要在基类中将该函数声明为虚函数,作用为使程序支持动态联遍。

21 全局变量和局部变量有什么区别?是怎么实现的?操作系统和编译器是怎么知道的?
答:一些变量整个程序中都是可见的,它们称为全局变量,一些变量在函数内部定义且只在函数中可知,则称为局部变量。
全局变量由编译器建立且存放在内存的全局数据区,局部变量存放在栈区
22 一些寄存器的题目,主要是寻址和内存管理等一些知识。
23 8086是多少尉的系统?在数据总线上是怎么实现的?
24 多态。overload 和 override的区别。
答:重载在相同范围(同一个类中),函数名字相同,参数不同,virtual关键字可有可无。
覆盖是指派生类函数覆盖基类函数,不同的范围,函数名字相同,参数相同,基类函数必须有virtual关键字。
<>
25.完成下列程序
*
*.*.
*..*..*..
*...*...*...*...
*....*....*....*....*....
*.....*.....*.....*.....*.....*.....
*......*......*......*......*......*......*......
*.......*.......*.......*.......*.......*.......*.......*.......
#i nclude
using namespace std;
const int n = 8;
main()
{
int i;
int j;
int k;
for(i = n; i >= 1; i--)
{
for(j = 0; j < n-i+1; j++)
{
cout<<"*";
for(k=1; k < n-i+1; k++)
{
cout<<".";
}
}
cout< }
system("pause");
}

26 完成程序,实现对数组的降序排序
#i nclude
  using namespace std;
void sort(int* arr, int n);
int main()
{
int array[]={45,56,76,234,1,34,23,2,3};
sort(array, 9);
for(int i = 0; i <= 8; i++)//曾经在这儿出界
cout<}
void sort(int* arr, int n)
{
  int temp;
for(int i = 1; i < 9; i++)
{
for(int k = 0; k < 9 - i; k++)//曾经在这儿出界
{
if(arr[k] < arr[k + 1])
{
temp = arr[k];
arr[k] = arr[k + 1];
arr[k + 1] = temp;
}
  }
}
}

27 费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他方法,但要说明你选择的理由。
非递归
#i nclude
  using namespace std;
int Pheponatch(int n);
main()
{
int Ph = Pheponatch(10);
cout< system("pause");
}
int Pheponatch(int n)
{
int elem;
int n1 = 1;
int n2 = 1;
if(n == 1 || n ==2)
return 1;
else
{
for(int i = 3; i <= n; i++)
{
elem = n1 + n2;
n1 = n2;
n2 = elem;
}
return elem;
}
}
递归
#i nclude
  using namespace std;
int Pheponatch(int n);
main()
{
int n;
cin>>n;
int ph = Pheponatch(n);
cout< system("pause");
}
int Pheponatch(int n)
{
  if(n <= 0)
exit(-1);
  else
if(n == 1 || n ==2)
return 1;
else
return Pheponatch(n - 1) + Pheponatch(n - 2);
}
28 下列程序运行时会崩溃,请找出错误并改正,并且说明原因。
#i nclude
#i nclude

typedef struct{
TNode* left;
TNode* right;
int value;
} TNode;

TNode* root=NULL;

void append(int N);

int main()
{
append(63);
append(45);
append(32);
append(77);
append(96);
append(21);
append(17); // Again, 数字任意给出
}

void append(int N)
{
TNode* NewNode=(TNode *)malloc(sizeof(TNode));
NewNode->value=N;

if(root==NULL)
{
root=NewNode;
return;
}
else
{
TNode* temp;
temp=root;
while((N>=temp.value && temp.left!=NULL) || (N
))
{
while(N>=temp.value && temp.left!=NULL)
temp=temp.left;
while(N
temp=temp.right;
}
if(N>=temp.value)
temp.left=NewNode;
else
temp.right=NewNode;
return;
}
}

29. A class B network on the internet has a subnet mask of 255.255.240.0, what is the maximum number of hosts per subnet .
a. 240 b. 255 c. 4094 d. 65534
30. What is the difference: between o(log n) and o(log n^2), where both logarithems have base 2 .
a. o(log n^2) is bigger b. o(log n) is bigger
c. no difference
31. For a class what would happen if we call a class’s constructor from with the same class’s constructor .
a. compilation error b. linking error
c. stack overflow d. none of the above
32. “new” in c++ is a: .
a. library function like malloc in c
b. key word c. operator
d. none of the above
33. Which of the following information is not contained in an inode .
a. file owner b. file size
c. file name d. disk address
34. What’s the number of comparisons in the worst case to merge two sorted lists containing n elements each .
a. 2n b.2n-1 c.2n+1 d.2n-2
35. Time complexity of n algorithm T(n), where n is the input size ,is T(n)=T(n-1)+1/n if n>1 otherwise 1 the order of
this algorithm is .
a. log (n) b. n c. n^2 d. n^n
36. The number of 1’s in the binary representation of 3*4096+ 15*256+5*16+3 are .
a. 8 b. 9 c. 10 d. 12

37.设计函数 int atoi(char *s)。
38.int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 输出是多少?
39.解释局部变量、全局变量和静态变量的含义。
40.解释堆和栈的区别。
  栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
堆:一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于
链表.
41.论述含参数的宏与函数的优缺点。
答:
1.函数调用时,先求出实参表达式的值,然后带入形参。而使用带参的宏只是进行简单的字符替换。
2.函数调用是在程序运行时处理的,分配临时的内存单元;而宏展开则是在编译时进行的,在展开时并不分配内存单元,不进行值的传递处理,也没有“返回值”的概念。
3.对函数中的实参和形参都要定义类型,二者的类型要求一致,如不一致,应进行类型转换;而宏不存在类型问题,宏名无类型,它的参数也无类型,只是一个符号代表,展开时带入指定的字符即可。宏定义时,字符串可以是任何类型的数据。
4.调用函数只可得到一个返回值,而用宏可以设法得到几个结果。
5.使用宏次数多时,宏展开后源程序长,因为每展开一次都使程序增长,而函数调用不使源程序变长。
6.宏替换不占运行时间,只占编译时间;而函数调用则占运行时间(分配单元、保留现场、值传递、返回)。
一般来说,用宏来代表简短的表达式比较合适。

42. 以下三条输出语句分别输出什么?[C易]
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
  const char str4[] = "abc";
  const char* str5 = "abc";
const char* str6 = "abc";
cout << boolalpha << ( str1==str2 ) << endl; // 输出什么?
cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?
cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?
43. 非C++内建型别 A 和 B,在哪几种情况下B能隐式转化为A?[C++中等]
答:
a. class B : public A { ……} // B公有继承自A,可以是间接继承的
b. class B { operator A( ); } // B实现了隐式转化为A的转化
c. class A { A( const B& ); } // A实现了non-explicit的参数为B(可以有其他带默认值的参数)构造函数
d. A& operator= ( const A& ); // 赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个
44. 以下代码中的两个sizeof用法有问题吗?[C易]
void UpperCase( char str[] ) // 将 str 中的小写字母转换成大写字母
{
for( size_t i=0; i if( 'a'<=str[i] && str[i]<='z' )
str[i] -= ('a'-'A' );
}
char str[] = "aBcDe";
cout << "str字符长度为: " << sizeof(str)/sizeof(str[0]) << endl;
UpperCase( str );
cout << str << endl;
45. 以下代码有什么问题?[C难]
void char2Hex( char c ) // 将字符以16进制表示
{
char ch = c/0x10 + '0'; if( ch > '9' ) ch += ('A'-'9'-1);
char cl = c%0x10 + '0'; if( cl > '9' ) cl += ('A'-'9'-1);
cout << ch << cl << ' ';
}
char str[] = "I love 中国";
for( size_t i=0; i char2Hex( str[i] );
cout << endl;
46. 以下代码有什么问题?[C++易]
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}
*** Test b();//定义了一个函数
47. 以下代码有什么问题?[C++易]
cout << (true?1:"1") << endl;
8. 以下代码能够编译通过吗,为什么?[C++易]
unsigned int const size1 = 2;
char str1[ size1 ];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[ size2 ];
48. 以下代码中的输出语句输出0吗,为什么?[C++易]
struct CLS
{
int m_i;
CLS( int i ) : m_i(i) {}
CLS()
{
CLS(0);
}
};
CLS obj;
cout << obj.m_i << endl;
49. C++中的空类,默认产生哪些类成员函数?[C++易]
答:
class Empty
{
public:
Empty(); // 缺省构造函数
Empty( const Empty& ); // 拷贝构造函数
~Empty(); // 析构函数
Empty& operator=( const Empty& ); // 赋值运算符
Empty* operator&(); // 取址运算符
const Empty* operator&() const; // 取址运算符 const
};
50. 以下两条输出语句分别输出什么?[C++难]
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?
51. 以下反向遍历array数组的方法有什么错误?[STL易]
vector array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 3 );
for( vector::size_type i=array.size()-1; i>=0; --i ) // 反向遍历array数组
{
cout << array[i] << endl;
}
52. 以下代码有什么问题?[STL易]
typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// 删除array数组中所有的2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}
53. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
答:
void* mymemcpy( void *dest, const void *src, size_t count )
{
char* pdest = static_cast( dest );
const char* psrc = static_cast( src );
if( pdest>psrc && pdest {
for( size_t i=count-1; i!=-1; --i )
pdest[i] = psrc[i];
}
else
{
for( size_t i=0; i pdest[i] = psrc[i];
}
return dest;
}
int main( void )
{
char str[] = "0123456789";
mymemcpy( str+1, str+0, 9 );
cout << str << endl;
system( "Pause" );
return 0;
}

54 线程与进程的区别
进程:(在批处理系统中)是资源分配的最小单位
线程:最独立运行的最小单位。
一个进程中可以一个或多个线程。当系统的资源分配给进程,线程从