malloc&free 
Author Message
 malloc&free

i've wrote a bookmark file content reader function to extract the URL from a
bookmark file
& the code is
////////////////////////////////////////////////////////
void Cpopupdiag::ReadFileContent(CString filename)
{
char *urlstring=(char*)malloc(300);
char buffer[300];
urlstring=NULL;
ifstream inFile(filename);
 while (!inFile.eof())
 {
  inFile.getline(buffer,300);
  urlstring=strstr(buffer,"URL=");
         }
 inFile.close();
 }
free(urlstring);
Quote:
}

/////////////////////////////////////////////
it works well when the program reads only few files...
but it crash when there are large amount of
bookmark files...the reason is the program keep allocate new memory block
for the variable "urlstring" in each call of this function but the memory
isn't free until the program terminated, so keep callin this function
will drain up all the memory & crash..
but my question is...I've placed the statment "free(urlstring)" in the
end of the function...why the program wont free the memory block in the
end of this function but in program termination instead?
pls help...


Wed, 29 Jan 2003 03:00:00 GMT  
 malloc&free

Quote:

> i've wrote a bookmark file content reader function to extract the URL from a
> bookmark file
> & the code is
> ////////////////////////////////////////////////////////
> void Cpopupdiag::ReadFileContent(CString filename)
> {
> char *urlstring=(char*)malloc(300);
> char buffer[300];
> urlstring=NULL;
> ifstream inFile(filename);
>  while (!inFile.eof())
>  {
>   inFile.getline(buffer,300);
>   urlstring=strstr(buffer,"URL=");
>          }
>  inFile.close();
>  }
> free(urlstring);
> }
> /////////////////////////////////////////////
> it works well when the program reads only few files...
> but it crash when there are large amount of
> bookmark files...the reason is the program keep allocate new memory block
> for the variable "urlstring" in each call of this function but the memory
> isn't free until the program terminated, so keep callin this function
> will drain up all the memory & crash..
> but my question is...I've placed the statment "free(urlstring)" in the
> end of the function...why the program wont free the memory block in the
> end of this function but in program termination instead?
> pls help...

You are in the wrong ng, this is comp.lang.c, you better try comp.lang.c++.

<OT>
To me C++ is only incrementing a variable named C, but I don't think that your
usage of malloc / free here is needed.

In particular, it looks like

char *urlstring=(char*)malloc(300);

can simply be replaced by

char *urlstring = NULL;

Note that strstr() returns a pointer.
</OT>

--
Tor



Wed, 29 Jan 2003 03:00:00 GMT  
 malloc&free

Quote:

> i've wrote a bookmark file content reader function to extract the URL from a
> bookmark file
> & the code is
> ////////////////////////////////////////////////////////
> void Cpopupdiag::ReadFileContent(CString filename)
> {
> char *urlstring=(char*)malloc(300);

allocates 300 characters, fine, but you don't need the cast to (char *),
just char *urlstring= malloc(300);

Quote:
> char buffer[300];

declares an array of 300 characters, fine.

Quote:
> urlstring=NULL;

changes urlstring to point to nothing. Now you have lost the pointer to
the memory allocated by malloc, so you will never be able to free it.
Why allocate memory you never use?

Quote:
> ifstream inFile(filename);
>  while (!inFile.eof())
>  {
>   inFile.getline(buffer,300);

Probably reads stuff into buffer; this looks like c++, which has its own
newsgroup.

Quote:
>   urlstring=strstr(buffer,"URL=");

Find the first occurrence of URL= in buffer. Since this sets urlstring,
you really didn't need to set it to NULL earlier.

Quote:
>          }
>  inFile.close();
>  }
> free(urlstring);

Passes something to free that was not allocated by malloc or calloc. A
very bad error.

Quote:
> }
> /////////////////////////////////////////////
> it works well when the program reads only few files...
> but it crash when there are large amount of
> bookmark files...the reason is the program keep allocate new memory block
> for the variable "urlstring" in each call of this function but the memory
> isn't free until the program terminated, so keep callin this function
> will drain up all the memory & crash..
> but my question is...I've placed the statment "free(urlstring)" in the
> end of the function...why the program wont free the memory block in the
> end of this function but in program termination instead?
> pls help...

This appears to be a program that can be fixed just by taking things
out. (I assume there is something more to go in it to actually use the
information it finds.)

void Cpopupdiag::ReadFileContent(CString filename)
{
char *urlstring;
char buffer[300];
ifstream inFile(filename);

 while (!inFile.eof())
 {
  inFile.getline(buffer,300);
  urlstring=strstr(buffer,"URL=");
 }
 inFile.close();

Quote:
}



Wed, 29 Jan 2003 03:00:00 GMT  
 malloc&free
Hallo Ben,

 void Cpopupdiag::ReadFileContent(CString filename)
{
  char *buffer = (char*)malloc (300);
  char *urlstring = NULL;

  ifstream inFile (filename);

  while (!inFile.eof () && buffer) {
   inFile.getline (buffer, 300);
   urlstring = strstr (buffer, "URL=");
   if (urlstring)
        do something.....
  }
  inFile.close ();

Quote:
}
  free (buffer);
}

One line in the file will never be greater then 300 chars?
Isn*t there new and delete under C++ for dyn. memory
allocations.
Regards Andreas

Quote:
> i've wrote a bookmark file content reader function to extract the URL from
a
> bookmark file
> & the code is
> ////////////////////////////////////////////////////////
> void Cpopupdiag::ReadFileContent(CString filename)
> {
> char *urlstring=(char*)malloc(300);
> char buffer[300];
> urlstring=NULL;
> ifstream inFile(filename);
>  while (!inFile.eof())
>  {
>   inFile.getline(buffer,300);
>   urlstring=strstr(buffer,"URL=");
>          }
>  inFile.close();
>  }
> free(urlstring);
> }
> /////////////////////////////////////////////
> it works well when the program reads only few files...
> but it crash when there are large amount of
> bookmark files...the reason is the program keep allocate new memory block
> for the variable "urlstring" in each call of this function but the memory
> isn't free until the program terminated, so keep callin this function
> will drain up all the memory & crash..
> but my question is...I've placed the statment "free(urlstring)" in the
> end of the function...why the program wont free the memory block in the
> end of this function but in program termination instead?
> pls help...



Thu, 30 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. malloc & free in c#

2. Malloc & free acting strange

3. simple question about malloc & free

4. malloc/calloc & free

5. memory to free or not to free, to malloc or not to malloc ?

6. ?malloc & free vs. new & delete

7. new & delete VS malloc & free

8. Override malloc,calloc,realloc and free?

9. malloc/free reference implementations

10. malloc, realloc, free questions

11. malloc and free pb

12. malloc/free work arrays - time?

 

 
Powered by phpBB® Forum Software