|
-
October 20th, 2010, 04:14 PM
#16
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
-
October 20th, 2010, 04:15 PM
#17
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.
-
October 20th, 2010, 04:28 PM
#18
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.
-
October 21st, 2010, 06:55 AM
#19
Re: Returning vectors from functions
 Originally Posted by hobbes543
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
-
October 21st, 2010, 08:09 AM
#20
Re: Returning vectors from functions
 Originally Posted by hobbes543
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.
-
October 21st, 2010, 12:09 PM
#21
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.
-
October 24th, 2010, 02:01 AM
#22
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|