-
November 13th, 2014, 11:50 AM
#1
why is this code crashing?
Code:
#include <iostream>
#include <vector>
#include <fstream>
/* this short code has to do this,
give an array of lenght 50, position (0,1) ,(5,6), (10,11), (15,16) and
so on till (45, 46) but be the final result in cont3.
but on the condition that the values in cont3 must not repeat. if there
should be a repeating value, then it must be substituted by a value on these position
--> 2,7,13,17, and so on till 47.
so this code am trying to write should in theory do just that..it compiles nicely..
but come runtime it crushes...
can anybody tell me why?
thanks!
*/
const int arr [50]={4,34,32,34,56,
76,4,98,3,4,
3,5,3,4,5,
6,7,55,67,65,
67,19,57,12,45,
37,59,67,57,4,
38,33,45,67,87,
98,31,45,65,23,
12,19,32,35,76,
87,45,82,98,78,};
int vll, y = 0, no = 0;
void load_values( const int * pt1, const int * pt2);
void add_value();
void write_to_file (std::vector<int>);
void eliminate_repeat(std::vector<int>,std::vector<int>);
int replacing ();
std:: vector <int> cont1;
std:: vector <int> cont2;
std:: vector <int> cont3;
int repeated_times = 0, curr = 0, repeated_value;
int main()
{
curr = 0; // watch out for this value!!!
load_values(&arr[0], &arr[49]);
eliminate_repeat(cont1,cont2);
write_to_file(cont3);
std::cout<<std::endl<<std::endl;
for(std::vector<int>::iterator ptr4 = cont3.begin(); ptr4 != cont3.end(); ptr4++)
{
std::cout<<*ptr4<<", ";
}
return 0;
}
void load_values ( const int * pt1, const int * pt2)
{
for (int x = 0, y = 1, z = 2; x <= 45, y <= 46, z <= 47; x+=5, y+=5, z+=5)
{
cont1.push_back (arr[x]);
cont1.push_back (arr[y]);
cont2.push_back (arr[z]);
}
}
void eliminate_repeat(std::vector<int>cont1, std::vector<int>cont3)
{
std:: vector<int>::iterator chk1 ;
std::vector<int>::iterator chk2;
int no = 0;
cont3.push_back(cont1[0]);
for ( chk1 = cont1.begin() ; chk1 != cont1.end(); chk1 ++)
{ for (chk2 = cont3.begin() ; chk2 != cont3.end(); chk2 ++)
{
if ( *chk1 == *chk2)
{ replacing();}
else vll = *chk1;
cont3.push_back(vll);
}
}
}
void write_to_file (std::vector<int>)
{
std::fstream file;
file.open("organizeddddd.txt",std::ios::app);
if (file.is_open())
{ file<<"{";
for(std::vector<int>::iterator ptr = cont3.begin(); ptr != cont3.end(); ptr ++)
{
file << "," << *ptr ;
}
file<<"},"<<std::endl;
std::cout<<"writing to file was successful...";
}
else
std::cout<<"Could not open file, corrupt or does not exist in location...";
}
int replacing ( )
{ int bb;
for (std::vector<int>::iterator pt2 = cont2.begin()+curr; pt2 != cont2.end();pt2++, curr ++)
{ for (std::vector<int>::iterator chpt1 = cont1.begin();chpt1 != cont1.end(); chpt1++)
{
if ( *chpt1 == *pt2)
{ no += 1;}
}
if (no >= 1);
else { int value = *pt2 ;
cont3.push_back(value);
goto loop;
}
}
loop:return bb;
}
-
November 13th, 2014, 12:01 PM
#2
Re: why is this code crashing?
Run the program under the debugger -- that is what anyone here would do to solve the problem, so you must learn how to do this. Now would be a good time to learn.
Also, just because a program compiles successfully only means that the code is syntactically correct. It does not guarantee the program will run successfully. That's what debugging is all about.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 13th, 2014 at 12:04 PM.
-
November 13th, 2014, 12:07 PM
#3
Re: why is this code crashing?
yes man! unfortunately i've not been able to successfully learn how to debug with code::blocks..or with any IDE for that matter.I mean i haven't been able to find a step by step instruction on how to do that..all i found was insufficient and i get stuck somewhere...i've read elsewhere how debugging is soo important and is like walking instead of crawling...i will like to learn...do you have a pdf or link to pass me? thanks
-
November 13th, 2014, 12:11 PM
#4
Re: why is this code crashing?
am searching for materials again on debugging..hope to be able to do it soon! thanks
-
November 13th, 2014, 12:15 PM
#5
Re: why is this code crashing?
Small point, but you don't put a trailing comma at the end.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 13th, 2014, 12:16 PM
#6
Re: why is this code crashing?
One thing I see that is clearly wrong is that you're changing a vector while you're using iterators from that vector to loop.
Code:
for (chk2 = cont3.begin() ; chk2 != cont3.end(); chk2 ++)
{
if ( *chk1 == *chk2)
{ replacing();} // trouble
replacing() may push_back a value to cont3, thus invalidating the chk2 iterator that you're using to loop with.
First, get rid of the goto statement in replacing() and use better techniques to leave a function.
Second, I'm sorry to say that your approach will not work. The way that erasure over a container can work safely is to use erase/remove_if().
Your comment as what you're supposed to do is not clear (to me). You have an array of numbers in your code, and none of those numbers are mentioned in your comments. So it is almost impossible to figure out what you're supposed to be doing.
If you clarified what you're supposed to do by showing what the input is and what the final output should be would be helpful.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 13th, 2014 at 12:20 PM.
-
November 13th, 2014, 12:29 PM
#7
Re: why is this code crashing?
i don't get your point, paul mckenzie, std::vector<int>::iterator chk1 and chk2 don't refer to a particular vector...the should be free to point to anyone, no???
and anyway... what the code is seeking to do is this .. {4,34,32,34,56,
76,4,98,3,4,
3,5,3,4,5,
6,7,55,67,65,
67,19,57,12,45,
37,59,67,57,4,
38,33,45,67,87,
98,31,45,65,23,
12,19,32,35,76,
87,45,82,98,78}; this is a single array... but now imagine it as ten arrays..i need to have the first two values of every single array in cont3 when all is said and done..condition being that there must be nothing repeating..if there is we should be taking a value of the third position in every array if they are not already in cont3..so lets say there is a repeation..then i replace the value repeating with 3rd position first array --> 32..but if 32 is already present then i take 3rd value of second array...that is 98...
coming to the go-to...well..i used it as a last resort...over here i think it doing the job well...the problem is not there..i think is elsewhere...because breaking out of the nested loop with just break does not do the job...
-
November 13th, 2014, 12:31 PM
#8
Re: why is this code crashing?
Originally Posted by TheLionKing
am searching for materials again on debugging..hope to be able to do it soon! thanks
Another thing you should do -- start out with 5 or 10 numbers, not 50. That way, bugs can be easily worked out with a smaller set of input values.
Regards,
Paul McKenzie
-
November 13th, 2014, 12:34 PM
#9
Re: why is this code crashing?
Code:
for (chk2 = cont3.begin() ; chk2 != cont3.end(); chk2 ++)
{
if ( *chk1 == *chk2)
{
replacing();
}
else
vll = *chk1;
cont3.push_back(vll);
}
As well as the comment made by Paul in his post #6, push_back() can invalidate all iterators (ie chk2 in this case) if a relocation happens. See http://www.cplusplus.com/reference/v...tor/push_back/
So if a reallocation happens for cont3 for the push_bacK() then the value in chk2 is invalid and shouldn't be used without being re-initialized with a valid value. As you are using push_back() each time round the for loop, this type of code is a no-no.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 13th, 2014, 12:34 PM
#10
Re: why is this code crashing?
Originally Posted by TheLionKing
i don't get your point, paul mckenzie, std::vector<int>::iterator chk1 and chk2 don't refer to a particular vector...
Code:
for (chk2 = cont3.begin(); chk2 != cont3.end(); chk2++)
That one line of code shows that chk2 does refer to the cont3 vector. It is an iterator into that vector. The replacing function messes up cont3 by pushing a value into cont3, thus invalidating the iterator you're using.
This is a case of changing the container while looping over the container, just not as obvious. For the vector container, this is a non starter.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 13th, 2014 at 12:37 PM.
-
November 13th, 2014, 12:38 PM
#11
Re: why is this code crashing?
oh a moment, i just learned something new...so with vector::iterator..pushback destroys the pointer? wow! so i may as well not use a vector::iterator but an int * chk2 no? anyway am gonna read on that soon...
-
November 13th, 2014, 12:38 PM
#12
Re: why is this code crashing?
std::vector<int>::iterator chk1 and chk2 don't refer to a particular vector
Iterators refer to specific memory locations (in this case used by the vector). When a vector re-allocation occurs, the contents of the vector may be moved to different memory locations. So the iterator value (eg memory address) held in chk2 points to memory which is no longer being used by the vector - hence the iterator is now invalid.
After any operation which may invalidate an iterator (see container documentation), any used iterators need to be re-initialized to valid values.
Last edited by 2kaud; November 13th, 2014 at 12:41 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 13th, 2014, 12:44 PM
#13
Re: why is this code crashing?
2kaud! of course with this explanation the code will surely crash!!! and it may crash still too if i use just int * pointer if my vector is moving about in memory...i have to read and go at the solution in another way..thank you guys!!!
-
November 13th, 2014, 12:45 PM
#14
Re: why is this code crashing?
Originally Posted by TheLionKing
there must be nothing repeating..
You need to clarify what is meant by "repeating". You have a pair of numbers -- by "repeating" are you talking about the two numbers in the pair? Or pairs that have the same data? See the confusion?
Your description still isn't clear, and maybe that is why you're having trouble with the program. If the description were clear, I bet there are better and safer solutions using a few algorithm functions that do the job, with just a minimum set of hand-coded loops to write.
I say this, since in this day and age of C++, there should be little reason to write code that basically does "basic things" such as finding, erasing, etc. by writing loops all over the place. You have in mind what you want to do, I get that -- the problem is that your explanation fits the way you've coded it, which is confusing. A less confusing description with sample input and output would definitely render a better, clearer solution.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 13th, 2014 at 12:50 PM.
-
November 13th, 2014, 12:48 PM
#15
Re: why is this code crashing?
my numbers are natural numbers...in cont3 there must be no repeating natural numbers...the pairing is insignificant in cont 3... but the rule is that i have to take the first two numbers of every five count on the bigger array of 50...so immagine it to be ten arrays of five values...i need the first two of everyone of them..then after i have them..i apply the condition...hope i made it clear now...
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
|