Getmemory问题
题目一: [cpp] view plaincopy void GetMemory( char *p )
{
p = (char *) malloc( );
} void Test( void )
{
char *str = NULL;
GetMemory( str );
strcpy( str, "hello world" );
printf( str );
} 【运行错误】传入GetMemory(char* p)函数的形参为字符串指针,在函数内部修改形参并不能真正的改变传入形参的值。执行完 [cpp] view plaincopy char *str = NULL;
GetMemory( str ); 后的str仍然为NULL。编译器总是要为每个参数制作临时副本,指针参数p的副本是_p,编译器使_p=p。如果函数体内的程序修改了_p的内容,就导 致参数p的内容作相应的修改,这就是指针可以用作输出参数的原因。在本例中,_p申请了新的内存,只是把_p所指的内存地址改变了,但是p丝毫未变。所以 GetMemory并不能输出任何东西。事实上,每执行一次GetMemory就会泄露一块内存,因为没有用free释放内存。 题目二: [cpp] view plaincopy char *GetMemory( void )
{
char p[] = "hello world";
return p;
} void Test( void )
{
char *str = NULL;
str = GetMemory();
printf( str );
} 【运行错误】GetMemory中的p[]为函数内的局部自动变量,在函数返回后,内存已经被释放。这是很多程序员常犯的错误,其根源在于不理解变量的生 存期。用调试器逐步跟踪Test,发现执行str=GetMemory语句后str不再是NULL指针,但是str的内容不是"hello world",而是垃圾。 题目三:
[cpp] view plaincopy void GetMemory( char **p, int num )
{
*p = (char *) malloc( num );
} void Test( void )
{
char *str = NULL;
GetMemory( &str, );
strcpy( str, "hello" );
printf( str );
} 【运行正确,但有内存泄露】题目三避免了题目一的问题,传入GetMemory的参数为字符串指针的指针,但是在GetMemory中执行申请及赋值语句
[cpp] view plaincopy *p = (char *) malloc( num ); 后未判断内存是否申请成功,应加上
[cpp] view plaincopy if ( *p == NULL )
{
...//进行申请内存失败处理
} 也可以将指针str的引用传给指针p,这样GetMemory函数内部对指针p的操作就等价于对指针str的操作: [cpp] view plaincopy void GetMemory( char *&p) //对指针的引用,函数内部对指针p的修改就等价于对指针str的修改
{
p = (char *) malloc( );
} void Test(void)
{
char *str=NULL;
GetMemory(str);
strcpy( str, "hello world" );
puts(str);
} 题目四: [cpp] view plaincopy void Test( void )
{
char *str = (char *) malloc( );
strcpy( str, "hello" );
free( str );
... //省略的其它语句
} 【运行正确,但有内存泄露】题目四与题目三存在同样的问题,在执行malloc后未进行内存是否申请成功的判断。此外,在free(str)后未置str为空,导致可能变成一个"野指针",应加上 [cpp] view plaincopy str = NULL; 题目三的Test函数中也未对malloc的内存进行释放。 题目五: [cpp] view plaincopy char* GetMemory(int num)
{
char* p = (char*)malloc();
return p;
} void Test(void)
{
char* str = NULL;
str = GetMemory();
strcpy(str, "hello");
cout<<str<<endl;
} 【运行正确】注意题目五和题目二的区别。虽然都是局部变量,但题目五用函数返回值来传递动态内存;而题目二return语句返回指向"栈"内存的指针,因为该内存在函数结束时自动消亡。
题目六: [cpp] view plaincopy char* GetMemory(void)
{
char* p = "hello world";
return p;
} void Test(void)
{
char* str = NULL;
str = GetMemory();
cout<<str<<endl;
}<strong> </strong> 【运行正确,但不合理】虽然Test运行不会出错,但是函数GetMemory的设计概念却是错误的。因为GetMemory内的"hello world"是常量字符串,位于静态存储区,它在程序生命期内恒定不变。无论什么时候调用GetMemory,它返回的始终是同一个"只读"的内存块。例 如,如想执行 [cpp] view plaincopy strcpy(str, "hello test"); 则程序会中断,并提示内存错误。 题目七: [cpp] view plaincopy int* GetMemory(int* ptr)
{
ptr = new int();
return ptr;
} int main()
{
int *ptr1 = , *ptr2 = ;
ptr1 = GetMemory(ptr2);
if(ptr1) { cout<<*ptr1<<'\n'; } else { cout<<"ptr1 == NULL\n"; }
if(ptr2) { cout<<*ptr2<<'\n'; } else { cout<<"ptr2 == NULL\n"; } system("pause");
return ;
} 程序输出: ptr2 == NULL 输入任意表达式输出结果
表达式中包括+,-, *,/ ,(, ) 和空格
算法思路:
: 去掉表达式中的括号,将表达式转化为后缀表达式
栈中保存运算符和括号
) '-' '+' :如果栈顶元素为'(', 压入栈中,否则放入后缀表达式
) ' ': ++i;
) '*' '/' : 如果栈顶元素不为'(',且栈顶元素为'*' 或'/' 弹出栈顶元素放入后缀表达式,否则压入栈中
) '' ~ '' : 放入后缀表达式,如果下一个元素不是数字,还放入'#'到后缀表达式,作为数字结束标志
Getmemory问题的更多相关文章
- C++面试之GetMemory问题
http://blog.csdn.net/zhuxiaoyang2000/article/details/8084629 #include <iostream> #include < ...
- 关于内存 GetMemory( ) 笔试分析
1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...
- 【C++基础】内存操作 getMemory改错
内存操作的考察点:①指针 ②变量生存期及作用范围 ③动态内存申请和释放 笔试题************************************************************* ...
- GetMemory 函数解析
GetMemory函数 代码1: void GetMemory(char *p){ p = (char*)malloc(100);}int main(int argc, char *argv[]){ ...
- 改错+GetMemory问题
试题1: void test1() { ]; "; strcpy( string, str1 ); } 试题2: void test2() { charstring[],str1[]; in ...
- GetMemory()函数
NO1 void GetMemory(char *p) { p=(char *)malloc(100); } void Test() { char * str=NULL; GetMemory(str) ...
- GetMemory那一题的理解
#include "stdafx.h" #include <iostream> void GetMemory(char *p,int num) { p = (char* ...
- getMemory的经典例子
//NO.1:程序首先申请一个char类型的指针str,并把str指向NULL(即str里存的是NULL的地址,*str为NULL中的值为0),调用函数的过程中做了如下动作:1申请一个char类型的指 ...
- Delphi中那些容易混淆的基础(@、^、Addr、Pointer,Move、CopyMemory,GetMem和FreeMem、GetMemory和FreeMemory、New和Dispose、StrAlloc和StrDispose、AllocMem)
@.^.Addr.Pointer Delphi(Pascal)中有几个特殊的符号,如@.^等,弄清楚这些符号的运行,首先要明白Delphi指针的一些基础知识:指针,是一个无符号整数(unsigned ...
随机推荐
- 关于SVG图片不显示
SVG图片在本地调试时.可以正常显示.可是上传到server或者虚拟主机以后不显示. 这个问题该怎么解决呢? 两种解决的方法: 第一种:在server上 IIS 或者其它Webserver上 加入 S ...
- 【POJ 2689】 Prime Distance
[题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...
- linux下.a/.so/.la目标库区别
在linux平台上编译时,常会遇到目标库的疑问,有静态库也有动态库,单个理解都不太难,但是对复杂的工程而言,一旦混合到一起去,对整个工程的理解和调用,将会造成很大困扰,本文就汇总这几种常见编译结果文件 ...
- 杂项:E-Learning
ylbtech-杂项:E-Learning 1.返回顶部 1. E-Learning:英文全称为(Electronic Learning),中文译作“数字(化)学习”.“电子(化)学习”.“网络(化) ...
- c++ pow函数
函数名称: pow 函数原型: double pow( double x, double y ); 函数功能: 计算x的y次幂 例:z=pow(x,y); x=9,y=8 z就是9 ...
- go语言Notepad++简易开发环境搭建(windows)
1.下载安装go语言:https://golang.org/dl/选择对应的平台,建议使用msi安装包,这个会帮你配置好环境变量(也许需要重启)对应的环境变量有: GOROOT - C:\Go\PAT ...
- [ BZOJ 3038 & 3211 / SPOJ GSS4 ] 上帝造题七分钟2 / 花神游历各国
\(\\\) \(Description\) 给出一个长度为\(N\)的数列,共进行\(M\)次操作: \(1\ L\ R\):查询\([L,R]\)区间和. \(2\ L\ R\):对\([L,R] ...
- mac中显示隐藏文件和.开头的文件
在控制台中执行一下命令,即可在finder中看到此类文件: defaults write com.apple.Finder AppleShowAllFiles YES killall Finder
- JS高级——Function
Function构造函数 可以用来新建函数对象 1.一个参数都不传的情况创建的就是一个空的函数 2.只传一个参数的情况这个参数就是函数体 3.传多个参数的情况,最后一个参数为函数体,前面的参数都是该函 ...
- JS——tab切换
排它思想: 1.先让所有的元素恢复默认值 2.再让选中的元素赋专有的值 3.干掉所有人,剩下我一个 <!DOCTYPE html> <html> <head lang=& ...