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 ...
随机推荐
- 更改App.config里的值并保存
using System; using System.Collections.Generic; using System.Configuration; using System.IO; using S ...
- 从零讲Java,给你一条清晰地学习道路!该学什么就学什么!
从零讲JAVA ,给你一条 清晰地学习道路!该学什么就学什么! 1.计算机基础: 1.1数据机构基础: 主要学习:1. ...
- mac 下安装caffe(二)
使用Anaconda Python 1.brew edit opencv args << "-DPYTHON_LIBRARY=#{py_lib}/libpython2.7.#{d ...
- iOS 中删除cookie的正确做法
需求:删除 qq 登录的 cookie,保证下次打开 qq 登录页面不会默认登录 实现: NSString *url =@"https://w.mail.qq.com/cgi-bi ...
- ALSA声卡驱动中的DAPM详解之四:在驱动程序中初始化并注册widget和route
前几篇文章我们从dapm的数据结构入手,了解了代表音频控件的widget,代表连接路径的route以及用于连接两个widget的path.之前都是一些概念的讲解以及对数据结构中各个字段的说明,从本章开 ...
- 使用 StackExchange.Redis 封装属于自己的 RedisHelper
目录 核心类 ConnectionMultiplexer 字符串(String) 哈希(Hash) 列表(List) 有序集合(sorted set) Key 操作 发布订阅 其他 简介 目前 .NE ...
- C#使用Quartz.NET详解
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
- 【POJ 2259】 Team Queue
[题目链接] http://poj.org/problem?id=2259 [算法] 由题,一个人入队时,若这个人所在的组已经有人在队列中,则加入队列,否则排到队末 因此我们发现,这个队列一定是由连续 ...
- ajax异步文件上传和进度条
一.ajax异步文件上传 之前有说过在form表单内的文件上传,但是会刷新页面,下面就来实现不刷新页面的异步文件上传 <div class="uploding_div"> ...
- Kubernetes 集群中使用 Helm 搭建 Spinnaker
在我们部署Spinnaker之前,我们需要一个YAML格式的配置文件,它会包含了一些配置信息.可以从Spinnaker Helm Chart repository[2]获得这个文件. $curl -L ...