Proper modularization technique

Discussion in 'C Programming' started by drhowarddrfine, Nov 10, 2008.

  1. I'm unsure the best way to do this. I want to call a function that
    will generate a string to be used by the calling routine. Where is
    the best place to store that string? iow:
    1) Should I set aside an array in the calling routine and have the
    function strcpy to that?
    routine(){
    char string[16];

    called_function(string);
    }

    called_function(char* string){
    strcpy(string,"string");
    }

    2) Make the string area static for the calling routine?
    routine(){
    char *string_pointer;
    string_pointer=called_function();
    }

    char* called_function(char* string){
    static char string[16];
    strcpy(string,"string");
    return string;
    }

    3) Make the string area global to that file?
    char string[16];

    calling_routine(){called_function();}
    called_function(){strcpy(string,"string");}

    Part 2 of this would be, I have a whole bunch of these small routines
    that need less than 256 bytes of memory for storing such strings.
    Would it be better to just malloc to acquire the memory for that and
    then release it? Does that cause relative program slow down doing all
    that acquire/releasing?
     
    drhowarddrfine, Nov 10, 2008
    #1
    1. Advertising

  2. drhowarddrfine

    maverik Guest

    On Nov 10, 7:51 pm, drhowarddrfine <> wrote:
    > I'm unsure the best way to do this.  I want to call a function that
    > will generate a string to be used by the calling routine.  Where is
    > the best place to store that string?


    There are many variants accroding to your application design at all.
    IMHO, as I understand your ideas, you can just store string within
    calling function and just return pointer:

    void
    routine() {
    char *processed = called_function(*processed);
    // you can ignore return value
    // char *processed;
    // called_function(*processed);
    ...
    // code goes here
    ...
    // Of course you must free the memory
    free(processed);
    }

    char*
    called_function(char * str) {
    // allocates memory
    strncpy(str, "YourString", ...) ;
    return str;
    }
     
    maverik, Nov 10, 2008
    #2
    1. Advertising

  3. drhowarddrfine

    maverik Guest

    First, I mistyped
    >     // called_function(*processed);

    I mean // called_function(processed);

    Second, I think that seperating allocation and deallocation is not a
    good idea. May be you should store string within the caller:

    void
    routine() {
    char *processed = malloc(...);
    if(processed == NULL) { ... }

    // Of course you may write
    // char processed[123];

    called_function(processed);
    ...
    // code goes here
    ...

    // In case of malloc
    free(processed);
    }

    void
    called_function(char * str) {
    strcpy(str, "YourString") ;
    }

    What others say?
     
    maverik, Nov 10, 2008
    #3
  4. drhowarddrfine

    Guest

    On Nov 10, 11:51 am, drhowarddrfine <> wrote:
    > I'm unsure the best way to do this.  I want to call a function that
    > will generate a string to be used by the calling routine.  Where is
    > the best place to store that string?  iow:
    > 1) Should I set aside an array in the calling routine and have the
    > function strcpy to that?


    No, because

    - the way you wrote it, called_function() will have no way to know
    how large the passed array is, and thus is likely to write pass the
    end of it. Of course, this is easily fixed by having the function
    accept an extra argument that represents the array size, but then
    there's the possibility that
    - the function might need to store more characters than the array
    can hold, thus causing truncation. If truncation isn't a problem
    (e.g., you can still get the remaining data in a future call, as with
    fgets()), then this is a good way to go.

    > 2) Make the string area static for the calling routine?


    No, because

    - if your application is multi-threaded, this will break things;
    - you can't have the function generate two or more strings and hold
    them at the same time (in other words, calling the function will
    modify what was previously on the array, even if you still need it);
    - there's still the issue of truncation.

    > 3) Make the string area global to that file?


    No, because

    - "global variables are evil." :)

    > Would it be better to just malloc to acquire the memory for that and
    > then release it?  Does that cause relative program slow down doing all
    > that acquire/releasing?


    That has the advantage that you can call realloc() to make the buffer
    as large as necessary. And yes, allocating on the heap is generally
    slower than allocating on the stack, but you probably don't need to
    worry about that unless you're doing micro-optimization.

    Sebastian
     
    , Nov 10, 2008
    #4
  5. Thanks guys. You verified what I was thinking. My sample code above
    was just to give a view of what I was trying to say though I may have
    confused a little with some omissions.
     
    drhowarddrfine, Nov 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Rohleder

    Modularization using modularized xhtml

    Peter Rohleder, Feb 20, 2004, in forum: XML
    Replies:
    0
    Views:
    357
    Peter Rohleder
    Feb 20, 2004
  2. ern
    Replies:
    36
    Views:
    841
    Michael Wojcik
    Jan 19, 2006
  3. C.W.Holeman II
    Replies:
    0
    Views:
    465
    C.W.Holeman II
    Apr 8, 2007
  4. Replies:
    0
    Views:
    987
  5. sunckell

    Proper Technique for DBD::mysql install

    sunckell, Apr 7, 2009, in forum: Perl Misc
    Replies:
    0
    Views:
    141
    sunckell
    Apr 7, 2009
Loading...

Share This Page