c-strings

Why use c strings in c++?

五迷三道 提交于 2019-12-03 10:02:40
Is there any good reason to use C-strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string. The only reasons I've had to use them is when interfacing with 3rd party libraries that use C style strings. There might also be esoteric situations where you would use C style strings for performance reasons, but more often than not, using methods on C++ strings is probably faster due to inlining and specialization, etc. You can use the c_str() method in many cases when working with those sort of APIs, but you should

Using C-string: “Address of stack memory associated with local variable returned”

余生长醉 提交于 2019-12-03 06:48:07
问题 I am not a C programmer, so I am not that familiar with C-string but new I have to use a C library so here is a shortened version of my code to demonstrate my problem: char** ReadLineImpl::my_completion () { char* matches[1]; matches[0] = "add"; return matches; } I am getting a warning: Warning - address of stack memory associated with local variable 'matches' returned And my application does not seem to work properly (might be because of this warning). What is the warning and will it cause

memset is not working with pointer to character

烂漫一生 提交于 2019-12-02 13:10:48
What is wrong with the following code? memset is supposed to work with Pointer to the block of memory to fill. But this code displays problem in console saying segmentation fault(core dumped) #include<iostream> #include <cstring> using namespace std; int main(int argc, char** argv) { char* name = "SAMPLE TEXT"; memset(name , '*', 6); cout << name << endl; return 0; } You have tripped over a very old backward-compatibility wart in C++, inherited from C and dating to the days when there was no such thing as const . String literals have type const char [n] , but, unless you tell your compiler you

Does garbage collection happen when we initialize a char array with a string literal in c?

前提是你 提交于 2019-12-02 10:04:39
When we write the following line of code in C, char local_arr[] = "I am here"; the literal "I am here" gets stored in the read only part of the memory(say RM ). How I visualize it is that it gets stored contiguously in the RM (Is that right?). Then the array local_arr (i.e local array) copies this array index by index from its location in RM. But what happens to the literal after the local_array copies it? Is it lost thereby causing memory leaks? Or is there some sort of garbage collector like in Java that cleans up unreferenced objects? For example if i write a piece of code as follows : for

How to use fgets to read a file line by line

别来无恙 提交于 2019-12-02 09:36:54
I'm new at programming so there are some basics and maybe common sense that I don't know. I have a question about how to use fgets right. Based on the explanation of fgets, it seems that fgets should stop whenever it reads n-1 characters, hit the EOF or hit a newline character. For example, I create a text file like below: red 100 yellow 400 blue 300 green 500 purple 1000 ... The color and the integer is separated by a tab. When I create this text file, I need to hit enter at the end of each line to start a new line. In this case, hitting enter equals to add a newline character, '\n', is that

Modifying a c string [duplicate]

大城市里の小女人 提交于 2019-12-02 08:42:24
This question already has an answer here: Access violation writing location when working with pointers to char 3 answers I'm trying to implement tolower(char *) function, but I get access violation error. I came to know that this is because to compiler stores string literals in a read-only memory. Is this true? Here's some code: char* strToLower(char *str) { if(str == nullptr) return nullptr; size_t len = strlen(str); if(len <= 0) return nullptr; for(size_t i = 0; i < len; i++) *(str+i) = (char)tolower(*(str+i));//access violation error return str; } int main() { char *str = "ThIs Is A

Not null-terminating a C-style string

守給你的承諾、 提交于 2019-12-02 08:12:30
问题 Given a string , say , char *str = "Hello,StackOverflow!" char newStr[30]; int l = strlen(str); for(int i =0 ; i<l ; i ++ ) newStr[i] = str[i]; printf("%s" , newStr); Now , we know that the last character of a c-string has to be '\0' , Since here we haven't explicitly done the same ( store '\0' at last index of string newStr) , this program should crash since printf won't find the end of string. But I noticed that it was working fine sometimes and sometimes it wasn't. What could be the

char [length] initialization and dealing with it

断了今生、忘了曾经 提交于 2019-12-02 08:11:45
I have defined a char array: char d[6]; Correct me if I'm wrong regarding following: At this moment no memory is allocated for variable d . Now I'm going to initialize it: d="aaaaa"; After this kind of initialization, there would be no need to free memory; it will be done automatically. How do I know if the char[] was initialized? I am looking for a pattern like if (filled(d)){..} Also, how do I fill char[] with one kind of character? hmjd At this moment no memory allocated for variable d. Incorrect. This: char d[6]; is an uninitialised array of 6 char s and memory, on stack, has been

scanf and strcmp with c string

≡放荡痞女 提交于 2019-12-02 05:05:21
I found a nice example of how to use strcmp, but it's only working with fgets(), and i need to make it work with scanf. So, here's the code: int main(void) { char fruit[] = "apple\n"; char ans[80]; do { printf ("Guess my favorite fruit? "); scanf ("%s",ans); } while (strcmp (fruit, ans) != 0); puts ("Correct answer!"); return 0; } Even when I write the correct answear ("apple") it stays in the loop and keeps asking me what is the favorite fruit... I'm guessing it has something to do with the chars that are not written at ans[80](I need it to be a char array with 80chars at max). I'm not

Not null-terminating a C-style string

二次信任 提交于 2019-12-02 04:43:57
Given a string , say , char *str = "Hello,StackOverflow!" char newStr[30]; int l = strlen(str); for(int i =0 ; i<l ; i ++ ) newStr[i] = str[i]; printf("%s" , newStr); Now , we know that the last character of a c-string has to be '\0' , Since here we haven't explicitly done the same ( store '\0' at last index of string newStr) , this program should crash since printf won't find the end of string. But I noticed that it was working fine sometimes and sometimes it wasn't. What could be the problem ? It was working almost everytime actually. Isn't it supposed to crash or give some run-time error?