Problems with writing to a file 
Author Message
 Problems with writing to a file

Hi,

I'm trying to write an own malloc()-function. The function works fine but I
would like to add an additional feature to it: to be able to "record" all
calls to malloc and write them to a file. I have added these lines to my
malloc, which are performed each time it is called:

FILE *f;

  f = fopen("trace.log", "a+");
  fprintf(f, "testing\n");
  fclose(f);

The strange thing is that if I have the code for malloc in the same
source-file as the main-function, which is to make calls to it, things work
fine. Of course, I want to have malloc in a separate file, to be able to
compile it with different programs. When I try to compile it from these two
different source-files it will run but gives a "Segmentation fault" when the
malloc-function is called.

I would be very grateful for tips about what might be wrong.

Regards,
Per



Fri, 29 Oct 2004 04:41:05 GMT  
 Problems with writing to a file

Quote:

> I'm trying to write an own malloc()-function.

Standard C doesn't provide a way for you to do that.  However:

Quote:
> The function works fine but I
> would like to add an additional feature to it: to be able to "record" all
> calls to malloc and write them to a file. I have added these lines to my
> malloc, which are performed each time it is called:

> FILE *f;

>   f = fopen("trace.log", "a+");
>   fprintf(f, "testing\n");
>   fclose(f);

My guess is that one of these functions, probably fopen(), calls
malloc() itself, thus you hit recursion.
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Fri, 29 Oct 2004 05:31:57 GMT  
 Problems with writing to a file


Quote:

> > I'm trying to write an own malloc()-function.

> Standard C doesn't provide a way for you to do that.  However:

> > The function works fine but I
> > would like to add an additional feature to it: to be able to "record"
all
> > calls to malloc and write them to a file. I have added these lines to my
> > malloc, which are performed each time it is called:

> > FILE *f;

> >   f = fopen("trace.log", "a+");
> >   fprintf(f, "testing\n");
> >   fclose(f);

> My guess is that one of these functions, probably fopen(), calls
> malloc() itself, thus you hit recursion.
> --
> "Large amounts of money tend to quench any scruples I might be having."
>   -- Stephan Wilms

Is your function named malloc?  When I wrote malloc, to avoid problems like
that, I named mine jmalloc.


Fri, 29 Oct 2004 06:08:58 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem opening a file for read while it is open for write

2. File Read/Write Problem

3. problems writing to binary files

4. help - write to file in interrupt problems

5. Q: problem: CGI-programming with C (writing files and sending picyures)

6. problem writing to file

7. PROBLEM READING/WRITING FILE __SIMULTANEOUSLY__!

8. File reading/writing problem

9. Problem with NT and writing large files

10. Problem writing to a writable file

11. file reading/writing speed problem

12. Problems with MFC-Internet Functions, writing a File

 

 
Powered by phpBB® Forum Software