Problems with the realloc() function in C language

Asked

Viewed 64 times

2

Hello guys! I am implementing a code to insert a kind of table into a pointer and relocate memory while the function add() is being executed, the function name() will be executed only once to set the table name.

The error: when adding two names to the table, the code performs normally, but when I exceed two calls to the function add(), there occurs a Segmentation.

Error example:

add(table, "Lucas\n");
add(table, "Vanessa\n");
add(table, "Marcos\n");

Complete code:

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

int name(char *table, char *text)
{
    table = (char *) realloc(table, (strlen(text) + 1) * sizeof(char));

    if (table == NULL) {
        return EXIT_FAILURE;
    }

    /* O nome da tabela é copiado para "table" */
    strncpy(table, text, (strlen(text) + 1) * sizeof(char));

    return EXIT_SUCCESS;
}

int add(char *table, char *text)
{
    table = (char *) realloc(table, (strlen(text) + 1) * sizeof(char));

    if (table == NULL) {
        return EXIT_FAILURE;
    }

    /* Os dados da tabela são concatenados */
    strncat(table, text, (strlen(text) + 1) * sizeof(char));

    return EXIT_SUCCESS;
}

int main(void)
{
    char *table = (char *) malloc(sizeof(char));

    if (table == NULL) {
        fprintf(stderr, "Erro ao inicializar o ponteiro!\n");
        exit(EXIT_FAILURE);
    }

    name(table, "Nomes:\n");

    add(table, "Lucas\n");
    add(table, "Vanessa\n");

    printf("%s", table);

    free(table);

    return EXIT_SUCCESS;
}

From now on, thank you very much! :)

1 answer

3


Your program’s error is in using the realloc(), in that passage:

// Esse trecho eh da funcao add
table = (char *) realloc(table, (strlen(text) + 1) * sizeof(char));

Observe:

void *realloc(void *ptr, size_t size);

The function realloc() changes the size of the memory allocated in ptr and the new size is equal to size. When I speak the same I am saying that the previous size is not added to the value of size, soon realloc() can decrease the size instead of increasing and this is what happens in your code.

Returning to this part of your program:

// Esse trecho eh da funcao add
table = (char *) realloc(table, (strlen(text) + 1) * sizeof(char));

To you use the realloc(), the value reallocated to table will equal (strlen(text) + 1).

Considering the value of (strlen(text) + 1) equal to 50 and table equal to 30, then the reallocated bytes will be 50 and not 80. When the program does this:

strncat(table, text, (strlen(text) + 1) * sizeof(char));

table, who has a string of 30 bytes, will be concatenated to (strlen(text) + 1), who has a string of 50 bytes, however table can only store 50 bytes and not 80. The more values are added with the function add, more data will be in unknown memory spaces until an error occurs.

To solve this just calculate the size that really table will need to have to store the string concatenated, in this way:

table = (char *) realloc(table, ((strlen(table)) + (strlen(text) + 1)) * sizeof(char));

Now the size reallocated will be (strlen(table)), which is the size of string that already has in it, more (strlen(text) + 1), which is the size of the new string.

See the code now working: code.

  • Caracaaa, I went unnoticed! Thank you very much, bro :D I really thought that the realloc() function matched the size of the previously allocated string, but it’s wrong that we learned. Big hug, my brother! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.