Go Down

Topic: Beta testers wanted re new String class (Read 414 times) previous topic - next topic

boylesg

Aug 25, 2016, 01:29 pm Last Edit: Aug 25, 2016, 01:39 pm by boylesg
After hearing on numerous occasions that the existing String class is bad, on account of its use of the heap, I have designed a new string class based loosely on the PString class but with the same and similar functionality as the original String class.

Particularly am wondering if there is a better way of doing the following that I am not aware of.

I could shorten the macros, but the general idea is to avoid having to declare each CString variable like this:

char* strBuff[20];
CString str(strBuff, 20);

Instead you do it like this with the macros:

DEC_CSTRING1("str", 20)
CString1("str")[0] = 'a';


Code: [Select]
#define DEC_CSTRING1(STR_NAME, N_SIZE) char* strBuff1[N_SIZE]; CString str1((char*)strBuff1, N_SIZE);
#define CSTRING1(STR_NAME) str1

#define DEC_CSTRING2(STR_NAME, N_SIZE) char* strBuff2[N_SIZE]; CString str2((char*)strBuff2, N_SIZE);
#define CSTRING2(STR_NAME) str2

#define DEC_CSTRING3(STR_NAME, N_SIZE) char* strBuff3[N_SIZE]; CString str3((char*)strBuff3, N_SIZE);
#define CSTRING3(STR_NAME) str3

#define DEC_CSTRING4(STR_NAME, N_SIZE) char* strBuff4[N_SIZE]; CString str4((char*)strBuff4, N_SIZE);
#define CSTRING4(STR_NAME) str4

#define DEC_CSTRING5(STR_NAME, N_SIZE) char* strBuff5[N_SIZE]; CString str5((char*)strBuff5, N_SIZE);
#define CSTRING5(STR_NAME) str5

#define DEC_CSTRING6(STR_NAME, N_SIZE) char* strBuff6[N_SIZE]; CString str6((char*)strBuff6, N_SIZE);
#define CSTRING6(STR_NAME) str6


Delta_G

#1
Aug 26, 2016, 02:47 am Last Edit: Aug 26, 2016, 02:50 am by Delta_G
Code: [Select]

char *m_strBuff, *m_strStartPtr, *m_strEndPtr;
    size_t m_nBuffSize;
    char m_strWorking[BUFF_SIZE];


Where BUFF_SIZE is 201.

You're not using dynamic allocation here, that's cool.  But for every one of these objects you create you are consuming  209 bytes of RAM.  That's a lot of ram for a string like "Hello World" which as a c-string consumes only 12 bytes.  No worries about fragmentation, but even worse you're just going to be out of RAM with just 10 strings.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up