试题1:

 void test1()
{
 char string[];
 char* str1 ="";
 strcpy( string, str1 );
}

试题2:

 void test2()
{
 charstring[],str1[];
 int i;
 for(i=; i<; i++)
 {
  str1 ='a';
 }
 strcpy( string, str1 );
}

解答:
  试题1字符串str1需要11个字节才能存放下(包括末尾的’\0’),而string只有10个字节的空间,strcpy会导致数组越界;

  对试题2,如果面试者指出字符数组str1不能在数组内结束可以给3分;如果面试者指出strcpy(string,str1)调用使得从str1内存起复制到string内存起所复制的字节数具有不确定性可以给7分,在此基础上指出库函数strcpy工作方式的给10分;

正确的写法为:

 void test1()
{
 char str[];
 char* str1 ="";
 strcpy( str, str1 );
}
 char str[], str1[];
int i;
for (i = ; i<; i++)
{
str1[i] = 'a';
}
str1[i] = '\0';
strcpy_s(str, str1);

试题3:

void GetMemory( char*p )
{
 p = (char*) malloc( );
}
void Test( void )
{
 char*str = NULL;
 GetMemory( str );
 strcpy( str, "hello world" );
 printf( str );
}

试题3传入中GetMemory(char *p )函数的形参为字符串指针,在函数内部修改形参并不能真正的改变传入形参的值,执行完

char *str = NULL;
GetMemory( str ); 
后的str仍然为NULL;

试题4:

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

试题4中
char p[] = "hello world"; 
return p; 
  的p[]数组为函数内的局部自动变量,在函数返回后,内存已经被释放。

试题5:

 void GetMemory( char**p, int num )
{
 *p = (char*) malloc( num );
}
void Test( void )
{
 char*str = NULL;
 GetMemory( &str, );
 strcpy( str, "hello" );
 printf( str );
}

在GetMemory中执行申请内存及赋值语句

*p = (char *) malloc( num );
后未判断内存是否申请成功,应加上:

if ( *p == NULL )
{
 ...//进行申请内存失败处理
}

未对malloc的内存进行释放。

试题6:

 void Test( void )
{
 char*str = (char*) malloc( );
 strcpy( str, "hello" );
 free( str );
 ... //省略的其它语句
}

在执行char *str = (char *) malloc(100);后未进行内存是否申请成功的判断;另外,在free(str)后未置str为空,导致可能变成一个“野”指针,应加上:

str = NULL;

试题7:

 swap( int* p1,int* p2 )
{
 int*p;
 *p =*p1;
 *p1 =*p2;
 *p2 =*p;
}

在swap函数中,p是一个“野”指针,有可能指向系统区,导致程序运行的崩溃。在VC++中DEBUG运行时提示错误“AccessViolation”。该程序应该改为:

 swap( int* p1,int* p2 )
{
 int p;
 p =*p1;
 *p1 =*p2;
 *p2 = p;
}

 

其他

1.char *str(){char str[] = "hello world"; return str;}

这个函数返回的是局部变量的地址,调用这个函数后,这个局部变量str就释放了。

2.

void test()
{
  char str[10],str1[10];
  for(int i=0; i<10; i++)
  {
    str1[i] = 'a';
  }
  //str1[9]='\0'; //正确做法
  strcpy(str,str1);
  cout<<str<<endl;
}

str1没有结束符,应在strcpy之前加上str1[9]='\0';

3.

class A{const int size = 0;};这个类声明正确吗?为什么?

错误,常量必须在构造函数的初始化列表中初始化,或将其设置为static

class A{A(){const int size = 0;}};或

class A{static const int size = 0;}

4.

class base
{
private:
  int i;
public:
  base(int x){i = x;}
};

class derived:public base{
private:

  int i;
public:
  derived(int x,int y){i = x;}
  //derived(int x,int y):base(x){i = x;} //正确做法
};

int main(int argc, char* argv[])
{
  base a(5);
  return 0;
}

要在子类中设定初始化成员变量。

5.

char *my_cpy(char*src,int len)
{
  char dest[1024];
  memcpy(dest,src,len);
  return dest;
}

返回局部变量地址。

6.

请找出下面代码中的所以错误
说明:以下代码是把一个字符串倒序,如“abcd”倒序后变为“dcba”

1、#i nclude"string.h"
2、main()
3、{
4、 char*src="hello,world";
5、 char* dest=NULL;
6、 int len=strlen(src);
7、 dest=(char*)malloc(len);
8、 char* d=dest;
9、 char* s=src[len];
10、 while(len--!=0)
11、 d++=s--;
12、 printf("%s",dest);
13、 return 0;
14、}
答:
方法1:
int main(){
char* src = "hello,world";
int len = strlen(src);
char* dest = (char*)malloc(len+1);//要为\0分配一个空间
char* d = dest;
char* s = &src[len-1];//指向最后一个字符
while( len-- != 0 )
*d++=*s--;
*d = 0;//尾部要加\0
printf("%s\n",dest);
free(dest);// 使用完,应当释放空间,以免造成内存汇泄露
return 0;
}
方法2:
#i nclude <stdio.h>
#i nclude <string.h>
main()
{
char str[]="hello,world";
int len=strlen(str);
char t;
for(int i=0; i<len/2; i++)
{
t=str[i];
str[i]=str[len-i-1]; str[len-i-1]=t;
}
printf("%s",str);
return 0;
}

改错+GetMemory问题的更多相关文章

  1. 【C++基础】内存操作 getMemory改错

    内存操作的考察点:①指针 ②变量生存期及作用范围 ③动态内存申请和释放 笔试题************************************************************* ...

  2. C++面试之GetMemory问题

    http://blog.csdn.net/zhuxiaoyang2000/article/details/8084629 #include <iostream> #include < ...

  3. JAVA程序改错 (易错题)

    JAVA程序改错 1. abstract class Name { private String name; public abstract boolean isStupidName(String n ...

  4. 关于内存 GetMemory( ) 笔试分析

    1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...

  5. YTU 2610: A改错题--体检情况分析

    2610: A改错题--体检情况分析 时间限制: 1 Sec  内存限制: 128 MB 提交: 233  解决: 161 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. ...

  6. YTU 2609: A改错题--学生信息的输入和输出

    2609: A改错题--学生信息的输入和输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 238  解决: 157 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方 ...

  7. GetMemory 函数解析

    GetMemory函数 代码1: void GetMemory(char *p){ p = (char*)malloc(100);}int main(int argc, char *argv[]){ ...

  8. GetMemory()函数

    NO1 void GetMemory(char *p) { p=(char *)malloc(100); } void Test() { char * str=NULL; GetMemory(str) ...

  9. GetMemory那一题的理解

    #include "stdafx.h" #include <iostream> void GetMemory(char *p,int num) { p = (char* ...

随机推荐

  1. UIProgressView 圆角

    里面外面都变成圆角 不用图片 直接改变layer 重点是里面外面都是圆角哦 for (UIImageView * imageview in self.progress.subviews) { imag ...

  2. spring mvc ajax

    <%@ page contentType="text/html;charset=UTF-8" %> <%@ include file="/WEB-INF ...

  3. CDZSC_2015寒假新人(1)——基础 h

    Description Ignatius was born in a leap year, so he want to know when he could hold his birthday par ...

  4. cocos2d-x创建工程批处理

    cd /d D:\cocos2d-x-2.2.2\cocos2d-x-2.2.2\tools\project-creator create_project.py -project %1 -packag ...

  5. [Leetcode] Two Sum (C++)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...

  6. Ubuntu13.04手动安装nvidia显卡驱动

    1. 下载最新版的nVidia驱动,命名为NVIDIA.run. http://www.nvidia.com/page/drivers.html 2.编辑blacklist.conf. sudo ge ...

  7. 异常处理与调试6 - 零基础入门学习Delphi55(完)

    调试(Debug) 让编程改变世界 Change the world by program 使用调试窗口 为方便调式程序,Delphi中提供了许多调试窗口,给开发人员的调试工作带来了极大的便利. 断点 ...

  8. Logging in Java

    Common logging: Log4j1仅仅作为一个实际的日志框架,commons-logging作为门面,统一各种日志框架的混乱格局 基本的commons logging Dependency: ...

  9. connectionStrings基本配置

    常用connectionStrings配置: <connectionStrings>   <add       name="LocalSqlServer"     ...

  10. win7 Oracle 11g安装及安装中遇到的问题

    根据自己的系统从oracle官方下载安装包,官方地址:http://www.oracle.com/index.html win7的oracle 11g 安装包(2个): http://223.20.2 ...