Help with functions

This is a discussion on Help with functions within the C Programming forums, part of the General Programming Boards category; Could you please help me with the following: Correct the problems with this function, and then extend it so that ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Help with functions

    Could you please help me with the following:

    Correct the problems with this function, and then extend it so that it also returns a usable array of pointers to some structures.

    The extended version of this function should:

    a) Maintain the same "char *" return type which returns a pointer to a usable, for storing a C string, memory buffer.

    b) In addition, also return a usable array of pointers to structures. This array of pointers is of variable and random length, which must be obtained using GetNumberOfSomeSeqsToGenerate(), which is called, ONLY, from within this new extended version of the function.

    c) The type of these structures and the function that should be used to get these structures are defined as follows:
    Code:
        typedef struct {
            char* name;   /* '\0'-terminated C string */
            int   number;
        } SomeSeq;
    /* How many structures should be in the returned array
    */
    int GetNumberOfSomeSeqsToGenerate(void);

    /* Return the next structure (to fill the
    * abovementioned array with).
    * Caller is responsible for the cleanup of the returned structure
    * and all its content. The latter are all allocated in
    * dynamic memory.
    */
    SomeSeq * GetNextSomeSeq(void);

    Do NOT(!) use "C++" syntax.
    Use good programming practice, as much as these instructions allow.


    Write a function which calls this function, prints out all data returned by it, and makes sure there are no memory leaks.

    Do NOT(!) use "C++" syntax.
    Use good programming practice, as much as these instructions allow.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well initially, it would go something like this
    Code:
    SomeSeq * GetNextSomeSeq(void) {
        int num = GetNumberOfSomeSeqsToGenerate();
        SomeSeq *array = malloc ( num * sizeof *array );
        if ( array != NULL ) {
            for ( i = 0; i < num ; i++ ) {
                // do something with array[i].name
            }
        }
        return array;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Help with functions

    Hi,

    Thanks for your help. Just wondered if you are returning a locally declared pointer (array)?

    Also, these questions should be viewed in the context of the ones that you've already answered. Please let me know if there need to be any changes to your suggestion above, based on the following:

    Code:
    LINE    Contains
    50    char * b, q, *r;
    200    b=getbuf();
    201    q = *b;
    212    r= anotherfunction(b);
    213-300    /* we want to use q and  r here*/
    2000    char * getbuf()
    2001    {
    2002       char buff[8];
    2003-2050  /* unspecified, buff defined here *./
    2051      return (char *) buff;
    2052    }


    > 2051 return (char *) buff;
    You're returning a pointer to a local variable. Whilst this code might run, any attempt to dereference the pointer after the function returns would be a bad idea.

    So to answer your other questions:

    > What will be in variable q after line 201 is executed?
    Junk, or a crash.

    > Under what conditions might this not be so?
    Pretty much all conditions are bad for this program.

    > Is there an alternative, but equivalent, way to write line 2000? If so, what is it?
    Yes, follow the style set by fgets()

    > Is getbuf() a reasonable function?
    Not as written, for the reasons stated.

    > Will getbuf() execute at all?
    Probably, until you try and use the result.

    > Please comment on line 2051.
    Done.

    > Is getbuf() good practice, and why?
    No, see above.

    > What line not given should be provided for compilation?
    What do you mean? It probably will compile "as is", but that's no guarantee of success at run time.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by am2007
    Thanks for your help. Just wondered if you are returning a locally declared pointer (array)?
    What's being returned to the calling function is a copy of the starting address of the malloc'd memory. The local pointer is popped off the stack once the function ends, but at that point the copied address has already been returned. If we were dealing with a locally declared array and we returned the address of the array then that would be bad since the array is popped off the stack at the end of the function and the returned address would then be pointing to an invalid memory location which could easily be overwritten. However, since we are dealing with dynamically allocated memory, that memory (and it's associated address) remain valid until expressly free'd by the user to the point of persisting beyond the life of the function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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