如何分配内存并将其(通过指针参数)返回给调用函数? [英] How can I allocate memory and return it (via a pointer-parameter) to the calling function?

查看:32
本文介绍了如何分配内存并将其(通过指针参数)返回给调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个不同的函数中有一些代码,看起来像这样:

I have some code in a couple of different functions that looks something like this:

void someFunction (int *data) {
  data = (int *) malloc (sizeof (data));
}

void useData (int *data) {
  printf ("%p", data);
}

int main () {
  int *data = NULL;

  someFunction (data);

  useData (data);

  return 0;
}

someFunction()useData() 在单独的模块(*.c 文件)中定义.

someFunction () and useData () are defined in separate modules (*.c files).

问题是,虽然 malloc 工作正常,并且分配的内存在 someFunction 中可用,但是一旦函数返回,相同的内存将不可用.

The problem is that, while malloc works fine, and the allocated memory is usable in someFunction, the same memory is not available once the function has returned.

可以在此处查看程序运行示例,输出显示了各种内存地址.

An example run of the program can be seen here, with output showing the various memory addresses.

有人可以向我解释我在这里做错了什么,以及如何让这段代码正常工作?

Can someone please explain to me what I am doing wrong here, and how I can get this code to work?

所以似乎我需要使用双指针来做到这一点 - 当我实际上需要使用双指针时,我将如何做同样的事情?所以例如数据是

So it seems like I need to use double pointers to do this - how would I go about doing the same thing when I actually need to use double pointers? So e.g. data is

int **data = NULL; //used for 2D array

那么我是否需要在函数调用中使用三重指针?

Do I then need to use triple pointers in function calls?

推荐答案

你想使用指针:

void someFunction (int **data) {
  *data = malloc (sizeof (int));
}

void useData (int *data) {
  printf ("%p", data);
}

int main () {
  int *data = NULL;

  someFunction (&data);

  useData (data);

  return 0;
}

为什么?好吧,您想更改主函数中的指针 data.在 C 中,如果您想更改作为参数传入的某些内容(并使该更改显示在调用者的版本中),则必须传入一个指向要更改的任何内容的指针.在这种情况下,你想改变的东西"是一个指针——所以为了能够改变那个指针,你必须使用一个指向指针的指针......

Why? Well, you want to change your pointer data in the main function. In C, if you want to change something that's passed in as a parameter (and have that change show up in the caller's version), you have to pass in a pointer to whatever you want to change. In this case, that "something you want to change" is a pointer -- so to be able to change that pointer, you have to use a pointer-to-pointer...

请注意,除了您的主要问题之外,代码中还有另一个错误:sizeof(data) 为您提供了存储指针所需的字节数(32 位为 4 个字节)OS 或 64 位操作系统上的 8 个字节),而您确实想要存储指针指向的内容(int,即 4 个字节)所需的字节数大多数操作系统).因为通常 sizeof(int *)>=sizeof(int),这可能不会引起问题,但需要注意.我已经在上面的代码中更正了这一点.

Note that on top of your main problem, there was another bug in the code: sizeof(data) gives you the number of bytes required to store the pointer (4 bytes on a 32-bit OS or 8 bytes on a 64-bit OS), whereas you really want the number of bytes required to store what the pointer points to (an int, i.e. 4 bytes on most OSes). Because typically sizeof(int *)>=sizeof(int), this probably wouldn't have caused a problem, but it's something to be aware of. I've corrected this in the code above.

这里有一些关于指针到指针的有用问题:

Here are some useful questions on pointers-to-pointers:

指向指针的指针在 C 中如何工作?

用于多级指针取消引用?

这篇关于如何分配内存并将其(通过指针参数)返回给调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆