Home > Software engineering >  Merge two integer arrays in C
Merge two integer arrays in C

Time:01-23

I am trying to program a function to merge two signed integer arrays. The idea is to get the contents of src, reallocate memory in dest to insert the contents of src after its own contents. If dest is NULL, the function must allocate memory space to store src 1. If there is an error the function must return NULL. The function must also free the memory space allocated to src after the merge. Integer arrays are terminated by an int called "End Of Buffer" (-1 in my example below).

the problem seems to be related to the realloc function, can you help me to fix it?

Here is the full code that produces the error :

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

ssize_t my_put_int(int nbr, int fd)
{
    char c = 0;
    char negative = '-';
    ssize_t len = 0;
    if (nbr < 0) {
        len  = write(fd, &negative, 1);
        len  = my_put_int(-nbr, fd);
    } else if (nbr < 10) {
        c = nbr   48;
        len  = write(fd, &c, 1);
    } else {
        len  = my_put_int(nbr / 10, fd);
        c = nbr % 10   48;
        len  = write(fd, &c, 1);
    }
    return len;
}

ssize_t my_put_int_arr(int *arr, int eob, int fd)
{
    size_t count = 0;
    int *ptr = NULL;
    if (!arr || fd < 0)
        return -1;
    ptr = arr;
    while (*ptr != eob) {
        count  = my_put_int(*ptr, fd);
        ptr  ;
    }
    return count;
}

size_t my_int_arr_len(int *arr, int eob)
{
    size_t count = 0;
    if (!arr)
        return -1;
    while (*arr != eob) {
        count  ;
        arr  ;
    }
    return count;
}

int *my_int_array_concat(int *dest, int *src, int eob)
{
    size_t src_size = 0, dest_size = 0;
    if (!src) return NULL;
    src_size = my_int_arr_len(src, eob);
    if (dest == NULL) {
        dest = malloc(sizeof(int) * (src_size   1));
        if (dest == NULL) return NULL;
        for (size_t i = 0; i < src_size; i  ) dest[i] = src[i];
        dest[src_size] = eob;
        free(src);
        return dest;
    }
    dest_size = my_int_arr_len(dest, eob);
    printf("Dest size %ld, src size %ld\n", dest_size, src_size);       // Debug
    dest = realloc(dest, sizeof(int) * (dest_size   src_size   1));
    printf("New dest size %ld\n", my_int_arr_len(dest, -1));            // Debug
    if (!dest) return NULL;
    for (size_t i = 0; i < src_size; i  ) dest[i] = src[i];
    dest[src_size] = eob;
    free(src);
    return dest;
}

int main()  //test main
{
    int *src = malloc(sizeof(int) * 2);
    src[0] = 3;
    src[1] = -1;
    int *dest = malloc(sizeof(int) * 3);
    dest[0] = 2;
    dest[1] = 1;
    dest[2] = -1;
    dest = my_int_array_concat(dest, src, -1);
    my_put_int_arr(dest, -1, 1);
    return 0;
}

I get this result :

Dest size 2, src size 1
New dest size 2
3

CodePudding user response:

Use following concat function -

int *my_int_array_concat(int *dest, int *src, int eob)
{
    size_t src_size = 0, dest_size = 0;
    if (!src) return NULL;
    src_size = my_int_arr_len(src, eob);
    if (dest == NULL) {
        dest = malloc(sizeof(int) * (src_size   1));
        if (dest == NULL) return NULL;
        for (size_t i = 0; i < src_size; i  ) dest[i] = src[i];
        dest[src_size] = eob;
        return dest;
    }
    dest_size = my_int_arr_len(dest, eob);
    dest = realloc(dest, sizeof(int) * (dest_size   src_size   1));
    if (!dest) return NULL;
    for (size_t i = 0; i < src_size; i  ) dest[dest_size i] = src[i];
    dest[dest_size src_size] = eob;
    free(src);
    return dest;
}

It should work.

  • Related