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

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

问题描述

我在几个不同功能的一些code,它看起来是这样的:

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.

可有人请向我解释什么,我做错了,我怎么能得到这个code的工作?

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?

推荐答案

您想使用一个指针到指针:

You want to use a pointer-to-pointer:

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;
}

为什么呢?那么,你想改变在主函数指针数据。在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...

请注意,您的主要问题的顶部,有一个在code另一个bug:的sizeof(数据)为您提供了存储所需的字节数指针(4字节在32位操作系统或8个字节在64位操作系统),而你真的要存储所需的字节数的什么指针指向的(一个 INT ,即4对大多数操作系统字节)。因为通常的sizeof(INT *)> = sizeof的(INT),这可能不会造成问题,但它的东西要注意的。我在code以上纠正了这个。

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?

<一个href=\"http://stackoverflow.com/questions/758673/uses-for-multiple-levels-of-pointer-dereferences\">Uses为指针引用的多层次?

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

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