Caller/Callee Allocation

This is a discussion on Caller/Callee Allocation within the C Programming forums, part of the General Programming Boards category; Does anyone know in what situations it is generally advised to use one or the other. I have the feeling ...

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398

    Caller/Callee Allocation

    Does anyone know in what situations it is generally advised to use one or the other.

    I have the feeling that if you stay within C, it really doesn't matter a whole lot, but if you are mixing C with various other languages it is probably better to have the callee allocate any needed memory.

    If you don't feel like explaining, I don't mind reading a good article, I just couldn't find anything.
    Last edited by carrotcake1029; 12-08-2008 at 03:05 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'd rather do the allocation in the callee function so that if I'm calling it multiple times I can take advantage of the fact that it's a function and that's what they're for.

    What this has to do with other languages I'm not sure.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Doesn't matter whether it's the caller or the callee as long as the storage comes from the heap and not from the stack.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    Doesn't matter whether it's the caller or the callee as long as the storage comes from the heap and not from the stack.
    I believe that in C, if you do the allocating, it always comes from the heap.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Only malloc() and its family come from the heap but not an array defined inside a function as in
    Code:
    int f(void)
    {
        char thisarray[100];
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are obviously several aspects here:
    If the caller allocates, the caller knows what has been allocated, and is then also responsible to deallocate by the nature of things.
    If the callee allocates, the caller doesn't KNOW intrinsicly that a memory allocation has happened, and thus don't know to clean up afterwards. That's fine if there is a natural "get, relase" type system in place.

    There are obviously also other aspect to take into account:
    - if a typical case is to call the callee several times for slightly different uses, and the same allocation can be used several times, you can only do that if the caller allocates, for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    - if a typical case is to call the callee several times for slightly different uses, and the same allocation can be used several times, you can only do that if the caller allocates, for example.

    Mats
    Oh come on. Doesn't this function have some arguments or something?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 09:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 10:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 06:47 PM
  4. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 06:43 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21