I have written the following:
My program breaks at free(fHandle);Code:FILE **fHandle;fHandle = (FILE**) calloc (4,sizeof (FILE*)); /*-----open and write and close files (code skipped)-----*/ free(fHandle);
What am I missing?
I have written the following:
My program breaks at free(fHandle);Code:FILE **fHandle;fHandle = (FILE**) calloc (4,sizeof (FILE*)); /*-----open and write and close files (code skipped)-----*/ free(fHandle);
What am I missing?
Last edited by uz_mz; 11-12-2013 at 04:46 AM.
Maybe the problem lies in code that you did not show, e.g., you forgot to #include <stdlib.h> or there's undefined behaviour somewhere.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You're probably missing some other code that is molesting a pointer.
Your code, as shown (and assuming it is all in the one function and <stdlib.h> has been #include'd) does nothing wrong.
However, a crash on free() is a common symptom of some other code having molested a pointer, with the effect of overwriting data structures used internally by malloc/calloc/realloc/free. Whatever the offending code is, you haven't shown it. But it can be any code executed before the call of free(), including before the definition of fHandle.
skipped code:
I ran through the code by setting breakpoints. It goes thru smoothly.Code:char cfileName[20]; for (int i=0; i<4; i++) { sprintf(cfileName, "Signal_%d.pcm",i); fHandle[i] = fopen(cfileName, "ab"); fclose(fHandle); }
It breaks only where I free the file pointer. If I comment out that line, then it does not break anywhere else.
(implying the mistake is in the mem dealloc).
Even after I include fWrite, it runs well and the output file is written properly. Only when I exit the program, it breaks in the destructor where memory is freed for file pointer..
I dont know whats the reason! :-(
mostly crash on free indicates corrupted heap
corrupted heap mostly caused by 2 main errors - memory overrun on the allocated array or double free
So problem could relate to any other dynamically allocated memory and not to this pointer exactly
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I'm just curious. why do you need to dynamically allocate a pointer?
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
you can do that with a normal pointer on the stack.
is effectively the same asCode:FILE* file = fopen(...);
the latter is harder to read, harder to maintain, risks additional memory leaks, and may be slower on some platforms.Code:FILE** pFile = malloc(sizeof(FILE*)); *pFile = fopen(...);
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I'm thinking the point was that he had n of them, for an n not necessarily specified in advance. (Granted we could now do a variable-length array, but either way we have to do something beyond a regular declaration.)
@all,
I just realized that when I use an array declaration:
it works fine.Code:char cfileName[20];
But I also need to use an array of strings. Therefore, I did the following:
char** cfileName;
and allocate memory for it and use it in fopen, its molesting the fHandle pointer.
Can u please show me how can I allocate memory for 4 strings each 20 characters long? Are the above lines correct? please advice.Code:cfileName = (char**) calloc (4, sizeof(char*)); for(i=0;i<20;i++) { cfileName[i] =(char*) calloc(20, sizeof(char)); }
@Elkvis: ya..but I need to open all files in advance, then write them during processing and close them all when I exit the program(fclose and free memory in destructor). therefore, I open pFile[0], pFile[1] and so on.. and only write them during processing using fwrite.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You have a typo error: fclose(fHandle[i]);Originally Posted by uz_mz
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
sorry..my mistake..was a typo.
Also, since I am allocating memory for 1st dimension only,
and free it by:Code:fHandle = (FILE**) calloc (4,sizeof (FILE*));
I do not need to loop thru the fHandle dimensions as in:Code:free(fHandle);
?? please suggest..Code:for(i=0;i<4;i++) { free(fHandle[i]); }
Last edited by uz_mz; 11-13-2013 at 02:36 AM.