Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Memory allocation problem, don't know what's wrong (http://www.go4expert.com/forums/memory-allocation-dont-whats-wrong-t10173/)

Metalmurphy 22Apr2008 01:58

Memory allocation problem, don't know what's wrong
 
Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NAME_BUFFER_SIZE 80
#define FICHEIRO "registo.bin"

typedef struct {
        char matricula[6];
        char proprietario[NAME_BUFFER_SIZE];
        char apagado;
} veiculo_t;

int main()
{

int opcao = 0, c;
char matricula[10];
FILE *fp;
veiculo_t *memptr = NULL;
veiculo_t registo;

do
{
scanf("%d", &opcao);
getchar();

switch( opcao )
{
case 1:
                fp=fopen(FICHEIRO,"ab+");
                if (fp==NULL) printf("Impossivel aceder ou criar ficheiro");
                else {
                        fgets(matricula,sizeof(matricula),stdin);
                        memcpy(registo.matricula, matricula, 6);
                        fwrite(registo.matricula, sizeof(registo.matricula),1,stdout);
                fgets(registo.proprietario,sizeof(registo.proprietario),stdin);
                        registo.apagado='0';
                        fwrite(&registo, sizeof(registo),1,fp);
                        fclose(fp);
                        fflush(fp);
                        break;
                }
case 2:
                fp=fopen(FICHEIRO,"rb");
                if (fp==NULL) printf("Impossivel aceder ou criar ficheiro\n\n");
                else {
                        c=0;
                        while(fread(&registo,sizeof(registo),1,fp) != 0)
                        {
                                memptr = realloc(memptr, c*sizeof(registo));
                                memcpy(memptr[c].matricula, registo.matricula, 6);
                                memcpy(memptr[c].proprietario, registo.proprietario, sizeof(registo.proprietario));
                                c++;
                        }
                        fclose(fp);
                        fflush(fp);
                }

                break;
case 3:
                printf("\n\nPrograma vai fechar!\n\n");
                break;
default:
                printf("\nOpcao invalida!!\n\n");
                break;
}
}while (opcao != 3);



return(0);
}

Case 1 saves a license plate (matricula) and a name (proprietario) to a file

Case 2 reads the entire file and puts everything in memory. I'm having a problem here:
Code:

memptr = realloc(memptr, c*sizeof(registo));
As soon as it goes a second time through the cycle I get this error:
Code:

*** glibc detected *** ./ex1: realloc(): invalid next size: 0x0804a170 ***
It has something to do with this:
Code:

memcpy(memptr[c].proprietario, registo.proprietario, sizeof(registo.proprietario));
Because if I remove it I no longer get the error, for some reason why I try to put the name in memory it messes it up. I've already tried using strcpy instead, strlen instead of sizeof, even manually putting the amount of bytes to copy, always the same error.

Anyone have any idea what's wrong and what should I do instead?

Metalmurphy 22Apr2008 02:25

Re: Memory allocation problem, don't know what's wrong
 
Before anyone mentions it, I just noticed it multiplies by zero on the first loop... stupid me.

I'm having other problems now but I'll try to fix them first now that I figured how what this problem was : p

Metalmurphy 22Apr2008 03:34

Re: Memory allocation problem, don't know what's wrong
 
I'm having another problem now, I switched the code that loads to the memory to a separate function, this one:

Code: Cpp

void memoria(FILE *fp, veiculo_t registo, veiculo_t *memptr)
{
        fp=fopen(FICHEIRO,"rb");
        if (fp==NULL) printf("Impossivel aceder ou criar ficheiro\n\n");
        else {
            int c=0;
            while(fread(&registo,sizeof(registo),1,fp) != 0)
            {
                memptr = realloc(memptr, (c+1)*sizeof(registo));
                memcpy(memptr[c].matricula, registo.matricula, 6);
                memcpy(memptr[c].proprietario, registo.proprietario, sizeof(registo.proprietario));
                memptr[c].apagado = registo.apagado;
                c++;
            }
            printf("\nFicheiro Carregado para a Memoria\n\n");
            fclose(fp);
            fflush(fp);
        }
}


And in main, I call it as soon as the program opens it:

Code: Cpp

int main()
{
int opcao;
FILE *fp;
veiculo_t registo;
veiculo_t *memptr = NULL;

memoria(fp, registo, memptr);

fwrite(memptr[0].matricula, 6,1,stdout);
fwrite(memptr[0].proprietario, strlen(memptr[1].proprietario),1,stdout);


I get a segmentation fault on those fwrites in main(), I know the problem is somewhere in the use of pointers but isn't the memptr supposed to be passed as a pointer to the function?


All times are GMT +5.5. The time now is 18:57.