Segfault while using arrays, pointers, and for loops.

I am doing an assignment for class in which we are required to use pointers for nearly all of the project, so please excuse the nonsensical usage.

The goal is to return the user entered string back without repeat characters. For some reason, the isInArray function is called way too many times, and then a segfault occurs.

I'm not quite done writing it, as this bug is holding me back.

I believe the error to be in this region:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void deleteRepeats(int startingSize, int* removedPtr, char text[]) {

char* temp = new char [startingSize];
for (char* ptr = &text[0]; ptr < ptr + startingSize; ptr++) {
	if (isInArray(text, startingSize, *ptr) == false) {
		*temp = *ptr;
		temp++;
	}
	else *removedPtr++;
}
char* trimmedArray = new char [startingSize - *removedPtr];
for (char* ptr = &temp[0]; ptr < ptr + (startingSize - *removedPtr); ptr++) {
	*(trimmedArray++) = *(temp++);
}
}


Here is the rest of the code just in case:
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
#include <iostream>
#include <string>
using namespace std;

int startingSize = 0, removedSize = 0;
int* removedPtr = &removedSize;
string input;
char text[140];
void deleteRepeats(int startingSize, int* removedPtr, char text[]);
bool isInArray(char text[], int StartingSize, char target);

int main(){
cout << "Enter 140 characters or less: ";
getline(cin, input);
cout <<"\nYou entered: " <<endl <<input <<endl;
char* arrayPtr= text;
	for (int i=0; i < input.length(); i++) {
                	*(arrayPtr + i) = input[i];
	}
startingSize = input.length();
cout <<"Loaded Array: ";
for (int i=0; i <= startingSize; i++)
	cout << text[i];
cout <<endl;
cout <<"Starting Size: " <<startingSize <<endl;
cout <<"\nDeleting Repeats: " <<endl;
deleteRepeats(startingSize, removedPtr, text);
return 0;
}
//Move non-duplicates to temporary array, then copy to a dynamic array of exact size.
void deleteRepeats(int startingSize, int* removedPtr, char text[]) {

char* temp = new char [startingSize];
for (char* ptr = &text[0]; ptr < ptr + startingSize; ptr++) {
	if (isInArray(text, startingSize, *ptr) == false) {
		*temp = *ptr;
		temp++;
	}
	else *removedPtr++;
}
char* trimmedArray = new char [startingSize - *removedPtr];
for (char* ptr = &temp[0]; ptr < ptr + (startingSize - *removedPtr); ptr++) {
	*(trimmedArray++) = *(temp++);
}

}

bool isInArray(char arr[], int StartingSize, char target) {
//cout <<"\n IsInArray called. \n";
char* ptr= &arr[0];
while ((ptr < ptr+startingSize) && (*ptr != '\n')) {
        if (*ptr == target) return 1;
        ptr++;
        }
return 0;
}
Last edited on
In retrospect, ptr < ptr + startingSize; might be one of the dumbest things i've ever written. For anyone who stumbles across this later, ptr is incrementing up, so the statement ALWAYS evaluates true and never leaves the for loop. Marked as solved.
Topic archived. No new replies allowed.