面试题

进程与线程的区别,进程如何同步?如何避免死锁?

1.写出判断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分)

int a = 4;

(A)a += (a++); (B) a += (++a) ;(C) (a++) += a;(D) (++a) += (a++);

a = ?

答:C错误,左侧不是一个有效变量,不能赋值,可改为(++a) += a;

改后答案依次为9,10,10,11

2.某32位系统下, C++程序,请计算sizeof 的值(5分).

char str[] = “www.ibegroup.com”

char *p = str ;

int n = 10;

请计算

sizeof (str ) = ?(1)

sizeof ( p ) = ?(2)

sizeof ( n ) = ?(3)

void Foo ( char str[100]){

请计算

sizeof( str ) = ?(4)

}

void *p = malloc( 100 );

请计算

sizeof ( p ) = ?(5)

答:(1)17 (2)4 (3) 4 (4)4 (5)4

写出string类的构造函数,拷贝构造函数,析构函数,赋值函数。

#include<iostream>
using namespace std;

class String
{
public:
       String(const char *str=NULL);
       String(const String &other);
       ~String(void);
       String &operator =(const String &other);
private:
       char *m_data;
};

String::String(const char *str)
{
    cout<<"构造函数被调用了"<<endl;
    if(str==NULL)//避免出现野指针,如String b;如果没有这句话,就会出现野
                  //指针
   {
        m_data=new char[1];
        *m_data='\0';
    }
    else
    {
      int length=strlen(str);
      m_data=new char[length+1];
      strcpy(m_data,str);
    }
}
String::~String(void)
{
    delete m_data;
    cout<<"析构函数被调用了"<<endl;
}

String::String(const String &other)
{
 cout<<"赋值构造函被调用了"<<endl;
 int length=strlen(other.m_data);
 m_data=new char[length+1];
 strcpy(m_data,other.m_data);
}
String &String::operator=(const String &other)
{
      cout<<"赋值函数被调用了"<<endl;
      if(this==&other)//自己拷贝自己就不用拷贝了
                 return *this;
      delete m_data;//删除被赋值对象中指针变量指向的前一个内存空间,避免
                    //内存泄漏
      int length=strlen(other.m_data);//计算长度
      m_data=new char[length+1];//申请空间
      strcpy(m_data,other.m_data);//拷贝
      return *this;
}
void main()
{
      String b;//调用构造函数
      String a("Hello");//调用构造函数
      String c("World");//调用构造函数
      String d=a;//调用赋值构造函数,因为是在d对象建立的过程中用a来初始化
      d=c;//调用重载后的赋值函数
}


1. 写一个函数实现字符串反转

版本1 - while版
void strRev(char *s)
{
    char temp, *end = s + strlen(s) - 1;
    while( end > s)
    {
        temp = *s;
        *s = *end;
        *end = temp;
        --end;
        ++s;
    }
}

用链表实现通用堆栈

#include <assert.h>
#include <stdlib.h>

typedef struct Stack//定义结构体
{
 int num;
 struct Stack *next;
}Stack;
/*尾节点:链表堆栈中最后一个入栈的节点*/
static Stack *stack;//指向尾节点的指针

int isempty()//判断是否链式堆栈是否为空
{
 return stack == NULL;
}
int isfull()//该情况不存在
{
 return 0;
}
void push(Stack *newStack)//结构体入栈,加入链表尾节点
{
 Stack *temp = (Stack *)malloc(sizeof(Stack));
 assert( !isfull() );//链表堆栈是否满
 assert( temp != NULL );//新节点空间分配是否失败
 temp->num = newStack->num;//新节点初始化
 temp->next = stack;//新节点指向旧的尾节点
 stack = temp;//链表堆栈指针指向尾节点
}
void pop()//删除尾节点
{
 Stack *temp;
 assert( !isempty() );//链表堆栈是否空
 temp = stack;//保存旧的尾节点
 stack = temp->next;//链表堆栈指针指向次尾节点
 free(temp);//释放旧的尾节点。注意:一定要释放,否则容易引起内存泄露
}
Stack f_top()//返回尾节点,但并不删除
{
 assert( !isempty() );//链表堆栈是否为空
 return *stack;//返回尾节点,但并不删除
}
void del_stack()//删除整个链表堆栈,释放内存空间

 while( !isempty() )
  pop();
}


posted on 2011-06-30 16:50  风乔  阅读(118)  评论(0编辑  收藏  举报

导航