创建动态数组ADT

时间:2014-12-07 16:56:36

标签: c

我在创建dynArray并向其添加元素时遇到问题。我不知道如何为它分配内存。我最近才启动指针,所以任何帮助都会非常感激。

typedef struct{
  doube *darray;
  int size;
}dynArray;//dynArray structure

dynArray *createDynArray(int n);
{
  dynArray *newdynArray;//newdynArray is a pointer to the dynArray structure
  newDynArray->darray=(double*)malloc(n*double);

4 个答案:

答案 0 :(得分:1)

您需要为容器和数组本身分配空间:

typedef struct dynArray {
  double *darray;
  int size;
} dynArray;

dynArray *createDynArray(int n) {
    dynArray *newDynArray = malloc(sizeof(dynArray));
    newDynArray->size = n;
    newDynArray->darray = calloc(n, sizeof(double));
    return newDynArray;
}

void deleteDynArray(dynArray *dynArray) {
    free(dynArray->darray);
    free(dynArray);
}

答案 1 :(得分:0)

我会包括检查NULL。只是在一个案例中。

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    double *darray;
    int size;
}dynArray;

dynArray *createDynArray(int n)
{
    dynArray *newdynArray = malloc(sizeof(dynArray));
    if (newdynArray!=NULL) newdynArray->darray=malloc(n*sizeof(double));
    newdynArray->size = n;
    return newdynArray;
} 

dynArray *deleteDynArray(dynArray *olddynArray) {
    if (olddynArray!=NULL) free(olddynArray->darray);
    free(olddynArray);
    return olddynArray;
}

int main() {
    int i;
    dynArray *my;
    my = createDynArray(10);
    if (my->darray!=NULL) {
        for (i=0; i<my->size; i++)
            my->darray[i] = (float)i;
        for (i=0; i<my->size; i++)
            printf("%lf\t", my->darray[i]);
    }
    deleteDynArray(my);
    return 0;
}

答案 2 :(得分:0)

你还需要一个功能来解放 没有强制转换和使用

,你的分配风格可以更好
typedef struct{
  double *darray;
  int size;
}dynArray;//dynArray structure

dynArray *createDynArray(int n)
{
  dynArray *newdynArray = malloc(sizeof *newdynArray); // don't cast the return
  // the below is called a compound literal (or composite literal)
  *newdynArray = (dynArray){malloc(n * (sizeof *newdynArray->darray)), n};
  return newdynArray;
}

void freeDynArray(dynArray *self)
{
  free(self->darray);    // free held data
  free(self);            // free the outer structure
}

至于添加元素,您可以将其隐藏在函数后面,如果您愿意,可以增加安全性

// return 0 on success, non-zero if error
int set_item(dynArray *self, int index, double e)
{
    if (index >= self->size || index < 0) { //bounds check
       return -1;
    }
    self->darray[index] = e;
    return 0;
}

double get_item(dynArray *self, int index)
{
    if (index >= self->size || index < 0) { //bounds check
        // Note: impossible to distinguish between error and actual 1.0
        return -1.0; 
    }
    return self->darray[index];
}

边界检查当然由您决定。如果您不想要它们,请将它们删除。

答案 3 :(得分:0)

为您的结构提供初始化程序和取消初始化程序的另一种流行方法是仅对已分配的内存进行操作。这种方法更为通用,因为它允许您的结构同时使用,堆栈和堆分配。

struct dynamic_array
{
  double * data;
  size_t size;
};

int
dynamic_array_init(struct dynamic_array *const dyn_arr_ptr,
                   const size_t size)
{
  if (dyn_arr_ptr == NULL)
    return -1;  /* invalid argument */
  /* Allocate an array initially filled with zeros. */
  dyn_arr_ptr->data = calloc(size, sizeof(double));
  if (dyn_arr_ptr->data == NULL)
    return -1;  /* out of memory */
  dyn_arr_ptr->size = size;
  return 0;  /* success */
}

int
dynamic_array_deinit(struct dynamic_array *const dyn_arr_ptr)
{
  if (dyn_arr_ptr == NULL)
    return 0;  /* nothing to do */
  free(dyn_arr_ptr->data);
  dyn_arr_ptr->data = NULL;
  dyn_arr_ptr->size = 0;
  return 0;
}

然后可以用它来初始化基于堆栈的结构......

struct dynamic_array dyn_arr;
if (dynamic_array_init(&dyn_arr, 42) < 0)
  {
    fprintf(stderr, "error: cannot initialize array\n");
    return EXIT_FAILURE;
  }
/* Use the dynamic array... */
dynamic_array_deinit(&dyn_arr);

...以及基于堆的。

struct dynamic_array * dyn_arr_ptr = malloc(sizeof(struct dynamic_array));
if (dynamic_array_init(dyn_arr_ptr, 42) < 0)
  {
    fprintf(stderr, "error: cannot initialize array\n");
    return EXIT_FAILURE;
  }
/* Use the dynamic array... */
dynamic_array_deinit(dyn_arr_ptr);
free(dyn_arr_ptr);

请注意,我可以将malloc未选中的结果传递给初始化函数,因为它会执行NULL检查并正常处理此情况。