Overloading a Pointer with New Operator

Hey,
My name is Corey, I've been doing programming off and on as a hobby over the last 10 years or so.

I needed to do a string operation in C++ to separate parts of the string based on a delimiter into an array

Example in VBA, return an array {"Hello", "World"}
Split("Hello World", " ")

I attempted doing this in C++ that would give an equivalent output, but I have a question about how i'm handling the New operator.

I created a pointer ss in my main function, then
In another function I create another pointer result
In the function I use the New operator to create a dynamic array then return the pointer.
back in my main I overload the ss pointer with the pointer returned from the split function.
In my Main, I delete[] the ss pointer
Is this enough, am I creating a memory leak?

I know somebody is going to point out that I should being using a vector for this type of thing. But I’m more interested in how to handle overloading in these sistuations.
As for resolve any doubts of a memory leak, I’ll simply reference the pointer ss from my main to the function instead.

The code I'm using to delimit a string:

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
#include <iostream>
#include <string>

using namespace std;

string* split (string str, string delimiter, int &numFound) {
    // seperate string into each time delimiter character is found
	size_t pos = 0;
	int num = 1;
	string s = str;
	string* result;
	// pre-pass get number of seperations
	while ((pos = s.find(delimiter)) != string::npos) {
		s.erase(0, pos + delimiter.length());
		num++;
		}
	numFound = num;
	// if pre-pass, then create array
	if (numFound > 0) {
	    num = 0;
	    s = str;
		result = new string[numFound];
		while ((pos = s.find(delimiter)) != string::npos) {
			result[num] = s.substr(0, pos);
			s.erase(0, pos + delimiter.length());
			num++;
			}
		result[num] = s.substr(0, pos);
		}
	// return array
	return result;
	}

int main() {
	string* ss; // String Stream Array
	int sc = 0; // String Stream Array Count
    
    ss = split("Hello World", " ", sc);
    
    if (sc > 0) {
        for (int i = 0; i < sc; i++){
            cout << ss[i] << endl;
            }
        }
    delete[] ss;
    }
First off, you're assigning a pointer. You can overload a function.

Your code is correct. It doesn't create a memory leak.
However, note that split() returns an invalid pointer if numFound is zero. You should avoid this. You should only return either a valid pointer or nullptr.
As far as C++ is concerned, you are misusing the word "overload". That's something totally different.

You are mixing spaces and tabs for your indentation. Using just spaces is best. You should be able to set your editor to insert spaces for tabs.

There's no reason to make copies of the string and erase parts of it just to get find to find successive delimiters. You can use find's second parameter, which tells it where to start searching.

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
#include <iostream>
#include <string>
using namespace std;

string* split(string str, string delimiter, int &size) {
    string* result = nullptr;
    size_t start = 0, end = 0;

    while ((start = str.find(delimiter, start)) != string::npos) {
        size++;
        start += delimiter.size();
    }
    if (str.size() > 0)
        size++;

    if (size > 0) {
        start = 0;
        result = new string[size];
        int i = 0;
        while ((end = str.find(delimiter, start)) != string::npos) {
            result[i++] = str.substr(start, end - start);
            start = end + delimiter.size();
        }
        result[i++] = str.substr(start, end - start);
    }

    return result;
}

int main() {
    int sc = 0;
    string* ss = split("one  two  three  four", "  ", sc);

    cout << '[';
    if (sc > 0) {
        cout << ss[0];
        for (int i = 1; i < sc; i++)
            cout << ',' << ss[i];
    }
    cout << "]\n";

    delete[] ss;
}

feels like you should return a vector of string that could be empty or have 10 split out 'words' or whatever? Is there a need for a pointer here?

Topic archived. No new replies allowed.