Re: How is memory allocated

Discussion in 'C Programming' started by Samuel Thomas, Jun 30, 2003.

  1. Hello Everybody,

    Thanks for helping with my query. I am very sorry for writing such
    code, its because I am still learning things. Based on the responses I
    got, can I now sum it like this?

    1. Suppose I pass a character pointer to a function defined as char *
    b(char*) from another function a().

    Case 1.
    ---------

    I want to play with the contents of the array that have been passed in
    but I don't want the original copy getting spoiled.

    I guess in this case I will create a new array either dynamically
    using malloc or use a statically defined array in function b, copy the
    contents of the passed in array to my local copy and then use it.

    Case 2.
    ---------

    I want to play with the contents of the array that have been passed
    and I don't bother whether the contents get spoiled.

    I would just use the char * that receives data in function b to get
    on.

    Case 3.
    ----------

    I want to return a char array back to the calling function a.

    I create a dynamic array using malloc in function b and return it.
    Since this then becomes independent of stack unwinding, I can be sure
    that I still have valid data in function a().

    What do my friends say about this?

    Thanks a million for all the help.
    Warm Regards
    :) Sam
     
    Samuel Thomas, Jun 30, 2003
    #1
    1. Advertising

  2. Samuel Thomas

    Morris Dovey Guest

    Samuel Thomas wrote:

    <snip>

    > What do my friends say about this?


    Nicely grasped.
    --
    Morris Dovey
    West Des Moines, Iowa USA
    C links at http://www.iedu.com/c
     
    Morris Dovey, Jun 30, 2003
    #2
    1. Advertising

  3. Dear Friends,

    I just want to add confirm one more thing about this issue. Can I say
    that when I pass variables between functions as arguments, if the
    variable is a basic data type like int, float, char I get a copy of
    the /content/ of the variable. When I however pass "collection" data
    types like arrays of basic data types, structures, unions etc, I get a
    copy of the /start address/ of the "collection" data type. If this is
    correct, it gets simple to remember :)

    Thanks for all the help
    Warm Regards
    :) Sam
     
    Samuel Thomas, Jul 2, 2003
    #3
  4. Samuel Thomas

    Chris Dollin Guest

    Samuel Thomas wrote:

    > I just want to add confirm one more thing about this issue. Can I say
    > that when I pass variables between functions as arguments, if the
    > variable is a basic data type like int, float, char I get a copy of
    > the /content/ of the variable. When I however pass "collection" data
    > types like arrays of basic data types, structures, unions etc, I get a
    > copy of the /start address/ of the "collection" data type. If this is
    > correct, it gets simple to remember :)


    It's not correct.

    The C rule is simple: arguments are passed by value. ints, floats,
    structs, pointers - it's all the same.

    There are *no* array values. In any value context, an array is
    represented by a pointer to its first element. If you "pass an array
    as an argument", you're really passing the address of its first element,
    by value, into the called function. This is not to do with argument
    passing: the same effect happens in assignments.

    [There are no array arguments, either; what looks like an array argument
    is silently rewritten into a pointer argument.]

    [Nitpick: there is one place that I can think of where there is an
    array value: in initialising a char[] array from a string literal:

    char spoo[] = "buffy the vampire slayer";

    This may generalise a little, but only a little.]

    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
     
    Chris Dollin, Jul 2, 2003
    #4
  5. Samuel Thomas

    Dan Pop Guest

    In <bduf2g$ja4$> Chris Dollin <> writes:

    >The C rule is simple: arguments are passed by value. ints, floats,
    >structs, pointers - it's all the same.
    >
    >There are *no* array values. In any value context, an array is
    >represented by a pointer to its first element. If you "pass an array
    >as an argument", you're really passing the address of its first element,
    >by value, into the called function. This is not to do with argument
    >passing: the same effect happens in assignments.
    >
    >[There are no array arguments, either; what looks like an array argument
    >is silently rewritten into a pointer argument.]


    You seem to be talking about array *parameters* here, not about array
    *arguments*. C99 makes a clear distinction between the two concepts.

    >[Nitpick: there is one place that I can think of where there is an
    >array value: in initialising a char[] array from a string literal:
    >
    > char spoo[] = "buffy the vampire slayer";


    I can't see any array value here, only an array *declarator* and a
    string literal acting as initialiser.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 2, 2003
    #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. Duncan
    Replies:
    0
    Views:
    405
    Duncan
    Jul 21, 2003
  2. Laura Heinzmann

    How to check memory size allocated to JVM?

    Laura Heinzmann, Feb 16, 2005, in forum: Java
    Replies:
    1
    Views:
    8,229
    John McGrath
    Feb 16, 2005
  3. Kjell Arne Johansen

    How do I know if memory is already allocated?

    Kjell Arne Johansen, Sep 1, 2003, in forum: C++
    Replies:
    8
    Views:
    406
    Kjell Arne Johansen
    Sep 2, 2003
  4. C++fan
    Replies:
    4
    Views:
    1,779
    Jack Klein
    Jan 7, 2004
  5. Replies:
    5
    Views:
    662
    Matt Wharton
    Dec 9, 2004
Loading...

Share This Page