将地址分配在C一个结构指针数组成员 [英] Assigning an address to a struct pointer array member in C

查看:161
本文介绍了将地址分配在C一个结构指针数组成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些指针算术相当大的麻烦。我的认为的我得到的概念(指针变量指向的内存地址,正常变量指向的数据),但我相信我的问题是与语法( *&安培;,( *)*()等)

Having considerable trouble with some pointer arithmatic. I think I get the concepts (pointer variables point to a memory address, normal variables point to data) but I believe my problem is with the syntax (*, &, (*), *(), etc.)

我想要做的就是建立一个自定义结构的动态数组(即指针数组堆结构),和我的接口提供了两个方法,ad_to_obj_array(这需要要添加的对象,哪些可以为空数组为空的)和obj_array_dustbin(这只是需要阵列处置,也设置的内容,堆OBJ文件)。前者呈现以下。

What I want to do is build dynamic arrays of a custom struct (i.e. arrays of pointers to heap structs), and my interface provides two methods, "ad_to_obj_array" (which takes the object to add and the array which can be null for empty) and "obj_array_dustbin" (which just takes the array to dispose, also disposing of the contents, the heap objs). The former is rendered below.

对象的细节并不重要(和结构已更名反正),但我解决普遍的问题是下面的,我会很感激,如果你能发现这个错误。编译器抱怨无效的左值,在那里我试图在一个指针数组分配中的RHS的指针指针值地址堆结构:

The details of the objects are not important (and the struct has been renamed anyway) but my solution to the general problem is below, and I'd be grateful if you can spot the error. The compiler is complaining about an invalid lvalue, where I try and assign the address in the pointer on the RHS to the pointer value in an array of pointers to heap structs:

#define NUM_ELEM(x) (sizeof (x) / sizeof (*(x)))

obj* add_to_obj_array(obj* new_obj, obj* array)
{
  int number_of_elements = 0;
  if (array != NULL)
  {
    number_of_elements = NUM_ELEM(array);
  }

  obj* new_array = NULL;

  /* note: I am expecting sizeof(new_obj) to return the size of an obj* 
     to go into the array of pointers. */
  if ( NULL ==
       (new_array = (obj*)malloc((number_of_elements + 1)* sizeof(new_obj))) )
  {
    /* memory request refused :( */
    return NULL;
  }

  /* copy the old array pointers into the new array's pointer slots: */
  int i;
  for (i = 0; i < number_of_elements; i++)
  {
    &(new_array[i]) = &(array[i]);
  }

  /* add the new item to the end (assign pointer value directly): */
  new_array[number_of_elements] = new_obj;

  if (number_of_elements > 0)
  {
    free(&array);
  }

  return new_array;
}

现在,我已经试过了违规线的排列如下:

Now, I have tried the following permutations of the offending line:

  &(new_array[i]) = &(array[i]);
  *(new_array[i]) = &(array[i]);
  new_array[i] = &(array[i]);

和所有给予这样或那样的编译错误。我相当肯定的右手边就是旧数组的第i个元素的地址,但如何我分配给新的第i个元素,当数组元素是指向结构?

and all give a compiler error of one sort or another. I am fairly sure that the right hand side is the address of the ith element of the old array, but how to I assign to the ith element of the new, when the elements of the array are pointers to structs?

编辑 - 请注意,宏NUM_ELEM上述方法无效;它总是低于返回1见@Merlyn摩根 - 格雷厄姆的回答为什么。

EDIT - please note, the macro NUM_ELEM above DOES NOT WORK; it will always return 1. See @Merlyn Morgan-Graham's answer below for why.

推荐答案

根据你的描述,你出发错了,所以你的时间去复制的东西,没有什么可以做的是可能的工作。

Based on your description, you're starting off wrong, so by the time you get to copying things, nothing you can do is likely to work.

现在,你已经定义 new_array (和,presumably,阵列)作为指针到 OBJ 。结果是这样的:

Right now, you've defined new_array (and, presumably, array) as a pointer to obj. The result looks like this:

在这种情况下,有一个指针的对象的动态分配的数组。当/如果展开配置,你需要自己复制所有对象。

In this case, you have a pointer to a dynamically allocated array of objects. When/if you expand the allocation, you'll need to copy all the objects themselves.

根据你的描述:(即指针数组堆结构),你想要的是一个指针数组。如果你想自动分配的指针数组,你的定义将如下所示:

According to your description: "(i.e. arrays of pointers to heap structs)", what you want is an array of pointers. If you want to allocate that array of pointers automatically, your definition would look like:

obj *array[NUMBER];

我的猜测是不是你想要什么,虽然。 presumably,要分配该数组动态,以及。这将是这样的:

My guess is that's not what you want though. Presumably, you want to allocate that array dynamically as well. That would look like this:

在这种情况下, new_array 阵列将每一个需要被定义为指针的指针的 OBJ 。然后你会分配一个指针数组(即指针到尽可能多的 OBJ 取值为你想),并在一个 OBJ <各有点对点/ code>:

In this case, new_array and array will each need to be defined as a pointer to pointer to obj. You'd then allocate an array of pointers (i.e., pointers to as many objs as you want) and have each point point at an obj:

obj **new_array;

// allocate an array of pointers with space to point at more items:    
new_array = malloc(sizeof(obj *) * new_elements);

// copy the pointers to the current items to the new array:
for (i=0; i<current_elements; i++)
    new_array[i] = array[i];

这样做的好处是,当你做的复制,仅复制指针的,而不是对象本身。尤其是对于大型的物体,这样可以节省精力大量。代价是使用元素通过一个间接这一翻译的两个层面去,所以引用可能会比较慢(虽然很少的速度较慢,尤其是在相对高性能的处理器)。

The advantage of this is that when you do the copying, you only copy pointers, not the objects themselves. Especially with large objects, this can save a substantial amount of effort. The tradeoff is that using an element goes through two levels of indirection intead of one, so the reference may be slower (though rarely much slower, especially on a relatively high-performance processor).

由于@rerun已经指出的那样,在这两种情况下,你可能想使用的realloc 。特别是,本可能的能够就地扩大分配,并且避免经常复制数据。当然,这不能保证,但至少你给它一个机会;如果您的malloc 并复制每次,您消除优化甚至有可能。

As @rerun already pointed out, in either case you probably want to use realloc. In particular, this might be able to expand an allocation "in place", and avoid copying data as often. Of course, that's not guaranteed, but at least you're giving it a chance; if you malloc and copy every time, you eliminate even the possibility of that optimization.

这篇关于将地址分配在C一个结构指针数组成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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