0

hi i've been doing some coding with dynamic memory and double pointers, and work fine in the first part, but when it starts executing the second part(the second double pointer) which is exactly coded as the first one it stops working main.c.

int main(int cant,char **argv)
{
    int troden;
    
    
    if(cant != 3)
    {
        printf("papu ta mal /n");
    }
    else
    {
        troden = archivOrden(*(argv + 1),*(argv + 2));
    }

    return 0;
}

function.c

#include "funciones.h"

int archivOrden(char *fileName1,char *fileName2)
{
    FILE *arch1 =NULL,*arch2 = NULL;
    char **vec1 = NULL;
    char **vec2 = NULL;
    char **aux = NULL;
    int ret = 0;
    
    int i = 0, j = 0, x = 0;
    int a;
    
    
    arch1=fopen(fileName1,"r");
    arch2=fopen(fileName2,"r"); 
    
    if(arch1 == NULL || arch2 == NULL)
    {
        printf("error al querer crear arch \n");
    }
    else
    {
        vec1 = (char**)malloc(sizeof(char)*1);
        vec2 = (char**)malloc(sizeof(char)*1);
        
        if(vec1 == NULL || vec2 == NULL) ret = -1;
        
        if(ret == 0)
        {
            *vec1 = (char*)malloc(sizeof(char)*1);
            *vec2 = (char*)malloc(sizeof(char)*1);
            
            if(*vec1 == NULL || *vec2 == NULL) ret = -1;
            if(ret == 0)
            {
                while(!feof(arch1))
                {
                    *(vec1+j) = realloc(*(vec1+j),sizeof(char)*(1+i));
                    fread((*(vec1+j)+i),sizeof(char),1,arch1);
                    
                    if(*(*(vec1+j)+i) =='\n')
                    {
                        *(*(vec1+j)+i) = '\0';
                        printf("%s \n",*(vec1+j));
                        j++;
                        i = 0;
                        vec1 = realloc(vec1,sizeof(char)*(1+j));
                        *(vec1+j) = (char*)malloc(sizeof(char)*(1+i));
                    }
                    else i++;
                }
                *(vec1+j) = realloc(*(vec1+j),sizeof(char)*(1+i));
                *(*(vec1+j)+i)  = '\0';
                i = 0;
                while(!feof(arch2))
                {
                    *(vec2+x) = realloc(*(vec2+x),sizeof(char)*(1+i));
                    fread((*(vec2+x)+i),sizeof(char),1,arch2);
                        
                    if(*(*(vec2+x)+i) =='\n')
                    {
                        //pongo un \0 en el final de la linea
                        *(*(vec2+x)+i) = '\0';
                        
                        printf("%s \n",*(vec2+x));
                        // paso a la siguiente linea
                        x++;
                        i = 0;
                            
                        vec2 = (char**)realloc(vec2,sizeof(char)*(1+x));
                        
                        *(vec2+x) = (char*)malloc(sizeof(char)*(1+i));
                    }
                    else i++;
                }
                *(vec2+x) = realloc(*(vec2+x),sizeof(char)*(1+i));
                *(*(vec2+x)+i)  = '\0';
                printf("paso por aca \n");
                for(a = 0;a<j;a++)
                {
                    printf("%d: %s /n",a,*(vec1+j));
                }
                
                for(a = 0;a<x;a++)
                {
                    printf("%d: %s \n",a,*(vec2+a));
                }
            }
        }
    }
    printf("finalice \n");
    
    fclose(arch1);
    fclose(arch2);
    
    for(a = 0;a<j;a++)
    {
        free(*(vec1+a));
    }
    for(a = 0;a<x;a++)
    {
        free(*(vec2+a));
    }
    free(vec1);
    free(vec2);
        
    return ret;
}

it breaks when it starts the second while and reaches the first '/n', but i cant understand because i do the exact same in the first while and works perfectly fine.

Bodo
  • 9,287
  • 1
  • 13
  • 29
  • Your code is a bit more complicated than it needs to be. I'd use `vec1[j]` instead of `*(vec1 + j)`. I'd use some `char *` variables: `if(*(*(vec1+j)+i) =='\n')` --> `char *cp1 = vec1[j]; if (cp1[i] == '\n')` And `sizeof(char)` is _always_ 1 by definition so remove it. If you rewrite a bit, the error may become obvious. But, based on your description, you have UB (undefined behavior). You are probably writing past the end of the alloc'ed area. You'll see this on the _next_ iteration when you call `realloc` and it sees a corrupted link. Try compiling with the `-fsanitize=address` option – Craig Estey Oct 01 '22 at 13:45
  • https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong – stark Oct 01 '22 at 14:29
  • Please [edit] your question and create a [mre]. The code should be complete compile without warnings. Add the (example) input you use, the actual output or error message you get and the expected output. The error "realloc():invalid old size" is probably a result from memory corruption or passing a wrong pointer. – Bodo Oct 01 '22 at 16:09
  • You seem to be fighting the language. Array reference syntax exists to make your code more readable. Use it. Don't write `*(arr+i)`. Just write `arr[i]`. Even worse, don't write `*(*(vec2+x)+i) `. No one wants to look at that. Just write `vec2[x][i]`. It's the same thing, but you can actually read it without having to puzzle over the syntax. – Tom Karzes Oct 01 '22 at 16:30

0 Answers0