Questions tagged [const-string]

11 questions
7
votes
5 answers

Why strrchr() returns `char*` instead of `const char*`?

The function char* strrchr(const char *str, int ch) returns a pointer (char*) within str (const char *) where the last occurrence of ch is located. So we can write the following code without any cast: #include int main() { const char…
oHo
  • 51,447
  • 27
  • 165
  • 200
5
votes
4 answers

is static const string member variable always initialized before used?

In C++, if I want to define some non-local const string which can be used in different classes, functions, files, the approaches that I know are: use define directives, e.g. #define STR_VALUE "some_string_value" const class member variable,…
Hunter
  • 151
  • 1
  • 14
2
votes
0 answers

Memory optimization: declare multiple string variables VS use in place

I have to code a class with heavy memory allocation considerations. This class have strings all over the place like so var data = GetPropertyValue(response.BaseData, "scriptData"); Just like "scriptData" there are over 10 strings declared…
2
votes
2 answers

User Defined String Literals compared to Const Strings

Consider the following code with these two structures: std::string operator"" _str(const char* str, std::size_t len) { return std::string( str, len ); } struct MessageLiterals { std::string HELP = "Press F1 for help"_str; std::string…
2
votes
2 answers

Where does const string store? in stack or .data?

I have written a simple c code which shows below. In this code snippet I want to verify where the const string abcd stores. I first guess that it should be stored in .data section for read-only. After a test in Debian, however, things is different…
Douglas Su
  • 3,214
  • 7
  • 29
  • 58
1
vote
1 answer

const_string lib issue

I'm trying const_string lib that looks not bad, but it crashes at runtime with access violation(atomic_count, operator++()). The test code: #include #include typedef…
G0r
  • 13
  • 2
1
vote
1 answer

Trouble with passing C string literals to a function while using a C++ compiler to compile the code?

I'm getting an error that says "argument type "const char *" is incompatible with "char *". This code was provided by my professor and I'm not sure what the problem is. I'm writing C but I'm using a C++ compiler because it is easier to debug if…
Destin Hilt
  • 15
  • 1
  • 7
1
vote
1 answer

Why segmentation fault happened when i define a const string in a google test programe?

I want to run unit test with google test, but got Segmentation fault. I have just define a global const string, and RUN_ALL_TESTS, why? the gdb result is : [Thread debugging using libthread_db enabled] Using host libthread_db library…
0
votes
1 answer

const_string as map key; make a buffer copy?

the whole point of const_string is avoiding make a unnecesary copy when the string is not supposed to change. However, there are circumstances where you cannot guarantee the lifetime of the const char* source to outlive the const_string, for…
lurscher
  • 25,930
  • 29
  • 122
  • 185
0
votes
2 answers

How to compare a string to a const string in C++

Let's say I have the following poc code: const string& a = "hello"; string b = "l"; if(a.at(2) == b) { // do stuff } I understand that there is no operator "==" that matches these operands. And, the way to fix it is by converting the value of…
Xigma
  • 177
  • 1
  • 11
-2
votes
1 answer

What is the best way to declare "string" variables in C #?

I'm a new developer and I have two questions, I researched and it wasn't very clear to me. What is the difference between using a "string" variable to "String"? what is the difference between the two examples below string my_example = "title…