C:添加元素来动态分配的数组 [英] C: adding element to dynamically allocated array

查看:203
本文介绍了C:添加元素来动态分配的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图搜索出通过谷歌的解决方案:我无法找到任何帮助;它甚至好像我是正确的这样做。唯一的网页我可以找到关于送我的动态分配处理,通过与阵列的结构体,这当然是标量里面是一个函数数组,所以表现不同。我不想使用结构现在 - 我想了解DAM和指针和职能工作

不过,我敢肯定,这是很基本的,但我坚持。在code编译,但它冻结了,当我运行可执行文件。 (我用MinGW的海湾合作​​委员会,如果该事项。而且我并不清楚,现在,关于如何使用GDB)。

这里的code(最终,我想整个code是一个ArrayList类数据结构):

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;无效添加(INT元素,诠释*向量);
无效display_vector为(int *向量);
无效initialize_vector为(int *向量);诠释元素= 0;
INT大小= 10;INT主要(无效)
{
    为int *矢量= 0;
    initialize_vector(矢量);
    加(1,向量);
    //增加(2,向量);
    //添加(3,向量);
    //添加(4向量);
    //添加(5,向量);
    //添加(6向量);
    //添加(7,向量);
    //添加(8,向量);
    //添加(9向量);
    //添加(10,向量);
    //添加(11向量);
    display_vector(矢量);    返回0;
}无效添加(INT元素,诠释*向量)
{
    矢量[元素++] =元素;
    返回;
}无效display_vector为(int *向量)
{
    INT I;
    对于(i = 0; I<要素;我++)
    {
        的printf(%2D \\ t的,向量[I]);
        如果(第(i + 1)%5 == 0)
            的printf(\\ n);
    }
    的printf(\\ n);
    返回;
}无效initialize_vector为(int *向量)
{
    矢量=(INT *)malloc的(的sizeof(int)的大小*);}


解决方案

编辑,以更清楚一点。

问题是你的init例程正在与载体的拷贝,并malloc'ing到该副本,而不是原来的向量指针。你失去指针从初始化回报的内存块。

在此功能更改参数向量的句柄(指针的指针)

 无效initialize_vector(INT **向量)
{
    *矢量=(INT *)malloc的(的sizeof(int)的大小*);
}

然后,更改调用init这个

  initialize_vector(和矢量);

我没编译这个,但它应该可以解决code。

I've tried to search out a solution via Google: I couldn't find anything that helped; it even seemed as if I was doing this correctly. The only pages I could find regarding sending my dynamically allocated array through a function dealt with the array being inside a struct, which is scalar of course, so behaves differently. I don't want to use a struct right now -- I'm trying to learn about DAM and working with pointers and functions.

That said, I'm sure it's very elementary, but I'm stuck. The code compiles, but it freezes up when I run the executable. (I'm using minGW gcc, if that matters. And I'm not clear at all, right now, on how to use gdb.)

Here's the code (eventually, I want the entire code to be an ArrayList-like data structure):

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

void add( int element, int *vector);
void display_vector( int *vector );
void initialize_vector( int *vector );

int elements = 0;
int size = 10;

int main(void)
{
    int *vector = 0; 
    initialize_vector(vector);
    add(1, vector);
    //add(2, vector);
    //add(3, vector);
    //add(4, vector);
    //add(5, vector);
    //add(6, vector);
    //add(7, vector);
    //add(8, vector);
    //add(9, vector);
    //add(10, vector);
    //add(11, vector);
    display_vector(vector); 

    return 0;
}

void add( int element, int *vector)
{
    vector[elements++] = element;
    return;
}

void display_vector( int *vector )
{
    int i;
    for( i = 0; i < elements; i++)
    {
        printf("%2d\t", vector[i]);
        if( (i + 1) % 5 == 0 )
            printf("\n");
    }
    printf("\n");
    return; 
}

void initialize_vector( int *vector )
{
    vector = (int *)malloc(sizeof(int) * size);

}

解决方案

Edited to make a little bit more clear.

The problem is your init routine is working with a copy of "vector" and is malloc'ing into that copy rather than the original vector pointer. You loose the pointer to the memory block on the return from the initialize.

Change parameter for vector to a handle (pointer to pointer) in this function

void initialize_vector( int **vector )
{
    *vector = (int *)malloc(sizeof(int) * size);
}

Then change the call to init to this

initialize_vector(&vector);

I didn't compile this, but it should fix the code.

这篇关于C:添加元素来动态分配的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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