PLEASE HELP - Fundamental C language question

Discussion in 'C Programming' started by cpptutor2000@yahoo.com, Oct 30, 2005.

  1. Guest

    Could some C guru please help me? Suppose I have a code snippet as:
    int* doit(){
    int intArray[25];
    int *ip;
    ip = intArray;
    return ip;
    }

    Since the array intArray is allocated on the stack and so destroyed
    when function returns, is it safe to say that at compile time it is
    not known what ip points to?
    Thanks in advance for your help.
     
    , Oct 30, 2005
    #1
    1. Advertising

  2. Chad Guest

    wrote:
    > Could some C guru please help me? Suppose I have a code snippet as:
    > int* doit(){
    > int intArray[25];
    > int *ip;
    > ip = intArray;
    > return ip;
    > }
    >
    > Since the array intArray is allocated on the stack and so destroyed
    > when function returns, is it safe to say that at compile time it is
    > not known what ip points to?
    > Thanks in advance for your help.


    If I remember right, it would also be safe to say that you won't know
    what doit() points to. BTW, it is better to write int* doit() as int
    *doit(void);

    Chad
     
    Chad, Oct 30, 2005
    #2
    1. Advertising

  3. said:

    > Could some C guru please help me? Suppose I have a code snippet as:
    > int* doit(){
    > int intArray[25];
    > int *ip;
    > ip = intArray;
    > return ip;
    > }
    >
    > Since the array intArray is allocated on the stack


    More precisely, the array intArray is an automatic object.

    > and so destroyed
    > when function returns, is it safe to say that at compile time it is
    > not known what ip points to?


    At compile time, it doesn't point anywhere, because at compile time the
    program isn't running!

    When the function returns, the returned pointer is of no use, as you rightly
    surmise, because the object to which it points (the first int in the array)
    no longer exists. (Nor do the other ints in the array, of course!)

    Solutions:

    a) pass the address of the first array element as an argument;
    b) create the space for the array dynamically;
    c) make intArray static.

    Of these, c) is the worst choice, in my opinion; b) is better, but you ought
    to free the allocated memory when you're finished with it; a) is probably
    the simplest choice.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Oct 30, 2005
    #3
  4. Jordan Abel Guest

    On 2005-10-30, Chad <> wrote:
    >
    > wrote:
    >> Could some C guru please help me? Suppose I have a code snippet
    >> as:
    >> int* doit(){
    >> int intArray[25];
    >> int *ip;
    >> ip = intArray;
    >> return ip;
    >> }
    >>
    >> Since the array intArray is allocated on the stack and so
    >> destroyed when function returns, is it safe to say that at
    >> compile time it is not known what ip points to?
    >> Thanks in advance for your help.

    >
    > If I remember right, it would also be safe to say that you won't
    > know what doit() points to. BTW, it is better to write int* doit()
    > as int *doit(void);
    >
    > Chad


    It doesn't matter when he's defining a function like that. int
    *doit(void) is better when declaring a prototype because int *doit()
    means not to check arguments at all
     
    Jordan Abel, Oct 30, 2005
    #4
  5. Malcolm Guest

    <> wrote
    > Could some C guru please help me? Suppose I have a code snippet as:
    > int* doit(){
    > int intArray[25];
    > int *ip;
    > ip = intArray;

    /* at this point we can use ip eg *ip = 123 will set intArray[0] to 123 */
    > return ip;
    > }
    >


    intArray is an "automatic variable". Usually but not always this means it is
    put on a stack. Some small embedded systems use a different system.

    Yu cannot use the return address of this function at all. Probably if you do
    you will corrupt the first variable of any subroutine called next, but you
    could cause a crash or a system defined error, or anything ("undefined
    behaviour". Even loading the return address into a variable

    /* illegal */
    int *ptr - doit();

    can cause a crash. Some very advanced systems detect that the pointer is
    pointing to memory the program doesn't own, and terminate with an error
    message.
     
    Malcolm, Oct 30, 2005
    #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. Praveen
    Replies:
    3
    Views:
    591
    Praveen
    Apr 24, 2005
  2. Gary

    FUNDAMENTAL QUESTION 1:

    Gary, Nov 29, 2003, in forum: Java
    Replies:
    3
    Views:
    498
    pete kirkham
    Nov 29, 2003
  3. Gary

    FUNDAMENTAL QUESTION 2:

    Gary, Nov 29, 2003, in forum: Java
    Replies:
    3
    Views:
    434
    Bjorn Abelli
    Nov 29, 2003
  4. Casper Bang
    Replies:
    51
    Views:
    1,249
  5. pras.vaidya

    Core java fundamental help

    pras.vaidya, Aug 31, 2006, in forum: Java
    Replies:
    5
    Views:
    2,161
    krishna
    Aug 31, 2006
Loading...

Share This Page