• Advertisement
Sign in to follow this  

My code in bugs

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am more than sure that my code is really bugged. I am trying to read a binary file ,and get error and system slowdown that only restart can handle. So what I am doing wrong ? Here is main.c
#include <stdio.h>
#include "binaryIO.h"





char filename[1024]; //this is our file
FILE* file;

char *buffer;        //here is buffer where we save info
char *ibuffer;       //here is numeric values transformed
int bcount = 4;      //how mucht byte are we going to read per turn
int maxcount;        //maximum number of chars'
int fcounter;        //counts file



//transforming to dec values
char* ToInt(char* buf)
{
	
	char *result;
	char *temp;
	int j;
	unsigned char c;

	maxcount = 0;

	result = (char*)malloc(sizeof(char) * 1024 );
    temp = (char*)malloc(sizeof(char) * 255);
	//result = "\0";

	
	j = 0;

    for(j = 0; j < bcount; j++)
	{
		c = buf[j];
       sprintf(temp,"%d\t",c);
	   //counting length of characters
	  memcpy(result + maxcount,temp,lstrlen(temp)); 

	       maxcount += lstrlen(temp);

	}

    free(temp);

	return result;

}


// MAIN
int main()
{
	
	char ts;


	//reading input filename from keyboard
	printf("Please type filename to test ex- <test.txt> : \n");
	sprintf(filename,"%s","o754_3869_j.exe");
    puts(filename);

	//opening file


    //getting memory equal to amount of bytes
	buffer = (char*)malloc(sizeof(char) * bcount);
	ibuffer = (char*)malloc(sizeof(char) * bcount);

	fcounter = 0;
	

	BFileOpen(&file,filename);

    while(fcounter < 17043983)
	{
         buffer = BFileRBuf(&file,bcount);

		 ibuffer = ToInt(buffer);
       // printf("try=%d buf= %.*s  ibuf= %.*s \n",fcounter++,bcount,buffer,maxcount,ibuffer);
	//	printf("try =%d",fcounter++);
		 if((fcounter % 100000) == 0)
		 {
           printf("%d\n",fcounter);
		 }

              //somewhere hear here i get an error
		 if(fcounter >= 753629)
		 {
             printf("%d\n",fcounter);
		 }
		 fcounter++;
	}
	//closing after reading
	printf("try=%d buf= %.*s  ibuf= %.*s \n",fcounter++,bcount,buffer,maxcount,ibuffer);

	BFileClose(&file);


	//freeig memory
	free(buffer);
	return 0;
}


here is binaryIO.h
/* Binary.h

   Is used for working with files
   (opening writing and error checking)

    use FILE file;

	to create buffer and 

	FileOpen(&file....    
	
	ti use it

*/
#include "BinaryIO.c"

//Opening file
void BFileOpen(FILE **file,char* path);

//Creating file
void BFileCreate(FILE **file,char* path);

//Closing file
void BFileClose(FILE **file);

//Writes line to the file
void BFileWLine(FILE **file,char *line);

//Reads Line from a file
char *BFileRLine(FILE **file);

//Reads a buffer of characters from file
char *BFileRbuf(FILE **file,int count);



Here is binaryIO.c

/* BinaryIO.C
 - working with files */


#include <stdio.h>

#include <windows.h>




// FILEOPEN()

//opens file

void BFileOpen(FILE **file,char* path)
{
   *file = fopen(path,"rb"); 

   // open a text file for mode specified by user

   if(file==NULL) {
    // error here :( exeting
	MessageBox(NULL,"file open","ERROR",0);
    fclose(*file);  
    return;
   }

}

//
//
//
//
//
//
//
//

// FILECREATE()

//creates new file

void BFileCreate(FILE **file,char* path)
{
   *file = fopen(path,"wb"); 
   // open a text file for mode specified by user

   if(file==NULL) {
    // error here :( exeting
    MessageBox(NULL,"file create","ERROR",0);
    fclose(*file);  
    return;
   }


}

//
//
//
//
//
//
//
//

// FILECLOSE()

//closes file
void BFileClose(FILE **file)
{
    fclose(*file);
}

//
//
//
//
//
//
//
//

// FILEWRITELINE()

//Writes line to the file
void BFileWLine(FILE **file,char *line)
{
   int c;

   while(c = *line++)
	   putc(c,*file);
   
   // draws error if found
   if (ferror(*file) != 0)
   {
	 char temp[25];

	 sprintf(temp,"file write error = %d",ferror(*file));
	 puts(temp);

    MessageBox(NULL,temp,"ERROR",0);
   }
}

//
//
//
//
//
//
//

// FILEREADLINE()

//Writes line to the file
char *BFileRLine(FILE **file)
{
  int i;
  char *result;
  char c;

  i = 0;
  //getting memory for strings
  result = (char*)malloc(sizeof(char) * 1024);

  while(TRUE)
  {
     c = getc(*file);

	 if((c == '\n')||(c == EOF))
	 break;
	 result[i++] = c; 
  }

  //putting end to string
  result[i] = '\0';
  return result;
}

//
//
//
//
//
//
//

// FILEREADBUFFER()

//Writes line to the file
char *BFileRBuf(FILE **file,int count)
{
  int i;
  char *result;
  unsigned char c;

  i = 0;

  //returning nothing
  if(count == 0)
  return NULL;


  //getting memory for strings
  result = (char*)malloc(sizeof(char) * count);

  while(i < count)
  {
     

	 if((c = getc(*file)) == NULL)
	 break;

	 result[i++] = c; 
  }

  //putting end to string
  return result;
}





Share this post


Link to post
Share on other sites
Advertisement
Ok i think i got the idea what is wrong. by the info from taskmanager my memory usage is increase for about 1 GB and more. As it seems each time i use BReadLine or ToInt the memory is reserved more and more. Well I can use &var to pass direct address to my variable, but if i wan't to return some dynamiclly allocated variable like char* by return , how can I do that ? Anyone can help ?

Share this post


Link to post
Share on other sites
Well...

In your ToInt-funktion you allocate memory for your result and return the pointer to it:


char* ToInt(char* buf)
{
char *result;
[...]
result = (char*)malloc(sizeof(char) * 1024 );
[...]
return result;
}


than in your main-loop you use this function:


[...]
while(fcounter < 17043983)
{
ibuffer = ToInt(buffer);
[...]
}


But you do not free this memory. Guess what? you have a really big memory leak here. Actually you try to allocate 17043983 * 1024 bytes, thats a total of about 16GB of memory. No wonder your computer pukes...


Share this post


Link to post
Share on other sites
Ditto with buffer and the BFileRBuf function. free what you malloc.

Enigma

Share this post


Link to post
Share on other sites
You might find WinBar a neat tool to monitor your CPU/Memory usage when running your application. You can pick extreme memory usage like this out in a second.

Share this post


Link to post
Share on other sites
Ok Thank's for replyes , I got the point what is wrong . But i got the other question if i have a function like

BFileRLine(&file)

//and i wan't it to use like ...

char* buffer;
buffer = (char*)malloc(sizeof(char) * 1024);

// !
buffer = BFileRLine(&file);

How I can do that ? I can add extra parameter like BFileRLine(&file,&buffer) - but is there other solution available like above ?

---->
2 Structural :

BTW nice thing , i am going to use it for my debugging now :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement