Thread: dynamic allocation of file* pointers

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    32

    dynamic allocation of file* pointers

    I have written the following:

    Code:
     FILE **fHandle;fHandle = (FILE**) calloc (4,sizeof (FILE*));
    
    /*-----open and write and close files (code skipped)-----*/
    
    free(fHandle);
    My program breaks at free(fHandle);

    What am I missing?
    Last edited by uz_mz; 11-12-2013 at 04:46 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    skipped code:

    Code:
    char cfileName[20];
    for (int i=0; i<4; i++)
          {
             sprintf(cfileName, "Signal_%d.pcm",i);
             fHandle[i] = fopen(cfileName, "ab");
             fclose(fHandle);
          }
    I ran through the code by setting breakpoints. It goes thru smoothly.
    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! :-(

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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?

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    Quote Originally Posted by Elkvis View Post
    I'm just curious. why do you need to dynamically allocate a pointer?
    Because I want to open and write and close files dynamically.

    @grumpy: They are not in the same function.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by uz_mz View Post
    Because I want to open and write and close files dynamically.
    you can do that with a normal pointer on the stack.

    Code:
    FILE* file = fopen(...);
    is effectively the same as

    Code:
    FILE** pFile = malloc(sizeof(FILE*));
    *pFile = fopen(...);
    the latter is harder to read, harder to maintain, risks additional memory leaks, and may be slower on some platforms.
    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?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    @all,
    I just realized that when I use an array declaration:

    Code:
    char cfileName[20];
    it works fine.
    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.
    Code:
    cfileName = (char**) calloc (4, sizeof(char*));
    for(i=0;i<20;i++)
    {
    cfileName[i] =(char*) calloc(20, sizeof(char));
    }
    Can u please show me how can I allocate memory for 4 strings each 20 characters long? Are the above lines correct? please advice.

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    @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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by uz_mz View Post
    skipped code:

    Code:
    char cfileName[20];
    for (int i=0; i<4; i++)
          {
             sprintf(cfileName, "Signal_%d.pcm",i);
             fHandle[i] = fopen(cfileName, "ab");
             fclose(fHandle);
          }
    I ran through the code by setting breakpoints. It goes thru smoothly.
    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! :-(
    You're calling fclose(fHandle) on every iteration of the loop. That is certainly a mistake: fclose should not even be called with fHandle to begin with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    sorry it should be:

    fclose([fHandle[i]);

    Is this right?

    Quote Originally Posted by laserlight View Post
    fclose should not even be called with fHandle to begin with.
    Then whats the correct way of calling fclose()?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by uz_mz
    sorry it should be:

    fclose([fHandle[i]);

    Is this right?
    You have a typo error: fclose(fHandle[i]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    sorry..my mistake..was a typo.
    Also, since I am allocating memory for 1st dimension only,
    Code:
    fHandle = (FILE**) calloc (4,sizeof (FILE*));
    and free it by:
    Code:
    free(fHandle);
    I do not need to loop thru the fHandle dimensions as in:
    Code:
    for(i=0;i<4;i++)
    { 
       free(fHandle[i]);
    }
    ?? please suggest..
    Last edited by uz_mz; 11-13-2013 at 02:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation using pointer to pointers
    By mp252 in forum C++ Programming
    Replies: 12
    Last Post: 06-22-2013, 05:34 PM
  2. Pointers and Dynamic memory allocation help needed
    By dave_the_bear10 in forum C Programming
    Replies: 3
    Last Post: 11-09-2011, 04:23 PM
  3. Replies: 16
    Last Post: 02-16-2011, 01:52 AM
  4. Dynamic allocation for array of pointers to char
    By bivhitscar in forum C Programming
    Replies: 7
    Last Post: 05-20-2006, 07:04 AM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM