Chinaunix首页 | 论坛 | 博客
  • 博客访问: 749936
  • 博文数量: 231
  • 博客积分: 3217
  • 博客等级: 中校
  • 技术积分: 2053
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-04 12:01
文章分类

全部博文(231)

文章存档

2015年(1)

2013年(10)

2012年(92)

2011年(128)

分类: C/C++

2012-09-19 15:52:10

堆区(heap):一般由程序员分配和释放,若程序员不释放,程序结束时可能由操作系统回收。注意它与数据结构中的堆是两回事,分配方式类似于链表。
栈区(stack):由编译器自动分配和释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中栈。
全局区(静态区)(static):全局变量和静态变量的存储时放在一块的,初始化的全局变量和静态变量在一块区域,未初始化的全局变量和为初始化的静态变量在相邻的一块区域。程序结束后由系统释放。
文字常量去:常量字符串就是放在这里。程序结束后由系统释放
程序代码区:存放函数体的二进制代码。
 
栈:只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出。
堆:

点击(此处)折叠或打开

  1. 二分法查找:
  2.  #include<stdio.h>
  3.   #include<stdlib.h>
  4.   int sea(int *a,int key,int low,int high)
  5.   {
  6.   int mid;
  7.   if(low>high)
  8.   return -1;
  9.   mid=(low+high)/2;
  10.   if(a[mid]==key) return mid;
  11.   else if(a[mid]>key) return sea(a,key,low,mid-1);
  12.   else return sea(a,key,mid+1,high);
  13.   }
  14.   
  15.   void main()
  16.   {
  17.   int a[]={1,2,3,4,5,6,7,8,9,12,13,45,67,99,101,111,123,134,565,777};
  18.   int i=sea(a,99,0,sizeof(a)/sizeof(a[0])-1);
  19.   printf("i=%d\n",i);}
c++实现代码:

点击(此处)折叠或打开

  1. #include<iostream>
  2.  #define N 10
  3.  using namespace std;
  4.  int main()
  5.  {
  6.  int a[N],front,end,x,mid,i;
  7.  cout<<"enter number: "<<endl;
  8.  for(i=0;i<10;i++)
  9.  cin>>a[i];
  10.  cout<<"shu ru yao cha zhao shu x= "<<endl;
  11.  cin>>x;
  12.  front=0;
  13.  end=N-1;
  14.  mid=(front+end)/2;
  15.  while(front<end && a[mid]!=x)
  16.  {
  17.  if(a[mid]<x) front=mid+1;
  18.  if(a[mid]>x) front=mid-1;
  19.  mid=(front+end)/2;
  20.  }
  21.  if(a[mid]!=x)
  22.  cout<<"can not find x"<<endl;
  23.  else
  24.  cout<<"find it"<<mid+1<<endl;
  25.  return 0;
  26.  }

字符串拷贝:
版本v1:src源地址,dest目的地址
返回拷贝好的地址;如果重叠或者出错,无定义
异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。
char *strcpy(char *dest,const char *src)
{
assert((dest!=NULL)&&(src!=NULL));
char *to=dest;
while((*dest++=*src++)!='\0')
{
NULL;
}
return to;
}

char *strcpy(char *dest,const char *src)
{
char *d=dest;
char c;
while(*src)
*dest++=*src++;
*dest='\0';
return d;
}


strlen函数实现代码:
int strlen_a(const char *src){
int length=0;
while(*str++)
++length;
return length;
}

char *strcat(char *s,const char *append)
{
char *save=s;//保存s的首地址到指针save中
for(;*s;*s++);//将s所指向的字符串首指针移到串尾处
while((*s++=*append++)!='\0');
return save;
}


int strcmp ( const char* src, const char* dst ) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); }

请写一个C函数,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1 
int checkcpu()
{
{
union w{int a;
     char b;}c;
  c.a=1;
return (c.b==1);
}
}


剖析: 

嵌入式系统开发者应该对Little-endian和Big-endian模式非常了解.采用Little-endian模式的CPU对操作数的存放方式是从低字节到高字节,而Big-endian模式对操作数的存放方式是从高字节到低字节.例如,16bit宽的数0x1234在Little-endian模式CPU内存中的存放方式(假设从地址0x4000开始存放)为: 

内存地址 
0x4000 
0x4001 

存放内容 
0x34 
0x12 


而在Big-endian模式CPU内存中的存放方式则为: 

内存地址 
0x4000 
0x4001 

存放内容 
0x12 
0x34 


32bit宽的数0x12345678在Little-endian模式CPU内存中的存放方式(假设从地址0x4000开始存放)为:

内存地址 
0x4000 
0x4001 
0x4002 
0x4003 

存放内容 
0x78 
0x56 
0x34 
0x12 


而在Big-endian模式CPU内存中的存放方式则为: 

内存地址 
0x4000 
0x4001 
0x4002 
0x4003 

存放内容 
0x12 
0x34 
0x56 
0x78 


联合体union的存放顺序是所有成员都从低地址开始存放,面试者的解答利用该特性,轻松地获得了CPU对内存采用Little-endian还是Big-endian模式读写.如果谁能当场给出这个解答,那简直就是一个天才的程序员. 



2. 

char str[] = “Hello” ; 

char *p = str ; 

int n = 10; 

请计算 

sizeof (str ) = 6 (2分) 

sizeof ( p ) = 4 (2分) 

sizeof ( n ) = 4 (2分) 
void Func ( char str[100]) 



请计算 

sizeof( str ) = 4 (2分) 



void *p = malloc( 100 ); 

请计算 

sizeof ( p ) = 4 (2分) 




3、在C++程序中调用被 C编译器编译后的函数,为什么要加 extern “C”? (5分) 

答:C++语言支持函数重载,C语言不支持函数重载.函数被C++编译后在库中的名字与C语言的不同.假设某个函数的原型为: void foo(int x, int y); 

该函数被C编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字. 

C++提供了C连接交换指定符号extern“C”来解决名字匹配问题. 

4.有关内存的思考题 

void GetMemory(char *p) 



p = (char *)malloc(100); 



void Test(void) 



char *str = NULL; 

GetMemory(str); 

strcpy(str, "hello world"); 

printf(str); 



请问运行Test函数会有什么样的结果? 

答:程序崩溃. 

因为GetMemory并不能传递动态内存, 

Test函数中的 str一直都是 NULL. 

strcpy(str, "hello world");将使程序崩溃. 
char *GetMemory(void) 



char p[] = "hello world"; 

return p; 



void Test(void) 



char *str = NULL; 

str = GetMemory(); 

printf(str); 



请问运行Test函数会有什么样的结果? 

答:可能是乱码. 

因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知. 











void GetMemory2(char **p, int num) 



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



void Test(void) 



char *str = NULL; 

GetMemory(&str, 100); 

strcpy(str, "hello"); 

printf(str); 



请问运行Test函数会有什么样的结果? 

答: 

(1)能够输出hello 

(2)内存泄漏 


   









void Test(void) 



char *str = (char *) malloc(100); 

strcpy(str, “hello”); 

free(str); 

if(str != NULL) 



strcpy(str, “world”); 

printf(str); 





请问运行Test函数会有什么样的结果? 

答:篡改动态内存区的内容,后果难以预料,非常危险. 

因为free(str);之后,str成为野指针, 

if(str != NULL)语句不起作用.
阅读(1059) | 评论(0) | 转发(1) |
0

上一篇:排序

下一篇:栈学习

给主人留下些什么吧!~~