2

vector_int.h is a header with self-made dynamic array (vector) structure.

test.c is a testing programm.

All code is bellow:

vector_int.h:

#include <stdio.h>

typedef struct 
{
    long int len; // Length
    int *array;   // Dynamic Array
} IntVector; 

void ResizeIntVector(IntVector *vector, int size) // Resizing of vector
{
    realloc(vector->array, size * sizeof(int));
    vector->len = size; // Changing of length variable
}

void SetIntVectorCell(IntVector *vector, unsigned int cell_number, int cell_value) // Put cell_value in array[cell_number]
{
    if (cell_number >= vector->len)
        ResizeVectorInt(&vector, cell_number); // Grow size of memory if it's not enough

    vector->array[cell_number] = cell_value;
}

test.c:

#include "vector_int.h"
#include <stdio.h>

int main()
{
    IntVector vector;

    int n;
    scanf("%d", &n);

    int i;
    for (i = 0; i < n; i++) // testing
    {
        SetIntVectorCell(&vector, i, i);
        printf("%d ", vector.array[i]);
    }

    return 0;       
}

Logs:

1   0   D:\Work\Raspberry Pi\test.c In file included from D:\Work\Raspberry Pi\test.c
        D:\Work\Raspberry Pi\vector_int.h   In function 'ResizeIntVector':
11  2   D:\Work\Raspberry Pi\vector_int.h   [Warning] incompatible implicit declaration of built-in function 'realloc' [enabled by default]
            [Linker error] C:\Users\ALEXAN~1\AppData\Local\Temp\cccFKqxs.o:test.c:(.text+0x4a): undefined reference to `ResizeVectorInt'
            collect2: ld returned 1 exit status

I think that there is error in using realloc function, but I thought that I did all right. Please help me and find a mistake or mistakes.

2 Answers 2

10

You have a few problems:

  • The implicit declaration/realloc problem is because you need to include stdlib.h for the realloc signature. Without a function signature the compiler will make some assumptions about your function arguments and return value, then during linking, the linker complains about this if these assumptions don't match the actual function implementation.

  • You're passing realloc an address that hasn't been initialized. This is asking for trouble. Before using your vector variable, do some initialization:

    vector->array = NULL;
    vector->len = 0;
    
  • Furthermore, your usage of realloc is incorrect: it won't change the actual pointer that you give it, only the size of the memory block pointed to. You need to re-assign the pointer yourself. Note that realloc can return NULL upon failure, so do something like:

    tmp = realloc(vector->array, size * sizeof(int));
    
    if (tmp != NULL)
    {
        vector->array = tmp;
        vector->len = size; // Changing of length variable
    }
    else handleAllocError();
    
  • Finally, don't define your functions in the header. This will work, but it's better to have an implementation file vector_int.c that defines the functions declared in the header.

Sign up to request clarification or add additional context in comments.

3 Comments

Also vector is never initialized, so first invocation to realloc is undefined.
Thank you very much! Now programm is compilling, but without handleAllocError(). [Linker error] C:\Users\ALEXAN~1\AppData\Local\Temp\ccGPxG6V.o:test.c:(.text+0x57): undefined reference to `handleAllocError'
Consider handleAllocError pseudocode: you'll need to decide yourself what to do if realloc fails. It can be as simple as a printf/log. So, remove handleAllocError and add your own code for handling realloc failure.
0

You need a +1 in here:

    ResizeVectorInt(&vector, cell_number + 1); // Grow size of memory if it's not enough

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.