Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
realloc does not copy values correctly

I am trying to read a text file with unknown size into an array. I do that by reading one character at a time into the array and reallocate memory if the the end of the array is reached.

Starting from the increase in array size from 16 to 32, the realloc method copies only the first 4 characters.

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


int main()
{
    int *text_arr = malloc(1*sizeof(int));
    text_arr[0] = '\0';
    int text_len=1;
    int i=0;
    FILE *fp;

    fp = fopen("1.txt", "r");
    
    while ((text_arr[i] = fgetc(fp)) != EOF) {
        /* increment i for next loop */
        i++;

        /* extend the array if needed */
        if (i >= text_len) {
            /* debug prints */
            printf("Text: ");
            for (int n=0; n<i; n++)
                printf("%c,", text_arr[n]);
            printf("\n\n");
            
            /* double array size */
            text_len *= 2;
            text_arr = (int*)realloc(text_arr, text_len);
        }
    }
    text_arr[i] = '\0';

    fclose(fp);
    
    return 0;
}

What could be the problem?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You are working with sizeof (int) sized objects, you need to make sure to multiply your requested number of bytes by this size when you reallocate.

realloc(text_arr, sizeof (int) * text_len);
over 4 years ago · Santiago Trujillo Relatório

0

realloc takes the size in bytes as argument. However, you are passing the size in elements, which will be lower by a factor of the size of int (usually 4) since you have ints as elements. That means that you are actually making the array smaller after the first resize, and then you keep writing beyond the end of the array.

Adding * sizeof(int) just like you have it in your malloc line should fix it:

text_arr = (int*)realloc(text_arr, text_len * sizeof(int));
over 4 years ago · Santiago Trujillo Relatório

0

Consider what happens the first time text_len is increased (from 1 to 2). You had originally allocated sizeof(int) bytes to text_arr. However, when you call realloc(text_arr, text_len) and text_len is 2, you're truncating the array from 4 bytes to 2 (assuming that sizeof(int) is 4).

text_arr shouldn't be an int* since you're reading individual characters into it and referencing them by text_arr[n]. Instead, it should be a char* and allocated by malloc(1*sizeof(char)) (or just malloc(1) since sizeof(char) is defined to be 1).

Also, with this change, you won't be able to store the return value of fgetc into the array when it's equal to EOF. Therefore, you should store the return value into an actual int variable and then, if it's not EOF, store it into the array.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda