(C ++ dialogue) Solmy's small piece series: Where is the string?

zhaozj2021-02-17  54

Original author Solmyr Posted: 2002-05-19 20:25:27 from PChome.net

This article I have appointed reposted, this is shared with you! At the same time, I hope that there can be more and better dialogue in China, which is better than watching cold ice-ice Effective and More Effective!

Painting: Today is a big sunny day, the warm sun passes through the window into this spacious office, and the people in the office are working hard before their computer, everything is so quiet, peaceful, normal ... "Ah ~ Help! Solmyr You use the folder to me! "" The stupid thing should be punished. "Painting outside: ... Hey, okay, I have a little less exception. This is a software company, which is called Zero, a new college student; here is elegant, it seems to be very cultivated, it is not like this, Solmyr, Senior Programmer, Responsible for the training of Zero's newcomers. Ah, the story begins ... "What did I do?" Zero asked with his nose. "This time you bake my folder and big one!" "You come over to see you committed. Error. "SolmyR turned out of Zero's first code: ... char * msg =" connectting ... please wait "... if (status == s_connected)         ... "What is wrong? This is a very ordinary string statement," Zero is dissatisfied. "You can't see it? Connect This word is in the past, you are missing, playing a T", SolmyR answers. "Just in order to use the folder again ... ah! This is a CD box!" "This is a commercial software, you think it is chatting on QQ and PPMM, there is a wrong word should not be tight?" Worse, I I deliberately stayed for such a long time to give you, now you haven't found your true mistake. You are not a general dish ~ "SolmyR deliberately dragged a long sound, satisfied see ZERO in an outbreak The edge, "Ok, let's start from the foundation, how is the string in the C language?" "I know", ZERO is very confident, "C / C language, string is a continuous character Source memory unit, each unit stores a character, and uses \0 as the end of the tail. "" So before we use the pointer, we should ... "We should ensure that this pointer points to the legal memory, or points to a already existing memory, Either dynamically assign a piece. "Zero starts to show a proud smile - this level of problem, ha! "Well! So where is the MSG of MSG pointing in your code?" The smile is solidified. "This ... oh ... I want ... It should point to a legal memory, because I have been working like this, it works ...", the Zero period Aii said. "Legal memory? Who is all assigned? How big is it? How long is the survival cycle? What special nature?" "..." "Hey!", Solmyr is sighing, "I know that I will like this. Ok, let's start with your simple start. "

SolmyR is fast to type as follows: char msg [] = "Hello"; char * pmsg = (char *) Malloc (Sizeof ("Hello")); STRCPY (PMSG, "Hello"); "These code you should It's very clear: MSG is a character array, and the C language guarantee will assign a continuous memory for it, and initialize it to "Hello". PMSG is a character pointer, we call the malloc function to dynamically assign a memory And fill its value "Hello" with the STRCPY function. The common point of these two practices is: First get a memory with normal means, then fill the value. Next, look at this: "Char * msg =" hello ";" What does it mean? First, MSG is a pointer, the C / C language is not responsible for assigning a memory for it; secondary, we have not explicitly assigned a memory. Where does it point to "hello", you are writing directly in the code directly The one. "" What is the one who is directly written directly in the code '? ", Zero reveals the confused expression", you will understand: ", SolmyR Type: Double DB = 1.5;" this line Inside, 1.5 is something? It is a Double type constant, C / C language to handle them, but also to allocate these things. Similarly, when you write "Hello" in the code, actually C / C The language is assigned a memory to save this string. When you write char * msg = "hello", you just give the address of such a memory to the pointer MSG. So MSG does point to a legal memory, this is sometimes This code can work. But do this, which contains many questions, I will ask you, what type should you point to this memory? "" Of course, char * ", Zero does not respond. "Wrong! It should be const char *. I want to be eared, the string written in the program You don't want it to change, so it is obvious that this memory should be interpreted as a constant. But what did you do when you declare MSG? "" Hey ... I used a very square pointer to point to a constant string. "This time, ZERO is obviously prudent." "Correct. Look at your original code, you not only point to it with a very amount of pointer, but also execute Strcpy on this pointer, write content in our compiler, what will be caused?" " Oh ... causing a runtime error? "" Partially correct. Accurately, only when the engineering compilation option is debug version, if the engineering compilation option is a release version, everything is normal - strange? Do not, Live this: C / C allows you to break any protection. So if the two lines of code have not been discovered when debugging is discounted, I am slipping into the release version ", saying this, SolmyR stunned ZERO," will It will be difficult to find. "" But what is it still not harmful to do it? MSG points to a legal memory, correct, and not really can't write, what is it worried? " Zero complains. SolmyR stepping into the cup, zero reflectivity immediately turning his face. "Don't worry, I just drink water.

转载请注明原文地址:https://www.9cbs.com/read-30547.html

New Post(0)