不看c++ primer  永远不知道自己基础有多差

函数的參数传值一般有两种方式:值传递,引用传递。

值传递有以下两种形式:

void func( int a )
{
//
} void func1( int *a )
{
//
}

对于 func 和func1都是通过拷贝内存来实现的

func1

int m = 10 ;
func1( int *a ) ;
//处理过程为: a = &m ;
//然后通过指针 *a 对 m进行间接操作

传引用

void func2( int &a )
{
//
}

引用就是变量的一个别名,不会发生内存的拷贝

典型的面试题:

void GetMemory1(char *p)
{
p = (char *)malloc(100);
} void Test1(void)
{
char *str = NULL;
GetMemory1(str);
strcpy(str, "hello world");
printf(str);
}
<p>
</p><pre name="code" class="cpp">// p = str;
// p = malloc(...);
//p和str有半毛线关系?
char *GetMemory2(void)
{
char p[] = "hello world";
return p;
}
void Test2(void)
{
char *str = NULL;
str = GetMemory2();
printf(str); }
char *GetMemory3(void)
{
return
"hello world";
}
void Test3(void)
{
char *str = NULL;
str = GetMemory3();
printf(str);}
//Test3 中打印hello world,由于返回常量区,并且并没有被改动过。Test2中不一定能打印出hello world,由于指向的是栈。
void GetMemory4(char **p, int num)
{
*p = (char *)malloc(num); }
void Test4(void)
{
char *str = NULL;
GetMemory3(&str, 100);
strcpy(str, "hello");
printf(str);
}//内存没释放
void Test5(void)
{
char *str = (char *) malloc(100);
strcpy(str, "hello");
free(str);
if(str != NULL)
{
strcpy(str, "world");
printf(str);
}
}//str为野指针,打印的结果不得而知
void Test6()
{
char *str=(char *)malloc(100);
strcpy(str, "hello");
str+=6;
free(str);
if(str!=NULL)
{
strcpy(str, "world"); printf(str);
}
}//VC断言失败,执行错误

c++ primer 函数传值1的更多相关文章

  1. jquery的ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...

  2. [java学习笔记]java语言基础概述之函数的定义和使用&函数传值问题

    1.函数 1.什么是函数? 定义在类中的具有特定功能的一段独立小程序. 函数也叫做方法 2.函数的格式 修饰符   返回值类型    函数名(参数类型  形式参数1, 参数类型  形式参数2-) { ...

  3. js 基础 函数传值

    让我忽略的函数传值问题 function box(num){ num += 10;  // num(有色的num) 实际就是arguments[0] ,如果参数没有num,则函数体的num(有色的nu ...

  4. ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({ dataType : ‘json',type : ‘POST',url : ‘ht ...

  5. C++函数传值问题

    在做题出现个神奇的事情,C++的传值跟其他OOP语言不一样.首先做个测试,看看下面输出结果是什么? void F(int a,int b,int c){ cout<<a<<b& ...

  6. C++函数传值调用

    C++的函数的参数调用是传值方式. 想要改变传值调用,有引用和指针两种方式.其中,引用的实现机理也是通过一个指针,但是具体和指针传值的方式又不一样.具体见:C++中的指针与引用 对于指针传值,其实实际 ...

  7. c#基础 函数传值

    随便新建个控制台程序做个演示! 1.最基础,最普通的传值方式: static void Main(string[] args) { ); Console.WriteLine("x:" ...

  8. go slice与函数传值的理解

    go语言中所有的传值方式都是传值操作. 今天遇到了以下代码: func main(){ slice := make([],) fmt.Println(slice) change(s) fmt.Prin ...

  9. 关于swap函数传值的问题

    #include <stdio.h> void swap(int * p3,int * p4); int main() {  int a = 9;  int b = 8;  int * p ...

随机推荐

  1. [Network]Wireless and Mobile

    Wireless 1 Introduction 1.1 Elements 1. Wireless Hosts Wireless does not mean mobility. 2. Base Stat ...

  2. PVPlayer的实现方式

    关于opencore下多媒体播放,在mediaserver进程里面仅仅有一行代码: MediaPlayerService::instantiate(); 这行代码的作用是初始化一个MediaPlaye ...

  3. 管理处理器的亲和性(affinity)

    管理处理器的亲和性(affinity) 管理处理器的亲和性(affinity)

  4. Extjs4 RowEditing 的使用和更新方法

    如何灵活快速的掌握RowEditing组件的应用,应大家的要求,今天给大家具体讲下该组件的使用. 1.创建 var rowEditing = Ext.create('Ext.grid.plugin.R ...

  5. ubuntu12 下怎样上网

    1,host 就是WIN7 使用WIFI上网 2.打开设置你的VM 8 edit--Virutal network editor--VMnet0--Bridged (connect VMs to di ...

  6. HdU 4046 Panda 段树

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 意甲冠军:到了bw组成的长度为n的字符串(n<=50000).有m次操作(m<=1000 ...

  7. hdu3652(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:求1~n含有13且能被13整除的数的个数. 分析:数位dp,dp数组加一维来维护到pos位 ...

  8. android学习---SeekBar和RatingBar

    SeekBar 拖动条:拖动条和滚动栏类似,当是拖动条能够拖动滑块改变进度 RatingBar 星级评分条:星级评分条与拖动条相似 SeekBar特有的xml属性 android:thumb    指 ...

  9. NVL NVL2 NVLIF

    ========Oracle=======NVL (expr1, expr2)->expr1为NULL,返回expr2:不为NULL,返回expr1.注意两者的类型要一致

  10. Flash-使用变形面板制作花朵

    在Flash中利用"变形"面板的"重置选取和变形"button(在变形面板右下角),能够自己主动将对象进行创造性变形地画图 步骤: (1)先导入一幅图像 (2) ...