Re: How is memory allocated

Discussion in 'C Programming' started by Emmanuel Delahaye, Jun 27, 2003.

  1. In 'comp.lang.c', (Samuel Thomas) wrote:

    To answer the question of your subject line, the memory is allocated in a
    implementation-dependent way.

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


    Non standard header.

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


    main returns inbt. Always. Even the old notation (pre-ANSI, aka K&R):

    main()

    implies a implicit return type of int. Note that 'void' is a new concept
    brought with the first release of the standard C (aka ANSI-C89 or ISO-C90).

    > {
    > clrscr();


    Non standard function.

    > 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?


    say a() call b().

    It is safe to define a variable in a() and to pass its address to b().

    void a (void)
    {
    int x;
    b(&x);
    }

    It is unsafe to define a variable in b() and to return its address to a():

    int *b(void)
    {
    int x = 123;

    return &x;
    }

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


    For a string (array of char), the passed value is an address. The parameter
    is a pointer of the correct type. You can use the 'const' qualifier to inform
    the compiler that:

    - The function will not change the original value of the string
    - Constant strings (e.g. string literals) are accepted.

    f (char const *s)
    {
    }

    > values when passed across functions? Does the value of the nam
    > variable declared in printnamefirst get modified because of the 'pass
    > by reference' mechanism?


    No, for the simple reason that there is no pass-by-reference in C.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-library: http://www.dinkumware.com/htm_cl/index.html
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jun 27, 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,146
    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:
    374
    Kjell Arne Johansen
    Sep 2, 2003
  4. C++fan
    Replies:
    4
    Views:
    1,705
    Jack Klein
    Jan 7, 2004
  5. Replies:
    5
    Views:
    608
    Matt Wharton
    Dec 9, 2004
Loading...

Share This Page