Returning contents of int array from function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Returning contents of int array from function

  1. #1
    Join Date
    Jul 2008
    Posts
    36

    Returning contents of int array from function

    Grr, cant seem to get this to work..

    I have a class that has

    The header has
    int* puzzle;

    The constructor has
    puzzle = new int[81];

    I want to have a member that returns the contents of the 81 element array and not a pointer to it.. Basically after the class is created and some other stuff happends, the class is deleted.. Before the class is deleted I need to get the contents of the array out of the class and "copy" it to a local variable so I will have it after the class is deleted..

    How can I do this?

  2. #2
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Returning contents of int array from function

    Make your life ALOT easier...STOP using naked pointers and "raw" arrays. In this case std::vector<int> is your friend.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,125

    Re: Returning contents of int array from function

    vector would make your life easier. The alternative would be to write a function that copies the value from one array into another.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Returning contents of int array from function

    Note that arrays are not copyable, so it's impossible to move them around in any way except (a) passing a pointer or (b) copying one array into another.

    std::vector wraps up these operations in a way that makes it *look* to the user like a copyable array, which is one of many reasons why it makes things easier.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Returning contents of int array from function

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Returning contents of int array from function

    Quote Originally Posted by TheCPUWizard View Post
    Make your life ALOT easier...STOP using naked pointers and "raw" arrays. In this case std::vector<int> is your friend.
    I'm waiting for the ubiquitous "my teacher won't allow me to use vector" comment any moment now...

    Regards.

    Paul McKenzie

  7. #7
    Join Date
    Aug 2007
    Posts
    858

    Re: Returning contents of int array from function

    Quote Originally Posted by Paul McKenzie View Post
    I'm waiting for the ubiquitous "my teacher won't allow me to use vector" comment any moment now...
    A completely off topic question...

    Do you guys [Paul, CPUWiz, etc] think that things like naked pointers and arrays shouldn't be taught, or something? I fully agree that vector and most other things STL make using C++ much easier, lead to less bugs, easier code maintenance and so on, but it still seems to me that one must know these things to be considered a proficient C++ programmer. Even if you never plan to write a single line of your own code using them, you have to know them because you simply can't escape them. Whether it's maintaining legacy code, using 3rd party libraries, or whatever...

    Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful. Because, I mean, if the guy is in a class and he is supposed to be learning about pointers, your comment just seems just slightly rediculous and/or arrogant.

  8. #8
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Returning contents of int array from function

    Speeking just for myself, I agree that a person must know and understand all of the "C"/"C-Style", but my experience as a trainer is that teaching a person how to USE collections, string etc (ie STL) and THEN teaching them how to implement the constructs is MUCH more effective.

    Which did you learn first? How ride a bicycle or how to rebuild a 10 speed shifter?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,125

    Re: Returning contents of int array from function

    Seems to me a lot of the instructors are basically teaching C using a C++ compiler. You can't use C++ without understanding pointers and C style strings, but the language has evolved over the last 20 years. From what we see here, I'd conclude that a lot of instructors are stuck in the past.

    It would make more sense to teach the build in list, vector, set, string, etc classes than to teach how to do it in C at least for beginners. Sure, it's helpful to know how a list or set is implemented, but not right of the bat.

    If posts seem arrogant, it's not directed at the poster. It's directed at the instructors who are still teaching the language as it existed in two decades ago.

  10. #10
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Returning contents of int array from function

    Quote Originally Posted by Speedo View Post
    Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful.
    If I were to consider to intern/novice applicants that were identical in all resepects except #1 knew how to use STL effectively, but was very very poor on pointers and dynnamic memory management, while #2 knew pointers, and robust memory management very well, but was not able to use STL effectively....

    I would hire the first one over the second.

    The reason is simple. I can have a slightly more senior person handle the low level code for a project, but nearly EVERY method in our systems includs STL constructs and concepts.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Returning contents of int array from function

    Quote Originally Posted by Speedo View Post
    A completely off topic question...

    Do you guys [Paul, CPUWiz, etc] think that things like naked pointers and arrays shouldn't be taught, or something? I fully agree that vector and most other things STL make using C++ much easier, lead to less bugs, easier code maintenance and so on, but it still seems to me that one must know these things to be considered a proficient C++ programmer. Even if you never plan to write a single line of your own code using them, you have to know them because you simply can't escape them. Whether it's maintaining legacy code, using 3rd party libraries, or whatever...

    Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful. Because, I mean, if the guy is in a class and he is supposed to be learning about pointers, your comment just seems just slightly rediculous and/or arrogant.
    It's one thing to learn and know how to use pointers, and a completely different thing to overuse them, even when a better approach, like using an STL container, is available and desirable.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Returning contents of int array from function

    Quote Originally Posted by Speedo View Post
    Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful. Because, I mean, if the guy is in a class and he is supposed to be learning about pointers, your comment just seems just slightly rediculous and/or arrogant.
    The author of the C++ language, Bjarne Stroustrup, must also be ridiculous and arrogant.

    http://www.research.att.com/~bs/new_learning.pdf

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Returning contents of int array from function

    Quote Originally Posted by Speedo View Post
    Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers?
    This is a very rare individual. I don't even know if such a person exists. First off, they would have learned the iterator concept, which means they know what "dereferencing" means, which leads them into knowing what pointers are and what they do. In addition, pointers are used for polymorphism (i.e. virtual functions). There is no such thing as virtual functions in C, so the likelihood that an ace C++ programmer who knows STL and at the same time doesn't know pointers is so remote, it shouldn't even be discussed.

    The other way around, where someone knows all the low-level details of pointers and know nothing about STL. Forget it -- he isn't hired by me. I know a lot of 'C' programmers who try to learn STL and can never get it (iterators, containers, algorithms), and all of these are fundamental in being a C++ programmer. This happens far more often than the first scenario you discussed about C++ programmers who know STL and do not know pointers.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 12th, 2008 at 08:23 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center