CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Returning vectors from functions

    The first part was correct, with one exception:
    Code:
    for (int i = 0; i <= num_x; i++)
    Vectors are like arrays, they are indexed starting at 0, and going to index size-1.

    Viggy

  2. #17
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Returning vectors from functions

    The first version (not the push_back version) is the one you should use. You should make the vector length num_x + 1 though since your loop runs from 0 to and including num_x + 1.

    Either that or make the loop termination condition i < num_x if your intention was to produce num_x entries.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #18
    Join Date
    Oct 2010
    Posts
    8

    Re: Returning vectors from functions

    The problem I am having is that it outputs a vector that, when num_x = 100 for example, has zeros in the first 100 elements, and then writes 21 ones followed by another 79 zeros. So the vector ends up being 200 elements, even though it is defined as only being 100 elements.

    What the vector should be is 100 elements long with the first 21 elements being 1 and the last 79 being zero.

  4. #19
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Returning vectors from functions

    Quote Originally Posted by hobbes543 View Post
    The problem I am having is that it outputs a vector that, when num_x = 100 for example, has zeros in the first 100 elements, and then writes 21 ones followed by another 79 zeros. So the vector ends up being 200 elements, even though it is defined as only being 100 elements.

    What the vector should be is 100 elements long with the first 21 elements being 1 and the last 79 being zero.
    When you access an out-of-range element in a vector (as others have already indicated you are doing) you get undefined behavior. It may crash or it may appear to run fine and produce bogus results. So you cannot depend on the outcome of your program until you are sure you fixed all of those problems.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #20
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Returning vectors from functions

    Quote Originally Posted by hobbes543 View Post
    The problem I am having is that it outputs a vector that, when num_x = 100 for example, has zeros in the first 100 elements, and then writes 21 ones followed by another 79 zeros. So the vector ends up being 200 elements, even though it is defined as only being 100 elements.

    What the vector should be is 100 elements long with the first 21 elements being 1 and the last 79 being zero.
    S_M_A and MrViggy both told you what you need to do to avoid that problem: Use the version without push_back, and fix the for loop.

    Alternatively, if you keep using push_back, then you should *not* be initially allocating the vector to size 100.

  6. #21
    Join Date
    Oct 2010
    Posts
    8

    Re: Returning vectors from functions

    Thank you all for the help. I got the code debugged and learned alot about how functions and vectors work.

  7. #22
    Join Date
    Nov 2007
    Posts
    35

    Re: Returning vectors from functions

    Afa the passing of arrays out of functions instead of using an STL container you could code or find an autoarray_ptr template. If you look in The C++ Programming Language, auto_ptr was provided to take ownership of pointers and implement "move semantics." Also the destructor called delete on the pointer for you when going out of scope. An autoarray_ptr template would do the same only call delete [] in the destructor.

    The new C++ provides && operator so that you can do move semantics without resorting to auto_ptr. If you look in MSDN docs as example, it shows what constructors and destructors to implement. You don't have to mimick all the stuff that's in auto_ptr. Just the necessary bits. Sometimes it's better to whip something small together rather than bend your code to fit the STL. Especially if you're not using 1/10th of the methods that come with the container.

    It may still be a good idea to look at the auto_ptr source and just think what you'd do differently if you allocated an array of whatever type instead of one instance. What I like about small memory object is once you have them debugged then you don't necessarily have to use exceptions just to have clean exits from functions. With the clean up in the destructor you get away from 4 or 5 line exit clusters in every condition test and get toward something like if(! succeeded()) return false; way more fun than deallocating or doing try catch crap just to have clean up code in one place.
    Last edited by MilesAhead; October 24th, 2010 at 02:12 AM.

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured