Process finished with exit code 143 (interrupted by signal 15: SIGTERM) error message

Hi! I wrote a program using dynamic arrays and got the error message "Process finished with exit code 143 (interrupted by signal 15: SIGTERM)".

Can someone help me understand how to change my code, so that error message does not come up?

#include <iostream>
#include <vector>
using namespace std;

void readInput(int *&dyArr, int &logicalSize); //function that reads positive integers as input from the user and
//prints all the line numbers in the sequence entered by the user that contain a number num selected by the user

int* expandArr(int *dyArr, int &physicalSize); //function that expands the physical size of a dynamic array

void searchNum(int *dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize); //function that searches an array for a number selected by the user

int* printArray(int *dyArr, int arrSize); //function to print an array given its' size and elements

int main_arr();
int main_vector();

int main(){

main_arr();

return 0;
}
int main_arr() {

int* dyArr = NULL;
int logicalSize = 0;
int num;
int * searchNumArr = nullptr;
int searchNumArrSize = 0;

cout <<"Please enter a sequence of positive integers, each in a separate line." << endl;
cout << "End your input by typing -1." << endl;
readInput(dyArr, logicalSize);

cout << "Please enter a number you want to search." << endl;
cin >> num;

searchNum(dyArr, logicalSize, num, searchNumArr, searchNumArrSize ); //need to make sure I call this function properly
if (searchNumArrSize == 0){
cout << "The number " << num << " does not show at all in the sequence.";
}
else{
cout << num << " shows in lines ";
printArray(searchNumArr, searchNumArrSize);
}

// delete [] searchNumArr;
// searchNumArr = nullptr;
return 0;
}

int main_vector(){
// vector<int> numberVector(int &len) ----numberVector does NOT need the length input;
//vector<int> .... whatever functions I decide to use for this case;

return 0;

}


void readInput(int *&dyArr, int &logicalSize){
int physicalSize = 1;
dyArr = new int[physicalSize];
int posInt;
int counter; //added two lines here
counter = 0;

bool seenEndOfInput = false;
while(seenEndOfInput == false){
cin >> posInt;
if (posInt == -1){
seenEndOfInput = true;
}
else {
if(logicalSize == physicalSize){
expandArr(dyArr, physicalSize);
}
dyArr[counter] = posInt;
// logicalSize++;
counter++; //added this line
}
}
logicalSize = counter;
}

int* expandArr(int *dyArr, int &physicalSize){
int *tempArr = NULL;
tempArr = new int[physicalSize * 2];
for (int i = 0; i < physicalSize; i++){
tempArr[i] = dyArr[i];}
dyArr = tempArr; //to give dyArr the address of tempArr
delete [] tempArr; //changed this from delete [] dyArr to delete [] tempArr
tempArr = nullptr;
physicalSize = physicalSize * 2;

return dyArr;
}

void searchNum(int *dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize ){
int index, i;
index = 0;
searchNumArr = new int[index]; //changed this from logicalSize

for (i = 0; i < logicalSize; i++){
if (dyArr[i] == num){
searchNumArr[index] = i + 1;
index++;
}
}
searchNumArrSize = index;

}

//function to print the results

int* printArray(int *dyArr, int arrSize){
int i;
for (i = 0; i < arrSize; i++){ //i < end of the array
if (i == arrSize -1){
cout << dyArr[i] << "." ;
}
else {
cout <<dyArr[i] << ", ";
}
}
}
Reformat your post for readability.
https://www.cplusplus.com/articles/jEywvCM9/
As formatted with a couple of minor changes (void return from printArray):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <vector>
using namespace std;

void readInput(int*& dyArr, int& logicalSize); //function that reads positive integers as input from the user and
//prints all the line numbers in the sequence entered by the user that contain a number num selected by the user

int* expandArr(int* dyArr, int& physicalSize); //function that expands the physical size of a dynamic array

void searchNum(int* dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize); //function that searches an array for a number selected by the user

void printArray(int* dyArr, int arrSize); //function to print an array given its' size and elements

int main_arr();
int main_vector();

int main()
{
	main_arr();
	return 0;
}

int main_arr()
{
	int* dyArr = NULL;
	int logicalSize = 0;
	int num;
	int* searchNumArr = nullptr;
	int searchNumArrSize = 0;

	cout << "Please enter a sequence of positive integers, each in a separate line." << endl;
	cout << "End your input by typing -1." << endl;
	readInput(dyArr, logicalSize);

	cout << "Please enter a number you want to search." << endl;
	cin >> num;

	searchNum(dyArr, logicalSize, num, searchNumArr, searchNumArrSize); //need to make sure I call this function properly
	if (searchNumArrSize == 0) {
		cout << "The number " << num << " does not show at all in the sequence.";
	} else {
		cout << num << " shows in lines ";
		printArray(searchNumArr, searchNumArrSize);
	}

	// delete [] searchNumArr;
	// searchNumArr = nullptr;
	return 0;
}

int main_vector() {
	// vector<int> numberVector(int &len) ----numberVector does NOT need the length input;
	//vector<int> .... whatever functions I decide to use for this case;

	return 0;
}

void readInput(int*& dyArr, int& logicalSize) {
	int physicalSize = 1;
	dyArr = new int[physicalSize];
	int posInt;
	int counter; //added two lines here
	counter = 0;

	bool seenEndOfInput = false;

	while (seenEndOfInput == false) {
		cin >> posInt;
		if (posInt == -1) {
			seenEndOfInput = true;
		} else {
			if (logicalSize == physicalSize) {
				expandArr(dyArr, physicalSize);
			}

			dyArr[counter] = posInt;
			// logicalSize++;
			counter++; //added this line
		}
	}
	logicalSize = counter;
}

int* expandArr(int* dyArr, int& physicalSize) {
	int* tempArr = NULL;
	tempArr = new int[physicalSize * 2];

	for (int i = 0; i < physicalSize; i++) {
		tempArr[i] = dyArr[i];
	}

	dyArr = tempArr; //to give dyArr the address of tempArr
	delete[] tempArr; //changed this from delete [] dyArr to delete [] tempArr
	tempArr = nullptr;
	physicalSize = physicalSize * 2;

	return dyArr;
}

void searchNum(int* dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize) {
	int index, i;
	index = 0;

	searchNumArr = new int[index]; //changed this from logicalSize

	for (i = 0; i < logicalSize; i++) {
		if (dyArr[i] == num) {
			searchNumArr[index] = i + 1;
			index++;
		}
	}
	searchNumArrSize = index;
}

//function to print the results

void printArray(int* dyArr, int arrSize) {
	int i;

	for (i = 0; i < arrSize; i++) { //i < end of the array
		if (i == arrSize - 1) {
			cout << dyArr[i] << ".";
		} else {
			cout << dyArr[i] << ", ";
		}
	}
}


"Process finished with exit code 143 (interrupted by signal 15: SIGTERM)".


Could be due to printArray() specifying a return type - but not returning a value.
Last edited on
Several issues
- readInput wasn't using logicalSize to track usage
- readInput was ignoring the return result of expandArr
- expandArr was deleting the memory it had just allocated.
- searchNum used a bogus size of zero as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <vector>
using namespace std;

void readInput(int*& dyArr, int& logicalSize); //function that reads positive integers as input from the user and
//prints all the line numbers in the sequence entered by the user that contain a number num selected by the user

int* expandArr(int* dyArr, int& physicalSize); //function that expands the physical size of a dynamic array

void searchNum(int* dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize); //function that searches an array for a number selected by the user

void printArray(int* dyArr, int arrSize); //function to print an array given its' size and elements

int main_arr();
int main_vector();

int main()
{
	main_arr();
	return 0;
}

int main_arr()
{
	int* dyArr = NULL;
	int logicalSize = 0;
	int num;
	int* searchNumArr = nullptr;
	int searchNumArrSize = 0;

	cout << "Please enter a sequence of positive integers, each in a separate line." << endl;
	cout << "End your input by typing -1." << endl;
	readInput(dyArr, logicalSize);

	cout << "Please enter a number you want to search." << endl;
	cin >> num;

	searchNum(dyArr, logicalSize, num, searchNumArr, searchNumArrSize); //need to make sure I call this function properly
	if (searchNumArrSize == 0) {
		cout << "The number " << num << " does not show at all in the sequence.";
	} else {
		cout << num << " shows in lines ";
		printArray(searchNumArr, searchNumArrSize);
	}

	delete [] searchNumArr;
	delete [] dyArr;
	// searchNumArr = nullptr;
	return 0;
}

int main_vector() {
	// vector<int> numberVector(int &len) ----numberVector does NOT need the length input;
	//vector<int> .... whatever functions I decide to use for this case;

	return 0;
}

void readInput(int*& dyArr, int& logicalSize) {
	int physicalSize = 1;
	dyArr = new int[physicalSize];
	int posInt;
// 	int counter; //added two lines here
// 	counter = 0;

	bool seenEndOfInput = false;

	while (seenEndOfInput == false) {
		cin >> posInt;
		if (posInt == -1) {
			seenEndOfInput = true;
		} else {
			if (logicalSize == physicalSize) {
				dyArr = expandArr(dyArr, physicalSize);
			}

			dyArr[logicalSize++] = posInt;
			// logicalSize++;
			// counter++; //added this line
		}
	}
	// logicalSize = counter;
}

int* expandArr(int* dyArr, int& physicalSize) {
	int* tempArr = NULL;
	tempArr = new int[physicalSize * 2];

	for (int i = 0; i < physicalSize; i++) {
		tempArr[i] = dyArr[i];
	}

	delete [] dyArr; // bye bye old array
// 	dyArr = tempArr; //to give dyArr the address of tempArr
// 	delete[] tempArr; //changed this from delete [] dyArr to delete [] tempArr
// 	tempArr = nullptr;
	physicalSize = physicalSize * 2;

	return tempArr; // hello new array
}

void searchNum(int* dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize) {
	int index, i;
	index = 0;

	searchNumArr = new int[logicalSize]; //changed this from logicalSize

	for (i = 0; i < logicalSize; i++) {
		if (dyArr[i] == num) {
			searchNumArr[index] = i + 1;
			index++;
		}
	}
	searchNumArrSize = index;
}

//function to print the results

void printArray(int* dyArr, int arrSize) {
	int i;

	for (i = 0; i < arrSize; i++) { //i < end of the array
		if (i == arrSize - 1) {
			cout << dyArr[i] << ".";
		} else {
			cout << dyArr[i] << ", ";
		}
	}
}


Now valgrind doesn't detect overruns, or leaks.

$ g++ -g -std=c++11 -Wall -Wextra -O2 foo.cpp
$ valgrind ./a.out 
==9176== Memcheck, a memory error detector
==9176== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9176== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9176== Command: ./a.out
==9176== 
Please enter a sequence of positive integers, each in a separate line.
End your input by typing -1.
1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100
-1
Please enter a number you want to search.
42
The number 42 does not show at all in the sequence.==9176== 
==9176== HEAP SUMMARY:
==9176==     in use at exit: 72,704 bytes in 1 blocks
==9176==   total heap usage: 10 allocs, 9 frees, 75,080 bytes allocated
==9176== 
==9176== LEAK SUMMARY:
==9176==    definitely lost: 0 bytes in 0 blocks
==9176==    indirectly lost: 0 bytes in 0 blocks
==9176==      possibly lost: 0 bytes in 0 blocks
==9176==    still reachable: 72,704 bytes in 1 blocks
==9176==         suppressed: 0 bytes in 0 blocks
==9176== Rerun with --leak-check=full to see details of leaked memory
==9176== 
==9176== For counts of detected and suppressed errors, rerun with: -v
==9176== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$ 
$ valgrind ./a.out 
==9183== Memcheck, a memory error detector
==9183== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9183== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9183== Command: ./a.out
==9183== 
Please enter a sequence of positive integers, each in a separate line.
End your input by typing -1.
1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100
1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100
1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100
-1
Please enter a number you want to search.
10
10 shows in lines 10, 29, 48.==9183== 
==9183== HEAP SUMMARY:
==9183==     in use at exit: 72,704 bytes in 1 blocks
==9183==   total heap usage: 11 allocs, 10 frees, 75,488 bytes allocated
==9183== 
==9183== LEAK SUMMARY:
==9183==    definitely lost: 0 bytes in 0 blocks
==9183==    indirectly lost: 0 bytes in 0 blocks
==9183==      possibly lost: 0 bytes in 0 blocks
==9183==    still reachable: 72,704 bytes in 1 blocks
==9183==         suppressed: 0 bytes in 0 blocks
==9183== Rerun with --leak-check=full to see details of leaked memory
==9183== 
==9183== For counts of detected and suppressed errors, rerun with: -v
==9183== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Thank you both! Appreciate your help
Topic archived. No new replies allowed.