program works for small iterations but throws vector subscript out of range during large iterations - c++

here is my code. i am trying to find min distance(diff in indices) b/w 2 equal elements:
#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
vector<string> split_string(string);
int n;
int minimumDistances(vector<int> a) {
int mn=n,flag=0,mch,pos,diff=0;
for (int i = 0;i < n-1;i++) {
//mch = a[i];
for (int j = i + 1;j < n;j++) {
if (a[j]==a[i]) {
flag = 1;
pos = j;
diff = j - i;
}
}
if (diff<mn) {
mn = diff;
}
}
if (flag==0) {
return -1;
}
return mn;
}
int main()
{
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string a_temp_temp;
getline(cin, a_temp_temp);
vector<string> a_temp = split_string(a_temp_temp);
vector<int> a(n);
for (int i = 0; i < n; i++) {
int a_item = stoi(a_temp[i]);
a[i] = a_item;
}
int result = minimumDistances(a);
cout << result;
cin.get();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(),
[](const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i +
1));
return splits;
}
The exception occurs at pt marked by arrow
when n=10 no error
when =1000 exception occurs
Why is this exception only occurring at large values?
sample input :
39572 89524 21749 94613 75569 74800 91713 62107 28574 22617 22271 22624 28116 67573 53717 9358 65220 59894 78686 10945 33641 11708 8851 11860 66780 64697 799 47782 41971 54170 8960 81543 60047 47061 92508 51968 38213 84221 14075 66787 23191 52698 5764 51307 20271 59481 77018 1843 19375 55704 12789 53016 83764 37992 64877 50545 19041 82028 98327 61012 52551 7287 42555 12598 70700 51416 80918 8914 35637 11345 75701 58828 80395 97817 26488 17019 57299 3506 18862 93026 75562 48003 62395 59327 85996 27272 9872 5037 25652 8199 82402 78203 31838 41309 7153 18890 92725 88071 27804 28363 99416 19858 3543 79812 17675 30031 96831 91326 49889 15693 84353 25452 80049 46748 84779 66045 90372 94651 87434 16024 19202 69836 94228 67392 27498 1381 86282 20223 5805 14087 48586 5221 50297 68482 85033 67972 98513 98216 59299 48403 30262 60004 73855 10311 6752 74986 92708 13476 85989 96494 29500 5191 82683 40080 88935 10181 57814 75217 30404 63619 5656 95343 68840 55953 63825 70226 23926 62338 68442 99577 27093 15056 59581 17300 25367 82685 92286 34427 96161 78275 30922 25661 99818 13605 82094 88753 23786 39908 80323 54190 3527 85979 65885 72367 41933 29710 58945 82211 8401 43740 81788 35494 58796 57721 69147 516 40406 77785 34943 36567 72413 65865 78580 72231 95822 60674 77337 35960 582 57660 6503 4109 43639 72388 92829 1924 2099 51774 84135 10500 95514 82275 62346 70663 39996 31493 71179 80402 9279 6122 16969 81692 88340 11902 70275 514 72576 47612 36475 73159 21624 42978 93620 65264 15366 86449 67188 17465 54576 67676 44317 50090 49951 6664 20753 89948 54509 8284 86702 80140 14407 20024 78184 19099 31926 48460 19613 4502 12424 72440 94013 34049 15418 87634 99313 30785 90435 82853 64602 45011 50529 25272 11454 481 48288 48559 6781 2797 56844 93483 82938 87603 13507 61122 6702 45433 25934 42667 66288 38359 15108 60301 72408 46878 64287 88073 94015 54723 87278 74970 99734 37808 242 27540 54641 48530 76100 61422 67679 32944 54905 66969 20547 84765 44444 43601 30198 86730 2620 12838 25089 17728 73140 13849 64607 53779 18274 74974 8502 5553 49944 24589 59713 50186 52129 14354 15068 28229 75776 99100 77525 47033 66069 98072 31798 26865 41673 78349 13596 44294 7539 55037 78374 80679 68887 59333 50811 3513 34308 59313 9066 84252 254 85131 50791 52384 99485 82211 96965 91613 81311 74491 38647 63733 88915 86797 90598 46941 65146 20546 7587 72686 75584 85961 69717 60823 61647 36880 64336 12307 96194 89755 12911 12800 74886 63702 65184 90724 45914 78502 82337 43577 52993 20984 7310 41908 24134 97909 88849 5632 18455 96436 94670 10391 98750 80740 71214 60397 17620 51903 89056 30166 41658 1967 42967 32896 65670 8151 23620 27936 86653 5958 71513 55998 43294 78824 14259 67428 93085 19460 73061 27892 15897 84083 54636 30999 64823 25850 7748 82444 77753 96804 12610 35763 98771 55577 68660 80793 80081 92280 8729 83086 14590 96595 55437 57885 91771 69696 41665 1208 89156 31078 29100 21405 15162 83736 52404 79985 25939 60152 78781 3692 73308 7744 39456 72080 63321 24468 69225 59754 33100 94307 42841 47691 90902 98278 21928 82673 84326 63593 83881 73482 11024 29333 94888 42538 29422 63644 38875 71713 40149 17657 75405 13457 25401 31213 1889 5074 72033 71115 64829 5134 65422 24022 52825 72676 22300 91105 71701 6626 54698 55582 96460 82074 1267 7700 24612 47041 71345 63488 18754 11494 97497 10512 41303 39250 58077 59545 44324 30111 47012 25505 35245 28786 49527 4422 1462 88179 95527 73163 11157 66577 45097 7618 48652 62716 31670 89616 9758 3015 69456 28512 30861 83305 39024 88517 22555 97102 48062 83232 43565 95074 8737 78810 23860 74617 99584 25322 62796 95111 14837 90306 61688 59934 14276 26692 22650 45946 32661 48760 48962 2117 77273 96175 1775 32649 84692 24330 46103 32754 23914 89668 44180 49004 84830 84392 23621 84414 9714 2769 95877 40903 93075 73918 17189 7351 16962 56192 53298 49623 4952 18612 68093 98577 31139 69868 47579 15832 10550 93682 64938 34465 99703 25471 83469 885 9863 23442 1652 35930 26211 97529 76833 35639 87799 94023 59342 4762 50215 28992 54385 71519 47604 38830 70097 78744 8698 17676 10928 19249 27710 92218 70066 27413 17689 53535 44651 27553 76977 46303 63483 19540 43832 56668 55179 31632 50691 30874 52746 17258 59866 7131 5130 23823 45962 75227 18919 71012 9255 29847 6613 53317 38417 76679 97083 56107 46566 41734 83660 23543 88037 63495 59436 48221 20163 30967 96205 87207 61841 48951 20817 38060 56083 25947 61883 18397 17526 80802 5761 26781 27001 12375 96451 81770 89054 93534 37877 35621 35268 37889 75516 23305 1384 34952 71526 21548 82272 67732 25107 44113 16683 45924 98525 89118 71872 60408 23867 5750 57562 29629 48884 915 42004 45335 82686 47410 38869 36915 83031 74137 74805 74900 97442 76189 26204 85320 14089 8476 69404 55548 68942 2440 1473 67467 7910 89697 44228 31778 95447 1790 77759 44331 19058 36115 89666 18096 83525 44887 55011 82909 19024 29816 57809 32818 22358 365 34491 52799 8842 3895 8348 94136 22687 26173 61603 30598 15870 5831 78728 27669 23974 56487 88353 59384 92602 94371 77480 92479 39259 32491 91740 58283 78660 65901 91102 1018 66267 25593 70169 91461 45840 78517 85597 68528 4690 47200 99126 36912 69384 94206 64582 93358 50693 52935 52742 59647 47306 46574 68478 86565 95417 60219 44849 74077 26120 52303 91447 8739 94248 61617 200 40088 40134 85797 8616 61177 32998 24094 98089 18734 34652 62671 12092 1697 31958 81186 61344 79265 44112 46175 82182 39529 22746 43383 13607 65218 95686 5054 73958 6286 66671 74158 46375 23158 76308 71343 84335 25658 11790 82424 44392 46442 61448 72836 48140 93406 70374 25836 89023 14486 88363 71206 54015 11109 14589 83974 76328 26628 89029 66638 32914 72052 57148 95641 11562 33456 66985 95897 59114 95127 94674 19858 57921 56122 92694 6061 65880 79420 48250 54904 93906 36613 26110 64274 64075 57051 48248 40403 31 37277 7041 49298 25682 64189 44939 37244 13998 28276 49494 89464 23403 44168 9323 97677 16642 18369 3738 82522 97790 68340

Related

DEVC++ is showing a duplicate code in the output window, while other C++ give me the right ouput. What am I doing wrong? (Random Number Generator)

As default, I use DevC++ to write programs for my computer science class. I am really new to all of this. For this program, we are told to use a random number generator to display values using loops. We used for, while, and do..while loops. I was able to do the for loop (Set1) and while loop (Set2) with no struggle. Once I got to do.. while loop (Set3), and finished it, the output window shows a repeat of Set 2, which is the while loop. I ran it through onlinegdb and it gave me the right output. Is it something with DevC++ or is there something wrong in my code that I haven't been able to see?
The coding for my program:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//declare constant doubles for the program
#define NUM_ONE 30
#define MAX_NUM_TWO 75
#define MAX_NUM_THREE 155
#define MIN_RAND_DOUBLE_VAL 0.0
#define MAX_RAND_DOUBLE_VAL 100.0
#define VAL_DISPLAY 7
int main()
{
//input the seed value of 12 into the random number generator
srand(12);
//declare integer values
int random_num;
int cnt;
int per_line;
per_line = 0;
//start the random number generator for the first set
cout << "Set 1 -- " << NUM_ONE << " values" << endl;
for (cnt = 1; cnt <= NUM_ONE; cnt++) {
random_num = rand();
cout << setw(13) << random_num;
per_line++;
if (per_line % VAL_DISPLAY == 0) {
cout << endl;
}
}
cout << endl
<< endl;
//form the formula to start the random number generator for the second set (1-75)
int random_num2;
random_num2 = rand() % MAX_NUM_TWO + 1;
//start the random number generator for the second set
cout << "Set 2 -- " << random_num2 << " values" << endl;
int cnt2;
int per_line2;
cnt2 = 1;
per_line2 = 0;
while (cnt2 <= random_num2) {
random_num = rand();
cnt2++;
cout << setw(13) << random_num;
per_line2++;
if (per_line2 % VAL_DISPLAY == 0) {
cout << endl;
}
}
cout << endl
<< endl;
//start the random number generator for the third set
int random_num3 = rand() % MAX_NUM_THREE + 1;
cout << "Set 3 -- " << random_num3 << " values" << endl;
double double_random_num;
int cnt3;
int per_line3;
cnt3 = 1;
per_line3 = 0;
do {
double_random_num = MIN_RAND_DOUBLE_VAL + (rand() / (RAND_MAX / (MAX_RAND_DOUBLE_VAL MIN_RAND_DOUBLE_VAL)));
cout << fixed << setprecision(5) << setw(13) << double_random_num;
cnt3++;
per_line3++;
if (per_line3 % VAL_DISPLAY == 0) {
cout << endl;
}
}
while (cnt3 <= random_num3);
cout << endl;
return 0;
}
On DevC++ the output is supposed to look like this:
Set 1 -- 30 values
77 5628 6232 29052 1558 26150 12947
29926 11981 22371 4078 28629 4665 2229
24699 27370 3081 18012 24965 2064 8285
21054 5225 11777 29853 2956 22439 3341
31337 14755
Set 2 -- 65 values
24855 4173 32304 292 5344 15512 12952
1868 10888 19581 13463 32652 3409 28353
26151 14598 12455 26295 25763 26040 8285
27502 15148 4945 26170 1833 5196 9794
26804 2831 11993 2839 9979 27428 6684
4616 30265 5752 32051 10443 9240 8095
28084 26285 8838 18784 6547 7905 8373
19377 18502 27928 13669 25828 30502 28754
32357 2843 5401 10227 22871 20993 8558
10009 6581
Set 3 -- 87 values
39.08811 14.20026 75.05417 65.71551 28.70876 20.87466 92.68166
7.11081 0.00916 85.52507 67.95251 58.98312 55.28123 55.23850
47.74316 66.31062 52.77261 25.62334 84.13038 6.10981 11.68859
38.34346 4.33363 90.12421 66.59139 30.37812 25.38835 33.32011
24.34767 75.70421 91.63793 53.89264 26.74642 9.94293 63.23130
20.26124 71.88940 78.69503 33.71685 10.81576 97.50053 0.06714
4.85549 1.96539 79.29014 82.14972 96.79250 50.13276 47.45933
85.93097 21.68950 83.30638 74.52010 74.41633 98.99594 67.82434
37.49199 38.45637 40.39125 65.93829 7.67846 39.96399 82.64412
49.83978 71.09287 63.16111 96.37745 87.76513 32.64565 14.43525
48.99747 67.77551 7.29698 61.47343 49.82147 74.88327 51.20396
52.24464 55.53758 87.09067 36.05152 4.54726 64.19263 6.03656
But it is giving me:
Set 1 -- 30 values
77 5628 6232 29052 1558 26150 12947
29926 11981 22371 4078 28629 4665 2229
24699 27370 3081 18012 24965 2064 26890
21054 5225 11777 29853 2956 22439 3341
31337 14755
Set 2 -- 65 values
24855 4173 32304 292 5344 15512 12952
1868 10888 19581 13463 32652 3409 28353
26151 14598 12455 26295 25763 26040 8285
27502 15148 4945 26170 1833 5196 9794
26804 2831 11993 2839 9979 27428 6684
4616 30265 5752 32051 10443 9240 8095 Set 2 -- 65 values
24855 4173 32304 292 5344 15512 12952
1868 10888 19581 13463 32652 3409 28353
26151 14598 12455 26295 25763 26040 8285
27502 15148 4945 26170 1833 5196 9794
26804 2831 11993 2839 9979 27428 6684
4616 30265 5752 32051 10443 9240 8095
28084 26285 8838 18784 6547 7905 8373
19377 18502 27928 13669 25828 30502 28754
32357 2843 5401 10227 22871 20993 8558
10009 6581
Set 3 -- 87 values
39.08811 14.20026 75.05417 65.71551 28.70876 20.87466 92.68166
7.11081 0.00916 85.52507 67.95251 58.98312 55.28123 55.23850
47.74316 66.31062 52.77261 25.62334 84.13038 6.10981 11.68859
38.34346 4.33363 90.12421 66.59139 30.37812 25.38835 33.32011
24.34767 75.70421 91.63793 53.89264 26.74642 9.94293 63.23130
20.26124 71.88940 78.69503 33.71685 10.81576 97.50053 0.06714
4.85549 1.96539 79.29014 82.14972 96.79250 50.13276 47.45933
85.93097 21.68950 83.30638 74.52010 74.41633 98.99594 67.82434
37.49199 38.45637 40.39125 65.93829 7.67846 39.96399 82.64412
49.83978 71.09287 63.16111 96.37745 87.76513 32.64565 14.43525

ifstream doesn't get data from file

I've started learning C++, and got a bit of a problem with homework.
The issue is: ifstream opens a file, but for some reason can't extract any data from it. The .txt file is in the same directory as main.cpp.
I'm 100% sure the compiler opens the file, because I don't receive an error.
As you can see below, I've made 2 structs, Person (which contains 2 strings: Surname, Firstname, and optional Middlename), and PhoneNumber (which contains: int Countrynumber, int CityNumber, string phonenumber, and optional additional number).
The whole code is pretty massive, so I don't think I should paste it here.
class PhoneBook
{
private:
std::vector<std::pair<Person, PhoneNumber>> contact;
public:
PhoneBook(std::ifstream& ifs)
{
std::string sN, fN;
std::optional<std::string> mN;
int countryNum, cityNum;
std::string phoneNum;
std::optional<int> addNum;
std::optional<std::string> aN;
if (ifs.is_open())
{
while (ifs >> sN >> fN >> mN.emplace() >> countryNum >> cityNum >> phoneNum >> addNum.emplace())
{
if (mN.has_value())
if (mN.value() == "-")
{
mN = std::nullopt;
}
Person p(sN, fN, mN);
if (addNum.has_value())
{
if (aN.value() == "-")
{
addNum = std::nullopt;
}
else
{
addNum.emplace() = std::stoi(aN.value());
}
}
PhoneNumber ph1(countryNum, cityNum, phoneNum, addNum);
contact.push_back(std::make_pair(p, ph1));
}
}
else
{
std::cout << "Error, couldn't open the file." << std::endl;
}
ifs.close();
}
};
And here is the main() function:
int main()
{
std::ifstream file("PhoneBook.txt");
PhoneBook book(file);
std::cout << book;
}
By the way, I've overloaded operator<< for the class PhoneBook and it works, so that's not the issue.
And here is the .txt file:
Ilin Petr Artemovich 7 17 4559767 -
Zaitsev Zakhar Artemovich 125 44 4164751 -
Dubinin Aleksei Mikhailovich 7 473 7449054 -
Solovev Artur Mikhailovich 4 940 2556793 -
Gerasimov Miroslav Stanislavovich 7 367 7508887 -
Makeev Marat - 77 4521 8880876 999
Solovev Ivan Vladimirovich 7 273 5699819 5543
Egorov Savelii Stanislavovich 77 4521 8880876 99
Sokolov Arsenii - 93 163 1992257 16
Davydov Filipp Grigorevich 7 247 1377660 -
Morozov Vladimir Mikhailovich 37 2290 5613649 -
Orekhov Matvei Petrovich 81 8281 7420182 2
Titova Natalia - 93 163 1992257 9
Markelov Dmitrii Vadimovich 19 7576 5734416 2
Kozlovskii Artem Daniilovich 81 8281 7420182 1
Kuznetsov Kirill Kirillovich 7 017 8346563 -
Mironova Margarita Aleksandrovna 7 273 5699819 5542
Kotov Vasilii Eliseevich 7 367 7508888 -
Ivanov Daniil Maksimovich 7 366 7508887 -
Aleksandrov Georgii - 493 7637 6114861 -

Why MFC dbgheap.c 1044 line runtime error occurs

An error sometimes occurs during program running.
It doesn't happen every time.
I don't know why I get an error in this part.
The length of strAckData is greater than 13
x_FWCommunity->sRevDataStr = strAckData.GetAt(13); '
User SourceCode
void CMLT2App::AckTotalFusingMsg(CString strAckData)
{
char szLog[512];
CString strCmd(""), strResult(""), strTemp(""), strLen("");
char cResult;
int nCmd = 0, nPos = 0, nDataLen = 0;
strTemp = strAckData;
nPos = strTemp.Find(0x02);
strAckData = strTemp.Mid(nPos, strAckData.GetLength() - nPos);
if (strAckData.GetAt(0) != 0x02) return; // Check STX
if (strAckData.GetAt((strAckData.GetLength() - 1)) != 0x03) // Check ETX
return;
strCmd.Format("%s", strAckData.Mid(7, 2)); // Get Command;
sscanf(strCmd, "%X", &nCmd); // String Convert to Hex
cResult = strAckData.GetAt(13); // String Convert to Decimal
x_FWCommunity->sRevCmdStr = strCmd;
strLen.Format("%s", strAckData.Mid(9, 4));
sscanf(strLen, "%X", &nDataLen);
// strCmd 는 0xBD
if (strCmd == "FE" || strCmd == "5E")
{
x_FWCommunity->sRevDataStr = strAckData.Mid(13, nDataLen);
// AfxMessageBox(x_FWCommunity->sRevDataStr);
}
else
x_FWCommunity->sRevDataStr = strAckData.GetAt(13); ← Error Point
switch (nCmd)
{
case CMD_CTRL_FUSING_SYSTEM:
case CMD_CTRL_FUSING_CONTROL_PTN:
case CMD_CTRL_FUSING_PREDISP_PTN:
Call Stack Log
MSVCRTD! 102136a8()
MSVCRTD! 102135b1()
operator delete(void * 0x01a11f28) line 351 + 12 bytes
CString::FreeData() line 146 + 15 bytes
CString::Release() line 157
CString::AllocBeforeWrite(int 1) line 200
CString::AssignCopy(int 1, const char * 0x01e2e684) line 315
CString::operator=(char 48) line 88
CMLT2App::AckTotalFusingMsg(CString {"A2A100BD002300375000011320373000000630331000000C3"}) line 6029
CMLT2App::RS232C_ReceivedFusingInfo(unsigned char * 0x01e2eb8c) line 5412
ThreadWatchComm1(CPortController * 0x01514010) line 966
KERNEL32! 7c7db729()

Exception 3 thrown during clear() or when destroying vectors

Description
Sometimes I get exception 3 when trying to clear() a std::vector.
Hardware
Hardware: ESP-07S
ESP8266-Arduino library core version: 2.4.0-rc1
Settings in IDE
Module: Generic ESP8266 Module
Flash Size: 4MB
CPU Frequency: 160Mhz
Flash Mode: qio
Flash Frequency: 40Mhz
Upload Using: OTA / SERIAL
Reset Method: ck
Stacktrace
This is stacktrace from Arduino's Exception Decoder:
Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
Decoding 30 results
0x401000b7: umm_disconnect_from_free_list at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266\umm_malloc/umm_malloc.c line 1151
0x4021e9bc: memmove at /Users/igrokhotkov/e/newlib-xtensa/xtensa-lx106-elf/newlib/libc/string/../../../.././newlib/libc/string/memmove.c line 69 (discriminator 1)
0x40217464: Print::write(unsigned char const*, unsigned int) at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/Print.cpp line 38
0x4010020c: _umm_free at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266\umm_malloc/umm_malloc.c line 1287
0x4010068c: free at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266\umm_malloc/umm_malloc.c line 1733
0x40215b04: operator delete(void*) at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/abi.cpp line 57
0x40207940: std::_Vector_base >::_M_deallocate(double*, unsigned int) at C:\Users\Defozo\Documents\Arduino\jeszcze nowsza wersja laser_na_stacje_v2\laser_na_stacje_v2/laser_na_stacje_v2.ino line 565
0x402088ac: std::_Vector_base >::~_Vector_base() at c:\users\defozo\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/stl_vector.h line 161
0x40209748: std::vector >::_M_erase_at_end(double*) at c:\users\defozo\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/stl_vector.h line 1353
: (inlined by) std::vector >::clear() at c:\users\defozo\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/stl_vector.h line 1126
: (inlined by) loop at C:\Users\Defozo\Documents\Arduino\jeszcze nowsza wersja laser_na_stacje_v2\laser_na_stacje_v2/laser_na_stacje_v2.ino line 412
0x40217082: std::default_delete ::operator()(TransportTraits*) const at c:\users\defozo\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/unique_ptr.h line 68
0x4010020c: _umm_free at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266\umm_malloc/umm_malloc.c line 1287
0x401020ea: wDev_ProcessFiq at ?? line ?
0x40215d84: loop_wrapper at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/core_esp8266_main.cpp line 124
0x4010070c: cont_norm at C:\Users\Defozo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/cont.S line 109
This is reset cause:
Hardware Watchdog
Fatal exception:4 flag:1 (WDT) epc1:0x40000f83 epc2:0x00000000 epc3:0x4000dd3b excvaddr:0x40023c5c depc:0x00000000
Code
I'm initializing vectors like that:
std::vector<double> dhtTempVector, dhtHumidityVector;
std::vector<double> dhtTempMedianVector, dhtHumidityMedianVector;
std::vector<double> bmpTempVector, bmpPressureVector;
std::vector<double> bmpTempMedianVector, bmpPressureMedianVector;
dhtTempVector.reserve(5);
dhtHumidityVector.reserve(5);
bmpTempVector.reserve(5);
bmpPressureVector.reserve(5);
dhtTempMedianVector.reserve(6);
dhtHumidityMedianVector.reserve(6);
bmpPressureMedianVector.reserve(6);
bmpTempMedianVector.reserve(6);
Inserting new values like that:
if (!isnan(temp) && !isnan(hum)) {
dhtTempVector.push_back(temp);
dhtHumidityVector.push_back(hum);
}
and:
if (!dhtTempVector.empty()) {
dhtTempMedianVector.push_back(median(dhtTempVector));
dhtTempVector.clear();
}
if (!dhtHumidityVector.empty()) {
dhtHumidityMedianVector.push_back(median(dhtHumidityVector));
dhtHumidityVector.clear(); //line number 412
}
This is the median function:
double median(std::vector<double> v)
{
size_t n = v.size() / 2;
std::nth_element(v.begin(), v.begin()+n, v.end());
double vn = v[n];
if(v.size()%2 == 1) {
return vn;
} else {
std::nth_element(v.begin(), v.begin()+n-1, v.end());
return 0.5*(vn+v[n-1]);
}
}
Additional debug information
This is a result:
dhtTempVector size: 4
dhtTempMedianVector size: 5
dhtHumidityVector size: 4
dhtHumidityMedianVector size: 5
29.00 29.00 29.00 29.00 --
28.70 28.75 28.80 28.90 28.90 --
41.20 41.30 41.80 41.40 --
42.65 42.55 41.80 42.30 41.75 --
of these lines of code:
Serial.print("dhtTempVector size: ");
Serial.println(dhtTempVector.size());
Serial.print("dhtTempMedianVector size: ");
Serial.println(dhtTempMedianVector.size());
Serial.print("dhtHumidityVector size: ");
Serial.println(dhtHumidityVector.size());
Serial.print("dhtHumidityMedianVector size: ");
Serial.println(dhtHumidityMedianVector.size());
for (int k = 0; k < dhtTempVector.size(); ++k) {
Serial.print(dhtTempVector.at(k));
Serial.print(" ");
}
Serial.println("--");
for (int k = 0; k < dhtTempMedianVector.size(); ++k) {
Serial.print(dhtTempMedianVector.at(k));
Serial.print(" ");
}
Serial.println("--");
for (int k = 0; k < dhtHumidityVector.size(); ++k) {
Serial.print(dhtHumidityVector.at(k));
Serial.print(" ");
}
Serial.println("--");
for (int k = 0; k < dhtHumidityMedianVector.size(); ++k) {
Serial.print(dhtHumidityMedianVector.at(k));
Serial.print(" ");
}
Serial.println("--");
just before the exception is thrown.
Summary
It looks like the exception is thrown during clearing the vector. I think I experienced exceptions thrown at the end of the loop() function too - so at the time when destructors of the vectors are fired. I'd think it's a problem with destroying the elements of the vectors but they're just double numbers.
What could be the problem?
#Edit
The Arduino-ESP8266 library that I'm using is available here:
https://github.com/esp8266
Here is umm_malloc.c:
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/umm_malloc/umm_malloc.c
Print.cpp:
https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/cores/esp8266/Print.cpp
cont.S:
https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/cores/esp8266/cont.S
Specific lines from umm_malloc.c:
1151:
UMM_NFREE(UMM_PFREE(c)) = UMM_NFREE(c);
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/umm_malloc/umm_malloc.c#L1151
1287:
if( UMM_NBLOCK(UMM_PBLOCK(c)) & UMM_FREELIST_MASK ) {
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/umm_malloc/umm_malloc.c#L1287
1733:
void umm_free( void *ptr ) {
ptr = GET_UNPOISONED(ptr);
/* check poison of each blocks, if poisoning is enabled */
if (!CHECK_POISON_ALL_BLOCKS()) {
return;
}
/* check full integrity of the heap, if this check is enabled */
if (!INTEGRITY_CHECK()) {
return;
}
_umm_free( ptr );
} //1733
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/umm_malloc/umm_malloc.c#L1733
Specific line from Print.cpp:
size_t Print::write(const uint8_t *buffer, size_t size) {
size_t n = 0;
while(size--) {
n += write(*buffer++); //line number 38
}
return n;
}
https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/cores/esp8266/Print.cpp#L38
Specific line from core_esp8266_main.cpp:
static void loop_wrapper() {
static bool setup_done = false;
preloop_update_frequency();
if(!setup_done) {
setup();
#ifdef DEBUG_ESP_PORT
DEBUG_ESP_PORT.setDebugOutput(true);
#endif
setup_done = true;
}
loop();
run_scheduled_functions(); //line number 124
esp_schedule();
}
Line number 109 from cont.S:
https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/cores/esp8266/cont.S#L109

c++ code for linear regression with cost function and gradiant descent

here is my work
#include<iostream.h>
using namespace std;
void gradiantFunction(x,y,th0,th1,a)
{
double temp0,temp1;
for (int i = 0; i < m.size(); i++)
{
h=th0+th1*x[i];
temp0=temp0+(h-y[i]);
temp1=temp1+(h-y[i])*x[i];
/* delta = y[i]-hypothesis(x[i]);
theta[1] += alpha*delta*x[i];
theta[0] += alpha*delta*1;*/
}
temp0=temp0*a/m;
temp1=temp1*a/m;
}
void CostFunction(x,y,th0,th1,a)
{
double temp2=0;
for (int i = 0; i < m.size(); i++)
{
h=th0+th1*x[i];
temp2=temp2+(h-y[i]);
}
double j=temp2/(2*m);
}
void main()
{
double a=0.01, th0=0.1, th1=0.2, temp0,temp1, jHistory[];
int m=1500;
for(int i=1; i<=m;i++)
{
th0=th0-temp0;
th1=th1-temp1;
double j= CostFunction()
double jHistory[i]=j;
}
i want the code to learn these values of thetas
- 6.1101 17.592
- 5.5277 9.1302
- 8.5186 13.662
- 7.0032 11.854
- 5.8598 6.8233
- 8.3829 11.886
- 7.4764 4.3483
- 8.5781 12
- 6.4862 6.5987
- 5.0546 3.8166
- 5.7107 3.2522
- 14.164 15.505
- 5.734 3.1551
- 8.4084 7.2258
- 5.6407 0.71618
- 5.3794 3.5129
- 6.3654 5.3048
- 5.1301 0.56077
- 6.4296 3.6518
- 7.0708 5.3893
- 6.1891 3.1386
- 20.27 21.767
- 5.4901 4.263
- 6.3261 5.1875
- 5.5649 3.0825
- 18.945 22.638
- 12.828 13.501
10.957 7.0467
13.176 14.692
22.203 24.147
5.2524 -1.22
6.5894 5.9966
9.2482 12.134
5.8918 1.8495
8.2111 6.5426
7.9334 4.5623
8.0959 4.1164
5.6063 3.3928
12.836 10.117
6.3534 5.4974
5.4069 0.55657
6.8825 3.9115
11.708 5.3854
5.7737 2.4406
7.8247 6.7318
7.0931 1.0463
5.0702 5.1337
5.8014 1.844
11.7 8.0043
5.5416 1.0179
7.5402 6.7504
5.3077 1.8396
7.4239 4.2885
7.6031 4.9981
6.3328 1.4233
6.3589 -1.4211
6.2742 2.4756
5.6397 4.6042
9.3102 3.9624
9.4536 5.4141
8.8254 5.1694
5.1793 -0.74279
21.279 17.929
14.908 12.054
18.959 17.054
7.2182 4.8852
8.2951 5.7442
10.236 7.7754
5.4994 1.0173
20.341 20.992
10.136 6.6799
7.3345 4.0259
6.0062 1.2784
7.2259 3.3411
5.0269 -2.6807
6.5479 0.29678
7.5386 3.8845
5.0365 5.7014
10.274 6.7526
5.1077 2.0576
5.7292 0.47953
5.1884 0.20421
6.3557 0.67861
9.7687 7.5435
6.5159 5.3436
8.5172 4.2415
9.1802 6.7981
6.002 0.92695
5.5204 0.152
5.0594 2.8214
5.7077 1.8451
7.6366 4.2959
5.8707 7.2029
5.3054 1.9869
8.2934 0.14454
13.394 9.0551
5.4369 0.61705