Strings and memory management paranoia

Hello,

I spent a while getting frustrated with the malloc() and free() baggage that comes with dealing in C strings, so I want a pure C++ approach to strings now. My very simple question is about efficiency and memory leak prevention.


Suppose I have a header file named stxaxticmath.h

1
2
3
4
5
6
7
8
9
10
#include <math.h>
#include <string>
#include <stdio.h>

std::string LEFTCPP (std::string TheString, int Pos1)
// Designed to copy QB's LEFT$(,) function.
{	std::string Temp;
	for (int index = 0; index < Pos1; index++) Temp += TheString[index];
	return Temp;
}


...and then a main program...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "stxaxticmath.h"

int main(){

std::string nnn = "+ABCD.EFG";
std::string mmm = "-ABCDEFG";
std::string ooo = "ABC756565DEFG";

std::cout << "LEFTCPP:\n";
std::cout << LEFTCPP(nnn,3) << "\n";
std::cout << LEFTCPP("ddfadfdf",3) << "\n";
std::cout << LEFTCPP(ooo,8) << "\n\n";

return 0;

}


For programmers old enough, you will notice this is a clone of QBASIC's LEFT$() function. It works when I test it - that's not the question.

And treat me like a beginner, I won't be offended... Here are the questions:

(i) Will this code leak memory?

(ii) Am I safe using std::string Temp; inside a function without any cleanup work? Does C++ know to get get space back after leaving the function scope?

(iii) Should I be using more pointers?

Thanks a lot guys.
(i) No.
(ii) Yes. Yes
(iii) No. Use pointer only if there's no other option (like std:: container)

You can safely assume that the standard library works. If used correctly it will not leak memory nor cause it any other problems.

1. Not "leak", but see "Exception safety" in http://www.cplusplus.com/reference/string/string/operator%5B%5D/

2. Safe. Temp is automatic local object that will be appropriately destroyed at the end of the function. "User-defined" types (like std::string) have "destructor" member function for "clean up".

It is the use of malloc/free or new/delete that put the burden on you.


3. Pointers are not necessary here.


See also: http://www.cplusplus.com/reference/string/string/substr/
substr() can do LEFT$, MID$, and RIGHT$ with appropriate parameters.
closed account (48T7M4Gy)
i) No. Use C++ 'new' followed by 'delete' instead of C malloc() etc and you won't have a problem. Read up on new and delete.

ii) Yes - read up on new and delete. delete is the knowledge you're referring to. because string doesn't declare or allocate dynamic memory then the garbage is automatically cleaned up. Dynamic memory is allocated by new and it is cleaned up by delete. No delete then you have a memory leak.

iii) new automatically creates the pointer to the data and it has specific uses but they are not always used.

For a start try this http://www.cplusplus.com/doc/tutorial/dynamic/ for a start

It takes a bit of reading but it's not all that complicated.

Thanks everyone!

From what I'm reading, I gather that I can avoid the malloc/free and new/delete paradigms completely, namely because <string> is doing all the dirty work for me.

So if I code up, say 250 more functions like the one above, and have them running all together, will I run into trouble?

Specifically, I want to translate my new largenum calculator / math sublanguage from QB to C++ (no graphics for now, just calculations on large numbers stored as strings):

http://people.umass.edu/wfbarnes/STxAxTICMathWriteup.html
STxAxTIC wrote:
From what I'm reading, I gather that I can avoid the malloc/free and new/delete paradigms completely, namely because <string> is doing all the dirty work for me.
Yes. In fact, you should try to avoid them whenever possible and let automatic memory management do work.

STxAxTIC wrote:
So if I code up, say 250 more functions like the one above, and have them running all together, will I run into trouble?
No. If all functions are like that (no leaks, no UB), then you are completely fine.

Topic archived. No new replies allowed.