Re: How is memory allocated

Discussion in 'C Programming' started by Micah Cowan, Jun 28, 2003.

  1. Micah Cowan

    Micah Cowan Guest

    (Samuel Thomas) writes:

    > Hello Everybody,
    >
    > Could you please go through the code I wrote and help me with my
    > doubts?
    >
    >
    > #include <stdio.h>
    > #include <conio.h>


    The above (conio) is not a standard C header, and off-topic here.

    >
    > void printnamefirst(char[]);
    > void printnamesec(char[]);
    >
    > void main()
    > {
    > clrscr();


    Great. You just obliterated the tail-end of an output run I still wanted.

    > printnamefirst(name);
    > }
    >
    > void printnamefirst(char nm[])
    > {
    > char nam[20] ="Samuej Thomas";
    > printnamesec(nam);
    > printf("%s \n",nam);
    > }
    >
    > void printnamesec(char ns[])
    > {
    > printf("%s \n",ns);
    > ns[5]='l';
    >
    > }
    >
    > 1.Is it safe to use the variables that are allocated in one function,
    > in another function as I have done by printing a string in the
    > printnamesec, but which has been declared in printnamefirst function?
    > When does it become unsafe to use variables declared in one function
    > else where?


    It is safe to refer to that object until execution leaves the block in
    which it was defined. It is unsafe any other time. Here are some
    unsafe examples:

    void printnamefirst(char nm[])
    {
    char *namptr;

    {
    char nam[20] = "Samuej Thomas";
    namptr = nam;
    } /* nam no longer exists once we reach here.

    printnamesec(nam); /* Uh-oh... */
    }

    --------------------------------------------------

    #include <stdio.h>

    int main(void)
    {
    char *nam = get_name();

    printf("%s\n",nam); /* Uh-oh... */
    return 0;
    }

    char *get_name(void)
    {
    char nam[] = "Samuej Thomas";
    return nam;
    } /* nam doesn't exist once this exits. */

    > 2.Is it possible to make 'pass by value' work with character strings
    > so that they dont get changed?


    In C, *all* parameters are passed by value; no exceptions.

    To avoid a string being changed, declare the parameter to be of type
    const char * rather than char *.

    > Do they always get passed as reference
    > values when passed across functions?


    C doesn't support anything being passed by reference.

    > Does the value of the nam
    > variable declared in printnamefirst get modified because of the 'pass
    > by reference' mechanism?


    No: you didn't pass a string by reference (it is impossible to pass
    strings at all in C); you passed a pointer-to-char. That
    pointer-to-char was passed by value, but it still points to the same
    place in memory. It's not possible to pass entire arrays by value in
    C; the best way to handle this is to pass it through a parameter of
    const char *:

    void considerate_func(const char *foo);
    /* considerate_func() promises that it won't alter any of the
    characters pointed at by foo. */

    void unscrupulous_func(char *foo);
    /* unscrupulous_func() makes no such guarantees... */

    HTH,
    Micah
    Micah Cowan, Jun 28, 2003
    #1
    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:
    383
    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,140
    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:
    372
    Kjell Arne Johansen
    Sep 2, 2003
  4. C++fan
    Replies:
    4
    Views:
    1,700
    Jack Klein
    Jan 7, 2004
  5. Replies:
    5
    Views:
    605
    Matt Wharton
    Dec 9, 2004
Loading...

Share This Page