Thread: dynamic allocation of file* pointers

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're allocating memory for the 1st dimension only, then you only have one dimension. If you intend to have a 2D array (ie an array of strings) then you need to be allocating the second dimension as well (as you did in post #13).

  2. #17
    Registered User
    Join Date
    Oct 2013
    Posts
    32
    Quote Originally Posted by tabstop View Post
    If you're allocating memory for the 1st dimension only, then you only have one dimension. If you intend to have a 2D array (ie an array of strings) then you need to be allocating the second dimension as well (as you did in post #13).
    Ya, but does it hold for array of FILES also?
    As in:
    Code:
    FILES **fHandle;

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by uz_mz
    does it hold for array of FILES also?
    calloc(4, sizeof(FILE*)) means that you have a dynamic array of 4 pointers to FILE, with the return value of calloc pointing to the first of the 4 pointers.
    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

  4. #19
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You don't want to use calloc for pointer types, since it initialises them with all bits zero, which is a meaningless value for pointers. malloc would be a better choice and, if you need them initialised with null pointers, you could manually assign each element a null pointer.

    Another thing to keep in mind is that your C implementation will have a limit for the number of FILE objects that may be open simultaneously, so you might consider redesigning your program to have few open at once, if that's a possibility.

    But otherwise, you seem to be going about this correctly. You should check the return value of calloc/malloc and avoid casting their return values, as that will help you diagnose any errors that may occur. You can also use valgrind's memcheck tool if you have further problems.

    In response to post #15, you should only free() something if it has been returned directly by malloc/calloc/realloc, or it's a null pointer. Similarly, you should only fclose() something if it's been opened with fopen/freopen or it's stdin/stdout/stderr. So, no, you would only call free() once in this case, but you may need to call fclose() up to four times.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

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