Like Tree1Likes
  • 1 Post By King Mir

Returning new array

This is a discussion on Returning new array within the C++ Programming forums, part of the General Programming Boards category; Why does compiler complain: warning: address of local variable 'newList' returned I thought we could return values b/c in fact ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    Returning new array

    Why does compiler complain:
    warning: address of local variable 'newList' returned

    I thought we could return values b/c in fact we are returning a copy of it.

    Code:
    int* newList(int* list, int size)
    {
     int newSize = size * 2;
     int newList[] = {};
     
     for ( int i = 0; i < newSize; ++i )
      list[i] = 0;
     
     return newList;
    }

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    a couple things:

    no, you'd return a copy of the integer address of where newList used to be (it's probably just been cleaned off the stack when the function exits).

    you're returning an empty array.

    you're also going to walk off the end of list in your for loop there

    avoid newing in a function without a thoroughly thought-out memory management model.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    I mean:
    Code:
    int* newList(int* list, int size)
    {
     int newSize = size * 2;
     int newList[] = {};
      
     for ( int i = 0; i < newSize; ++i )
      newList[i] = 0;
    
      
     return newList;
    }
    What do you mean I should avoid newing within a function?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    it complains because the returned pointer would point to a freed resource after the function completes. For all intent, the pointer is invalid.
    Last edited by whiteflags; 07-09-2012 at 01:03 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Now you're just invoking undefined behavior. The size of newList isn't newSize.
    Initializing an array with an empty initializer list shouldn't even compile.
    You are better off learning how to use std::vector instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Quote Originally Posted by Elysia View Post
    Initializing an array with an empty initializer list shouldn't even compile.
    It does, but you know, if you do something else wrong, you break everything.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    It doesn't compile for me:
    error C2466: cannot allocate an array of constant size 0
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Make the size greater than zero (duh).

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Ah, well, perhaps you misunderstood my intent.
    Initializing an array without an explicit size and an empty initializer list will compile. This is exactly what the OP did and what I was complaining about.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    I can only read what you take the time to type, you know.

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    Yes you're right that I should learn libraries such as C++'s STL w/ vectors, but as a new programmer I thought I learn the basics and I thought creating a new array would be one of those 'basics'.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    There are different kinds of arrays.
    If you want a static array, learn std::array.
    If you want a dynamic array, learn std::vector.
    There is little need for C-style arrays unless you need to work with C code (or have an outdated compiler).
    Those are basics. You'll save a lot of time with these instead of having to learn how to dynamically manage memory in order to resize your arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,036
    Quote Originally Posted by monkey_c_monkey View Post
    Yes you're right that I should learn libraries such as C++'s STL w/ vectors, but as a new programmer I thought I learn the basics and I thought creating a new array would be one of those 'basics'.
    C arrays aren't the basics. They are the backwards compatibility features.
    Elysia likes this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. Replies: 4
    Last Post: 10-02-2010, 06:33 AM
  3. Returning 2D Array
    By Stlcardinal50 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2009, 02:56 AM
  4. Returning Array
    By Suchy in forum C Programming
    Replies: 4
    Last Post: 03-02-2008, 10:05 PM
  5. Replies: 6
    Last Post: 11-09-2006, 02:28 AM

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