malloc in a loop using valgrind - c

I am having troubles with memory leaks (error detected by valgrind).
So here is my first function:
message *creationCellule() {
message *cellule;
cellule = (message *)malloc(sizeof(message));
if (cellule != NULL) {
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
}
return cellule;
}
It returns cellule which is allocated by malloc.
Now I have this:
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
message *test;
test = creationCellule();
int k = 0;
if (fp != NULL) {
k = fscanf(fp, "%d %d ", &(test->dateDeb), &(test->dateFin));
while (k != EOF) {
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
}
}
}
In which I call creationCellule() in a loop.
My problem is that, if I put free(test) inside a loop I lose all the context of my code and valgrind shows me ERROR SUMMARY:213 errors from 19 contexts.
What should I do ?
This is the complete code and the valgrind output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tp1.h"
message *creationCellule() {
message *cellule;
cellule = (message *)malloc(sizeof(message));
if (cellule != NULL) {
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
}
return cellule;
}
message **recherche(message *tete, int date) {
message **prec = tete;
message *cour = *tete;
while (cour != NULL && cour->dateDeb < date) {
prec = &(cour->suivant);
cour = cour->suivant;
}
return prec;
}
void insertion(message **tete, message *cellule) {
message **prec;
if (cellule != NULL) {
prec = recherche(tete, cellule->dateDeb);
cellule->suivant = *prec;
*prec = cellule;
}
}
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
message *test;
test = creationCellule();
int k = 0;
if (fp != NULL) {
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
while (k != EOF) {
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
}
}
}
void affichageListe(message **tete) {
if (tete != NULL) {
message *tmp = *tete;
while (tmp != NULL) {
//printf("jam ktu\n");
printf("DateDeb = %d \n", tmp->dateDeb);
printf("DateFin = %d \n", tmp->dateFin);
printf("Text = %s \n", tmp->text);
tmp = tmp->suivant;
}
}
}
void suppression(message **tete, int valeur, int dateDeb) {
message **prec;
prec = recherche(tete, dateDeb);
//printf("Prec text: %s , prec dateFin: %d\n", (*prec)->text, (*prec)->dateFin);
if ((*prec) != NULL && (*prec)->dateFin == valeur) {
(*prec) = (*prec)->suivant;
}
}
void supprimeObsoletes(message **tete) {
message *pt = *tete;
time_t temps;
struct tm *date;
int intNum;
temps = time(NULL);
date = localtime(&temps);
char buffer[9];
if ((date->tm_mon) < 10) {
sprintf(buffer, "%d0%d%d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
} else {
sprintf(buffer, "%d%d%d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
}
intNum = atoi(buffer);
while (pt != NULL) {
if ((pt->dateFin) < intNum) {
printf("KTU HYB %s\n", pt->text);
suppression(tete, pt->dateFin, pt->dateDeb);
}
pt = pt->suivant;
}
}
void changeDate(int dateChange, int dateInit, message **tete) {
message *point = *tete;
//printf("Kjo eshte tete %p:\n", (*point));
while (point != NULL) {
if ((point->dateDeb) == dateInit) {
printf("%d\n", point->dateDeb);
printf("%s\n", point->text);
point->dateDeb = dateChange;
}
point = point->suivant;
}
}
int main(int argc, char *argv[]) {
const char *name = argv[1];
message *pointeur = NULL;
message **tete = &pointeur;
int dateInit = 19973012;
int dateChange = 20003008;
FILE *fp = fopen(name, "r");
lectureFichier(name, tete);
//changeDate(dateChange, dateInit, tete);
supprimeObsoletes(tete);
affichageListe(tete);
return 0;
}
The header: tp1.h
#ifndef TP1_TEST_H
#define TP1_TEST_H
#define TAILLE_MAX 100
typedef struct cell {
int dateDeb;
int dateFin;
char text[TAILLE_MAX];
struct cell *suivant;
} message;
message *creationCellule();
message **recherche(message **tete, int date);
void affichageListe(message **tete);
void insertion(message **tete, message *cellule);
void lectureFichier(const char * nomFichier, message **tete);
The .txt file (added in execution)
19973012 20220512 TEXT 1
19980511 19001203 THIS
20011102 20301123 HOUSE
20020809 20301025 HELP
Valgrind output:

Function lectureFichier does not free unused nodes properly.
Here is a corrected version:
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
if (fp != NULL) {
for (;;) {
message *node = creationCellule();
if (fscanf(fp, "%d%d %99s", &node->dateDeb, &node->dateFin, node->text) == 3) {
insertion(tete, node);
} else {
free(node);
break;
}
}
}
}

the following proposed code:
cleanly compiles
properly handles errors
removed unused code
does NOT cleanup the linked list when an error occurs -- you will need to add that
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef TP1_TEST_H
#define TP1_TEST_H
#define TAILLE_MAX 100
struct cell
{
int dateDeb;
int dateFin;
char text[TAILLE_MAX];
struct cell * suivant;
};
typedef struct cell message;
message * creationCellule( void );
message ** recherche( message ** tete, int date );
void affichageListe( message ** tete );
FILE * ecrireFichier( message ** tete );
void dateNonExpires( message ** tete );
#endif // TPI_TEST_H
message * creationCellule()
{
message * cellule = malloc(sizeof(message));
if( !cellule )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
return cellule;
}
message ** recherche( message ** tete, int date )
{
message ** prec = tete;
message * cour = *tete;
while( cour != NULL && cour->dateDeb < date )
{
prec = &(cour->suivant);
cour = cour->suivant;
}
return prec;
}
void insertion(message ** tete, message * cellule)
{
(void)tete;
(void)cellule;
// insert code that does something reasonable
}
void lectureFichier(const char * nomFichier, message ** tete)
{
FILE * fp = fopen(nomFichier, "r");
if( !fp )
{
perror( "fopen failed" );
exit( EXIT_FAILURE );
}
test = creationCellule();
while( fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin)) == 2 )
{
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
}
}
void affichageListe( message ** tete )
{
if( tete )
{
message * tmp = *tete;
while( tmp )
{
//printf("jam ktu\n");
printf( "DateDeb = %d \n", tmp->dateDeb );
printf( "DateFin = %d \n", tmp->dateFin );
printf( "Text = %s \n", tmp->text );
tmp = tmp->suivant;
}
}
}
void suppression( message**tete, int valeur, int dateDeb )
{
message **prec;
prec = recherche( tete, dateDeb );
//printf("Prec text: %s , prec dateFin: %d\n",(*prec)->text,(*prec)->dateFin);
if( (*prec) != NULL && (*prec)->dateFin == valeur )
{
(*prec)=(*prec)->suivant;
}
}
void supprimeObsoletes(message **tete)
{
message *pt = *tete;
time_t temps;
struct tm *date;
temps=time(NULL);
date=localtime(&temps);
char buffer[9];
if((date->tm_mon)<10)
{
sprintf(buffer,"%d0%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
else
{
sprintf(buffer,"%d%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
int intNum=atoi(buffer);
while( pt )
{
if( (pt->dateFin) < intNum )
{
printf( "KTU HYB %s\n", pt->text );
suppression( tete, pt->dateFin, pt->dateDeb );
}
pt=pt->suivant;
}
}
int main(int argc, char * argv[])
{
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s filename\n", argv[0] );
exit( EXIT_FAILURE );
}
const char * name = argv[1];
message * pointeur = NULL;
lectureFichier( name, &pointeur );
supprimeObsoletes( &pointeur );
affichageListe( &pointeur );
while( pointeur )
{
message *current = pointeur;
pointeur = pointeur->suivant;
free( current );
}
return 0;
}

Related

Debugging: Problem with linked list linkage

I have a linked list implementation as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAPACITY 128
typedef struct blob blob;
struct blob {
char text[CAPACITY];
struct blob* prev;
struct blob* next;
};
static blob* create_blob() {
blob* newBlob = malloc(sizeof(blob));
if (!newBlob) {
fprintf(stderr, "%s\n", "Malloc failed");
return 0;
}
newBlob->prev = 0;
newBlob->next = 0;
memset(newBlob->text, 0, CAPACITY);
return newBlob;
}
int insertAtTail(blob* list[static 1]) {
blob* newBlob = create_blob();
if (!newBlob) {
fprintf(stderr, "%s\n", "Malloc failed");
return 0;
}
if (!*list) {
*list = newBlob;
return 1;
}
blob* lastElement = *list;
while (lastElement->next) {
// Check if linkage until last element is okay
if (lastElement != (lastElement->next)->prev) {
fprintf(stderr, "%s\n", "The next_element->prev does not point to the current element");
} else {
fprintf(stderr, "%s\n", "Okay");
}
lastElement = lastElement->next; // Find last Blob
}
lastElement->next = newBlob;
newBlob->prev = lastElement;
return 1;
}
int reversePrintBlobs(blob const list[static 1]) {
if (!list) {
fprintf(stderr, "%s\n", "list is empty");
return 1;
}
const blob* tmp = list;
size_t blobCounter = 1;
while (tmp->next) {
blobCounter++;
tmp = tmp->next;
}
while (tmp && tmp != list) {
if ((tmp->prev)->next != tmp) {
fprintf(stderr, "%s\n", "error with the linkage");
exit(1);
}
printf("#number = %zu: %s\n", blobCounter--, tmp->text);
tmp = tmp->prev;
}
return 0;
}
When I add some blobs with the following main() function, it seems like the prev member of my struct doesn't point to the previous element. That is, there is a problem with the linkage. Can anyone point out why?
int main() {
FILE* pFile = fopen("example.txt", "r");
if (!pFile) {
perror("fopen failed\n");
return EXIT_FAILURE ;
}
fseek(pFile, 0, SEEK_SET);
blob* list = create_blob();
blob* tmp = list;
while (!feof(pFile)) {
size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
pFile);
if (noBytesRead != CAPACITY && !feof(pFile)) {
fprintf(stderr, "%s\n", "File read failed");
exit(EXIT_FAILURE);
}
// Append a null terminating character in the end
tmp->text[noBytesRead] = '\0';
if (!feof(pFile) && insertAtTail(&list)) {
tmp = tmp->next;
}
}
}
On the other hand, If I use the following main() everything works fine.
int main() {
blob* list = create_blob();
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
/* code */
return 0;
}
The bug has been fixed, the new main function is the following:
int main() {
FILE* pFile = fopen("example.txt", "r");
if (!pFile) {
perror("fopen failed\n");
return EXIT_FAILURE ;
}
fseek(pFile, 0, SEEK_SET);
blob* list = create_blob();
blob* tmp = list;
int blobCounter = 1;
while (!feof(pFile)) {
size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
pFile);
if (noBytesRead != CAPACITY && !feof(pFile)) {
fprintf(stderr, "%s\n", "File read failed");
exit(EXIT_FAILURE);
}
// Append a null terminating character in the end
tmp->text[noBytesRead - 1] = '\0';
if (!feof(pFile) && insertAtTail(&list)) {
blobCounter++;
tmp = tmp->next;
}
}

Memory leaks problems using valgrind even after fclose

I'm having problems with valgrind,the problem is in this function where I have memory leaks apparently.
void lectureFichier(const char * nomFichier, message ** tete){
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))==2)
{
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
free(test);
exit(EXIT_FAILURE);
}
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
fclose(fp);
}
Take a look at valgrind result:
I can't find the problem. Do you have any suggestion?
HERE'S THE WHOLE CODE THAT CONTAINS THE FONCTION ABOVE ,WHEN I USE VALGRIND WITH THIS CODE IT GIVES THE RESULT AS SHOWN IN THE PICTURE ABOVE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tp1.h"
message * creationCellule(){
message * cellule=NULL;
free(cellule);
cellule=malloc(sizeof(message));
if(!cellule)
{
perror("malloc failes\n");
exit(EXIT_FAILURE);
}
cellule -> dateDeb = 0;
cellule -> dateFin = 0;
cellule -> suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
return cellule;
}
/*bejme recherche per date de deb se eshte trie nga date deb*/
message * recherche(message * tete, int date){
message ** prec = tete;
message * cour = *tete;
while( cour != NULL && cour -> dateDeb < date){
prec = &(cour -> suivant);
cour = cour -> suivant;
}
return prec;
}
/*la prochaine fois il faut venir avec un makefile*/
void insertion(message ** tete, message * cellule){
(void)tete;
(void)cellule;
message ** prec;
if(cellule != NULL){
prec = recherche(tete, cellule -> dateDeb);
cellule -> suivant = *prec;
*prec = cellule;
}
}
/*duhet te lexojme fichier ne fillim dhe kete fichier do e krijojme ne vete*/
void lectureFichier(const char * nomFichier, message ** tete){
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))==2)
{
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
free(test);
exit(EXIT_FAILURE);
}
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
fclose(fp);
}
void affichageListe(message ** tete)
{
if(tete)
{
message * tmp = *tete;
while( tmp )
{
//printf("jam ktu\n");
printf("DateDeb = %d \n", tmp -> dateDeb);
printf("DateFin = %d \n", tmp -> dateFin);
printf("Text = %s \n", tmp -> text);
tmp = tmp -> suivant;
}
}
}
void suppression(message**tete,int valeur,int dateDeb)
{
message **prec;
prec=recherche(tete,dateDeb);
if((*prec)!=NULL && (*prec)->dateFin==valeur)
{
(*prec)=(*prec)->suivant;
}
}
//EXERCICE 3
void supprimeObsoletes(message **tete)
{
message *pt=*tete;
time_t temps;
struct tm *date;
int intNum;
temps=time(NULL);
date=localtime(&temps);
char buffer[9];
if((date->tm_mon)<10){
sprintf(buffer,"%d0%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
else{
sprintf(buffer,"%d%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
intNum=atoi(buffer);
while(pt!=NULL)
{
if((pt->dateFin)<intNum)
{
printf("KTU HYB %s\n",pt->text);
suppression(tete,pt->dateFin,pt->dateDeb);
}
pt=pt->suivant;
}
}
void changeDate(int dateChange, int dateInit,message **tete)
{
message *point=*tete;
//printf("Kjo eshte tete %p:\n",(*point));
while(point!=NULL)
{
if((point->dateDeb)==dateInit)
{
printf("%d\n",point->dateDeb);
printf("%s\n",point->text);
point->dateDeb=dateChange;
}
point=point->suivant;
}
}
int main(int argc, char * argv[])
{
const char * name=argv[1];
message * pointeur = NULL;
//message ** tete = &pointeur;
int dateInit=19973012;
int dateChange=20003008;
lectureFichier(name, &pointeur);
supprimeObsoletes(&pointeur);
affichageListe(&pointeur);
while(pointeur)
{
message *current=pointeur;
pointeur=pointeur->suivant;
free(current);
}
return 0;
}
Your valgrind log shows "fscanf of first two" as part of the program output. That part happens when the fscanf call fails inside of the loop. You call exit but don't close the file. You need to close the file first:
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
fclose(fp);
free(test);
exit(EXIT_FAILURE);
}
void lectureFichier(const char * nomFichier, message ** tete)
{
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin))==2)
{
// Here you are reading again. That's not what you want.
// if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
// {
// fprintf(stderr,"fscanf of first two\n");
// free(test);
// exit(EXIT_FAILURE);
// }
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
// Here you can free the last test, because it is not used
free(test);
fclose(fp);
}
If your problem is memory leaks then you should use free(fp) for dyamically allocated pointers.

Malloc and Realloc crash in C?

i have to do with my friend a program in C for my school.
The problem is, when i would malloc a pointer, it doesn't work, and the application will crashed.
But not in debug mod. In debug mod, it works.
This is a part of my code:
#include <bplus.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LOG "log.txt"
#define LOGIDX "log.idx"
struct event {
time_t nb;
char *username;
int type;
char *message;
};
struct eventlist {
struct event event;
struct eventlist *nextelem;
};
struct eventlist *getEventList(time_t date);
void insert(struct eventlist *list, struct eventlist *event);
struct event getEventFromString(char *str);
char *getLine(FILE *file);
int file_exists(const char *fname);
char *dechiffrer(const char *pChaineChiffree);
int main(void) {
time_t timenow = time(NULL);
struct tm *tm = gmtime(&timenow);
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
time_t time = mktime(tm);
struct eventlist *list = getEventList(time);
return 0;
}
struct eventlist *getEventList(time_t date) {
int end = 0;
FILE *file = NULL;
char str[20];
char *line = NULL;
char *uncode = NULL;
ENTRY e;
IX_DESC faddress;
struct eventlist *list = NULL; // Liste a retourner
struct event *event = NULL; // Contient l'evenement
struct eventlist *templist = NULL; // Contient l'evenement a mettre dans list
// On ouvre / crée le fichier .idx
if (file_exists(LOGIDX))
open_index(LOGIDX, &faddress, 0);
else
make_index(LOGIDX, &faddress, 0);
// On ouvre le fichier de log
if ((file = fopen(LOG, "rb")) != NULL) {
// On met dans e.key le temps
sprintf(str, "%d", (int) date);
strcpy(e.key, str);
if (find_key(&e, &faddress)) { // Si la clé existe
fseek(file, e.recptr, SEEK_SET); // On se positionne
line = getLine(file); // On récupère la ligne
while (!feof(file) && !end) { // Boucle principale
printf("\ngetEventList 1");
if (line != NULL) {
uncode = dechiffrer(line); // On déchiffre la ligne
printf("\ngetEventList 2");
event = (struct event *) malloc(sizeof(struct event *) * 1); // On alloue de la place
printf("\ngetEventList 3");
if (event) {
*event = getEventFromString(uncode); // On la transforme en structure
printf("\ngetEventList 4");
if (event->nb < date + 86400) {
templist = (struct eventlist *) malloc(sizeof(struct eventlist *) * 1);
printf("\ngetEventList 5");
if (templist) {
templist->event = *event;
templist->nextelem = NULL;
printf("\ngetEventList 6");
if (list == NULL)
list = templist;
else
insert(list, templist);
printf("\ngetEventList 7");
line = getLine(file); // On récupère la ligne
printf("\ngetEventList 8");
} else {
list = NULL;
end = 1;
}
} else
end = 1;
} else {
list = NULL;
end = 1;
}
} else
end = 1;
}
} else { // Sinon, on affiche un message
list = NULL;
printf("\nErreur: Clé non trouvée !");
}
fclose(file);
} else {
list = NULL;
printf("\nErreur lors de l'ouverture du fichier !");
}
return list;
}
void insert(struct eventlist *list, struct eventlist *event) {
struct eventlist *temp = list;
struct eventlist *lasttemp = NULL;
printf("\n(%s %s)", temp->event.username, event->event.username);
while (temp->nextelem != NULL && stricmp(temp->event.username, event->event.username)) {
temp = temp->nextelem;
}
lasttemp = temp;
while (temp != NULL && !stricmp(temp->event.username, event->event.username)) {
lasttemp = temp;
temp = temp->nextelem;
}
event->nextelem = temp;
lasttemp->nextelem = event;
}
struct event getEventFromString(char *str) {
struct event event;
event.nb = 0;
event.type = 0;
event.username = NULL;
event.message = NULL;
int time;
int type;
char *username = (char *) malloc(sizeof(char *) * strlen(str));
char *message = (char *) malloc(sizeof(char *) * strlen(str));
if (sscanf(str, "%d %d %s %[^\n]s", &(time), &(type), username, message)) {
event.nb = (time_t) time;
event.type = type;
event.username = username;
event.message = message;
}
return event;
}
char *getLine(FILE *file) {
char *line = NULL;
unsigned char c;
int end = 0;
int ln = 0;
printf("\ngetLine 1");
line = (char *) malloc(sizeof(char *) * 1);
printf("\ngetLine 2");
if (line != NULL) {
while(!feof(file) && !end) {
c = fgetc(file);
if (c != '\n' && c != '\r') {
printf("\nDEBUG: %c %d %s", c, ln, line);
line = (char *) realloc(line, sizeof(char *) * (ln + 2));
if (line != NULL) {
line[ln++] = c;
line[ln] = '\0';
} else
end = 1;
} else
end = 1;
}
line[ln] = '\0';
}
if (line[0] == '\0' || line[1] == '\0')
line = NULL;
return line;
}
int file_exists(const char *fname) {
FILE *file;
int returncode = 0;
if ((file = fopen(fname, "r"))) {
fclose(file);
returncode = 1;
}
return returncode;
}
char *dechiffrer(const char *pChaineChiffree) {
char *dechiff;
unsigned char car;
unsigned int i;
dechiff = malloc(strlen(pChaineChiffree) + 1);
for (i = 0; pChaineChiffree[i] != '\0'; i++) {
car = pChaineChiffree[i];
car = (car & 0x0F) << 4 | (car & 0xF0) >> 4;
// car -= 0x55;
dechiff[i] = car;
}
dechiff[i] = '\0';
return dechiff;
}
I think it's a bufferoverflow, but i don't know where is the problem, and why it's bugged.
The crash occured in this malloc:
printf("\ngetLine 1");
line = (char *) malloc(sizeof(char *) * 1);
printf("\ngetLine 2");
Please help me
Thanks
0ddlyoko
EDIT:
Ok i've found the problem, it was with all my sizeof(XXX *), i've just changed this to sizeof(XXX).
Thanks !

Read a string and get it in (int) in C

Can you help me? I have a string 23;56;36.6;run in a txt file.Then I am reading this string in order to use it for some work: I would like to take this values from a string, then compare them with some values in a code and output my result in console. I think,I should use atoi() function that make my string in numbers, for picking out , I am using strtok(). But how correctly should I record my tokens in loop while and the last token is a type of char. How can I do this work?
CODE:
void printInfo(int note)
{
int i;
FILE *out;
char str[250];
char sp[10]=";";
char *istr;
if ((out =fopen("test.txt","r"))==NULL)
printf("Error open, file\n");
else
{
for (i=0;i<note;i++)
{
fgets(str,250,out);
istr=strtok(str,sp);
while (istr != NULL)
{
printf("%d\n",atoi(istr));
istr=strtok(NULL,sp);
// I think, I need to create a variable for recording my values.
}
}
}
fclose(out);
}
I would use sscanf to convert the string to the three floats:
#include <stdio.h> // sscanf
#include <stdlib.h> // EXIT_SUCCESS
#include <string.h> // memset
int main(void) {
const char *input = "23;56;36.6;run";
int i;
float numbers[3] = {0, 0, 0};
char buf[10];
int nElementsRead;
// init buf
memset(buf, 0, sizeof(buf));
// sscanf returns the number of read elements
// or EOF on error
nElementsRead = sscanf(input, "%f;%f;%f;%9s", &numbers[0], &numbers[1], &numbers[2], buf);
if (nElementsRead == 4) {
printf("Successfully read %d elements\n", nElementsRead);
for (i = 0; i < 3; ++i) {
printf("number[%d]: %f\n", i, numbers[i]);
}
printf("Buffer is: %s\n", buf);
} else {
printf("Something went wrong!");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Another solution, using comparable records:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct MyRecord_s {
int f1;
int f2;
float f3;
char f4[10];
} MyRecord;
static int MyRecordInit( MyRecord * out, char * line ) {
if( out == NULL ) {
return 0;
}
char * f1 = strtok( line, ";" );
char * f2 = strtok( NULL, ";" );
char * f3 = strtok( NULL, ";" );
char * f4 = strtok( NULL, ";" );
if( f1 && f2 && f3 && f4 ) {
char * err = NULL;
out->f1 = strtol( f1, &err, 10 );
if( err && *err ) {
return 0;
}
out->f2 = strtol( f1, &err, 10 );
if( err && *err ) {
return 0;
}
out->f3 = strtof( f1, &err );
if( err && *err ) {
return 0;
}
strncpy( out->f4, f4, 10 );
out->f4[9] = '\0';
return 1;
}
return 0;
}
int MyRecordCmp( const MyRecord * r1, const MyRecord * r2 ) {
int diff = r1->f1 - r1->f2;
if( diff ) {
return diff;
}
diff = r1->f2 - r2->f2;
if( diff ) {
return diff;
}
float d = r1->f3 - r2->f3;
if( d > 0.000001 ) {
return +1;
}
if( d < -0.000001 ) {
return -1;
}
return strcmp( r1->f4, r2->f4 );
}
int main() {
char line1[] = "23;56;36.6;run";
char line2[] = "24;57;37.6;stop";
MyRecord r1, r2;
if( MyRecordInit( &r1, line1 ) && MyRecordInit( &r2, line2 )) {
printf( "cmp: %d\n", MyRecordCmp( &r1, &r2 ));
}
return 0;
}

glibc detected outfile: free(): invalid pointer:

My code is giving glibc error.Can someone please point out my mistake.
This code is used to tokenize words. How do i solve these problems using GDB or any other tool that can be helpful. The code is given below
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <string.h>
struct token
{
char *token;
int length;
};
void freeing(gpointer item)
{
free(((struct token*)item)->token);
}
int main(int argc, char* argv[])
{
int start1 = 0, start2 = 0, i =0;
struct token *tk = NULL;
char line[256], *temp = NULL;
FILE *fp1 = NULL;
FILE *fp2 = NULL;
GSList *list = NULL, *iterator = NULL;
fp1 = fopen(argv[1], "r");
fp2 = fopen(argv[2], "w+");
if (NULL == fp1)
{
fprintf(stderr,"cant open %s",argv[1]);
return 1;
}
if (NULL == fp2)
{
fprintf(stderr,"cant open %s",argv[2]);
return 1;
}
while (1)
{
if (NULL == fgets(line, 255, fp1))
break;
tk = (struct token *)malloc(sizeof(struct token));
start1 = -1; start2 = -1;
for(temp = line,i = 0; temp[i] != '\n'; i++)
{
if ((temp[i] == ',') || (temp[i] == ' ') || (temp[i] == ';') || (temp[i] == '.'))
start2 = i;
if (start1 == start2)
continue;
tk->token = strndup(line + (start1+1), start2 - (start1+1));
tk->length = strlen(tk->token);
list = g_slist_append(list, tk);
start1 = start2;
}
tk->token = strndup(line + (start1+1), strlen(line));
tk->length = strlen(tk->token);
printf("\ntk->length : %d\n",tk->length);
list = g_slist_append(list, tk );
}
for (iterator = list; iterator; iterator = iterator->next)
{
printf("%s -> ",((struct token *)(iterator->data))->token);
printf("%d\n",((struct token*)iterator->data)->length);
}
g_slist_foreach(list, (GFunc)freeing, NULL);
fclose(fp1);
fclose(fp2);
return 0;
}
the following code has the major problems fixed, but not the logic errors in the loop :
for(int i=0, temp = line; temp[i] != '\n'; i++)
{
...
}
tk->token = strndup(line + (start1+1), strlen(line));
tk->length = strlen(tk->token);
printf("\ntk->length : %d\n",tk->length);
list = g_slist_append(list, tk );
the suggested fixes for the majority of the problems in the posted code are corrected in the following:
added final call to g_slist_free(list); so all the instances of the struct token are returned to the heap.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <string.h>
#define MAX_LINE_LEN (256)
struct token
{
char *token;
int length;
};
void freeing(gpointer item)
{
free(((struct token*)item)->token);
}
int main(int argc, char* argv[])
{
if( 3 != argc )
{ // then incorrect number of command line arguments
fprintf( stderr, "USAGE: %s <inputfile> <outputfile>\n", argv[0]);
exit( EXIT_FAILURE );
}
// implied else, correct number of command line arguments
FILE *fp1 = NULL;
FILE *fp2 = NULL;
if( NULL == (fp1 = fopen(argv[1], "r") ) )
{ // then fopen failed
fprintf( stderr, "fopen for %s for read failed\n", argv[1]);
exit( EXIT_FAILURE );
}
// implied else, fopen successful
if( NULL == (fp2 = fopen(argv[2], "w+") ) )
{ // then fopen failed
fprintf( stderr, "fopen for %s for write failed\n", argv[2]);
fclose( fp1 ); // cleanup
exit( EXIT_FAILURE );
}
// implied else, fopen successful
GSList *list = NULL;
GSList *iterator = NULL;
char line[ MAX_LINE_LEN ];
struct token *tk = NULL;
char *temp = NULL;
while ( fgets(line, sizeof(line), fp1) )
{
if( NULL == (tk = malloc(sizeof(struct token)) ) )
{ // then malloc failed
perror( "malloc for struct token failed");
fclose( fp1 );
fclose( fp2 );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
size_t start1 = 0;
size_t start2 = 0;
temp = line;
for(size_t i=0; temp[i] != '\n'; i++)
{
if ((temp[i] == ',') || (temp[i] == ' ') || (temp[i] == ';') || (temp[i] == '.'))
start2 = i;
if (start1 == start2)
continue;
tk->token = strndup(line + (start1+1), start2 - (start1+1));
tk->length = (int)strlen(tk->token);
list = g_slist_append(list, tk);
start1 = start2;
} // end for
tk->token = strndup(line + (start1+1), strlen(line));
tk->length = (int)strlen(tk->token);
printf("\ntk->length : %d\n",tk->length);
list = g_slist_append(list, tk );
} // end while
for (iterator = list; iterator; iterator = iterator->next)
{
printf("%s -> ",((struct token *)(iterator->data))->token);
printf("%d\n",((struct token*)iterator->data)->length);
}
g_slist_foreach(list, (GFunc)freeing, NULL);
g_slist_free(list);
fclose(fp1);
fclose(fp2);
return 0;
}

Resources