Appending a value to the end of a dynamic array

2019-07-26 06:10发布

Well I have been studying a little C this winter break and in my adventures I stumbled upon an issue with a Dynamic Array.

It's a fairly simple program really. What I am trying to do is to create an array that holds the numbers of the Fibonacci series. Here is the code:

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

int dynamic_arry_append(int* arry, int* number, int* size);

int main() {

    int i, n, size = 3, *arry = NULL, fibarr[size];

    printf("Dynamic array, Fibonacci series. \n");
    printf("Capture upto element: ");
    scanf("%d", &n);

    i = 0;
    // passing the first elements
    fibarr[0] = 0;
    fibarr[1] = 1;
    fibarr[2] = 1;

    while ( i < n ) {
        printf("**%d\n",fibarr[0]);
        dynamic_arry_append( arry, &fibarr[0], &size );
        fibarr[0] = fibarr[1];
        fibarr[1] = fibarr[2];
        fibarr[2] = fibarr[1] + fibarr[0];
        i++;
    }

    for ( i = 0 ; i < size ; i++) 
        printf("Element %d of the array: %d.\n", i, arry[i]);

    return 0;
}

int dynamic_arry_append(int* arry, int* number, int* size) {
    int i;

    int bacon = *size; // first name i thought of
    bacon++;
    int *new_addr = realloc(arry, bacon * sizeof(int));

    if( new_addr != NULL ) {
        arry = new_addr;
        arry[bacon-1] = *number;
        // printf for easier debugging, or so i thought
        for ( i = 0 ; i < bacon ; i++ ) 
            printf("%d\t%d\n", i+1, arry[i]);
        printf("\n");
        *size = bacon;
    } else {
        printf("Error (re)allocating memory.");
        exit (1);
    }

    return 0;
}

At least in my mind this works. However, in practice I get funny results:

Dynamic array, Fibonacci series.
Capture upto element: 5
**0 // next fibonacci number
1       5256368
2       5246872
3       1176530273
4       0

**1
1       5256368
2       5246872
3       1768053847
4       977484654
5       1

**1
1       5256368
2       5246872
3       1551066476
4       1919117645
5       1718580079
6       1

**2
1       5256368
2       5246872
3       977484645
4       1852397404
5       1937207140
6       1937339228
7       2

**3
1       5256368
2       5246872
3       1551071087
4       1953724755
5       842231141
6       1700943708
7       977484653
8       3

/* Code::Blocks output */
Process returned -1073741819 (0xC0000005)   execution time : 17.886 s
Press any key to continue.

I am really baffled by this error, and after searching around I found no solution...Can anyone help? Thank you very much.

4条回答
Melony?
2楼-- · 2019-07-26 06:22

You should declare your fibarr as a pointer (so name it differently) not an array. And you should pass to your dynamic_arry_append the address of that pointer, like &fibarr. And you should initialize fibarr in your main with calloc. At last you should dynamically update (and keep, and pass) the size of the allocated array.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-26 06:38

The problem may be that the arry pointer variable is passed by value to the function dynamic_arry_append. That means, that changes that you make to the arry variable within that function will not be reflected by any variables outside of that function. For example:

int *a = NULL;

someFunc(a);

// a will still be NULL here no matter what someFunc does to it.
查看更多
一夜七次
4楼-- · 2019-07-26 06:45
#include <stdio.h>
#include <stdlib.h>

int * dynamic_array_append(int * array, int size);

int main() {

    int i, n, size=0, *array = NULL;

    printf("Dynamic array, Fibonacci series. \n");
    printf("Capture upto element: ");
    scanf("%d", &n);

    for (i=0 ; i<n ; i++)
        array = dynamic_array_append(array, i);

    for (i=0 ; i<n ; i++) 
        printf("array[%d] = %d\n", i, array[i]);

    return 0;
}

int * dynamic_array_append(int * array, int size) 
{
    int i;
    int n1, n2;
    int new_size = size + 1;
    int * new_addr = (int *) realloc(array, new_size * (int)sizeof(int));

    if (new_addr == NULL) {
        printf("ERROR: unable to realloc memory \n");
        return NULL;
    }

    if (size == 0 || size == 1) {
        new_addr[size] = size;
        return new_addr;
    }

    n1 = new_addr[size-1];
    n2 = new_addr[size];
    new_addr[new_size-1] = new_addr[new_size-2] + new_addr[new_size-3];

    return new_addr;
}

/*
Output:

Dynamic array, Fibonacci series. 
Capture upto element: 10
array[0] = 0
array[1] = 1
array[2] = 1
array[3] = 2
array[4] = 3
array[5] = 5
array[6] = 8
array[7] = 13
array[8] = 21
array[9] = 34

*/

Points to note:

  • The newly (re)allocated array should be returned back to main and stored in a pointer-to-int (or) pass pointer-to-pointer-to-int and update it accordingly once after reallocing
  • The fibarr is not needed. It doesn't solve any problem.
  • You don't have to pass the size and the number. Just send the size and it will pick the n-1 and n-2 to calculate n.
  • This is considered to be highly inefficient. Because if you know the n then you can allocate memory for n integers in one shot and calculate the fib series.
查看更多
冷血范
5楼-- · 2019-07-26 06:45

You are not returning the new address of the array... and you are reading/writing not your memory. Run the program with all error messages under debugger and you'll see the problem is in this line:

  dynamic_arry_append( arry, &fibarr[0], &size );  
查看更多
登录 后发表回答