C语言有关内存和动态分配内存传递问题

C语言 有关内存的思考题

1.

void GetMemory(char *p)
{
     p = (char *)malloc(100);
}
void Test(void)
{
     char *str=NULL;
     GetMemory(str);
     strcpy(str,"Hello World");
     printf(str);
}

请问运行Test函数会有什么样的结果? 
程序编译可以通过,运行中出现内存错误。 
因为GetMemory不能传递动态内存,动态内存的传递可以用指向指针的指针来传递(例如下面的3题)Test函数中的str一直都是NULL。strcpy(str,”Hello World”);将由于str中没有开辟足够的内存空间而使内存崩溃。

2.

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

请问运行Test函数会有什么样的结果? 
程序编译通过,可能显示为乱码。 
因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是NULL,但其原先的内容已经被清除,新内容不确定,可能显示为乱码。

3.

void GetMemory2(char **p,int num)
{
     *p = (char *)malloc(num);
}
void Test(void)
{
     char *str=NULL;
     GetMemory2(&str,100);
     strcpy(str,"Hello World");
     printf(str);
}

请问运行Test函数会有什么样的结果? 
程序编译通过,能够正确输出Hello World,但是对于malloc没有free,造成内存泄漏。

4.

void Test(void)
{
     char *str=(char *)malloc(100);
     strcpy(str,"Hello");
     free(str);
     if(NULL != str)
     {
          strcpy(str,"World");
          printf(str);
     }
}

请问运行Test函数会有什么样的结果? 
程序编译通过,但是篡改动态内存区的内容,后果难以预料,非常危险。 
因为free(str);之后,str成为野指针,if(NULL != str)语句对str不起作用,在str成为野指针之后,又继续为str指针赋值,可能会缠上难以预料的后果。


三种方法实现动态分配内存的传递

#include <stdio.h>
#include <stdlib.h>
/*Three methods to return the memory area allocated by malloc*/

/* 第一种方法:返回锁分配内存的首地址,将首地址强制转换为int或long保存
  在以后调用中,将该数值请转换为所需类型对应的指针类型*/


int alloc_mem_1(int num)
{
    int *q;

    q = (int *) malloc(num);
    return (int) q;
}

/*第二种方法:
类似于第一种方法,只是将内存首地址转换为int或long保存在指针中。
以后调用传递变量时,可以将一个int或long变量取址传递即可*/


void alloc_mem_2(int *p, int num)
{
    int *q;

    q = (int *) malloc(num);
    *= (int) q;
}

/* 第三种方法:
   使用指向指针的指针保存分配存储内存的首地址
*/

void alloc_mem_3(int **p, int num)
{
    *= (int *) malloc(num);
}

int main(void)
{
    int i;
    int handle;

    /*three pointers to test three methods*/
    int *p1 = NULL;
    int *p2 = NULL;
    int *p3 = NULL;

    /*test of alloc_mem_1()*/
    printf("1: test of alloc_mem_1()\n");
    handle = alloc_mem_1(10 * sizeof(int));
    p1 = (int *)handle;
    for (= 0; i < 10; i++){
        *(p1 + i) = i ;
        printf("%d\t", p1[i]);
    }

    printf("\n");
    free(p1);

    /*test of alloc_mem_2()*/
    printf("2: test of alloc_mem_2()\n");
    alloc_mem_2(&handle, 10 * sizeof(int));
    p2 = (int *)handle;
    for (= 0; i < 10; i++){
        p2[i] = i * 2;
        printf("%d\t", p2[i]);
    }

    printf("\n");
    free(p2);

    /*test of alloc_mem_3()*/
    printf("3: test of alloc_mem_3()\n");
    alloc_mem_3(&p3, 10 * sizeof(int));
    for (= 0; i < 10; i++){
        p3[i] = i * 3;
        printf("%d\t", p3[i]);
    }

    printf("\n");
    free(p3); 

    return 0;
}


其中:
1.第一种方法是采用返回值的方式,将所分配的内存的首地址以整型之返回。在调用该函数是,将其返回值强制转换为相应类型的指针即可,我这里转换为整形指针。然后就可以以该内存为首地址,进行读写num个整形数据;
2.第二种方法的实现类似于第一种,只是将所分配内存的首地址存在一个整形指针里面;
3.第三种方法应该是使用指向指针的指针来实现动态内存的传递。这个方法在《高质量C/C++编程》中最后的一份试题中提到了这种方法。

以上三种方法中,第三种方法应该是常用的,也是容易理解的。前两种方法的实现体现在一些特别的方面,譬如,GUI编程中窗口的handle的使用,等等。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值