Compare strings and character arrays (arrays and dynamically allocating memory) in C + + and Delphi

Source: Internet
Author: User



This article explains only one aspect of the summary, so it is necessary to combine the conversion between Delphi string and character array (the importance of initialization) And more comprehensive understanding of the string, memory, type conversion






Memory space allocated by strings, character arrays, character pointers in C/s + +



We know that in C + +, we can understand strings and character arrays like this



1) A string, which is an array of characters ending with '/'



2) a character array, including a string, but not necessarily ending with ' \ ', and not as a string if it does not end with



The use of strings can be used for arrays, or by allocating memory dynamically for character pointers to hold strings, similar to character arrays, but the memory of this method requires the programmer to manage



An array of characters not ending with '/', cannot be used as a string


char c[2];
c[0] = ‘a‘;
c[1] = ‘b‘;


An array of characters ending with '/', which can be used as a string


Char c[4];
c[0] = ‘a‘; c[1] = ‘b‘; c[2] = ‘c‘;
c[3] = ‘\0‘;
/ *
Equivalent to the following method
char c[4] = "abc";
* /


The dynamically allocated char pointer to the memory that is not ending with '/' and cannot be used as a string


char *p = (char *)malloc(2);
p[0] = ‘a‘;
p[1] = ‘b‘;


Iv. dynamically allocated char pointer to memory, ending with '/', can be used as a string


char *p = (char *)malloc(4);
p[0]=‘a‘; p[1]=‘b‘; p[2]=‘c‘;
p[3]=‘\0‘;


  But it is not equivalent to the following code


char *p = (char *)malloc(4);
p="abc";


Instead of storing "ABC" in the memory that P points to, the P pointer points to the address of the string constant "ABC", so that the address of the previously allocated memory is not recorded, and there is no way to release it, thus causing a memory leak.



You can verify this by two times the address of the output p.


#include<stdio.h>

int main(){
char * p;
p = (char *)malloc(4);

printf("%d\n", (p));

p="abc";

printf("%d", p);

return 0;
}


  my test, two times the address of the output, one is 202720, one is 4206632, obviously not point to the same address . and


char *p = (char *)malloc(4);
printf("%d\n", p);
p[0]=‘a‘; p[1]=‘b‘; p[2]=‘c‘;
p[3]=‘\0‘;
printf("%d\n", p);


Because it is the element that operates P, not the direct direction of the P pointer, the two address of the above output is the same



So to understand what the nature of a char array and char* dynamically allocated memory, different assignment methods









There are string types in Delphi, and so on, but only the character arrays, the memory spaces allocated by the character pointers, and the cases in which they represent strings are discussed.



In fact, Delphi's knowledge in this area is the same as in C + +, as previously said, they are compatible, so in the writing of C/S and Delphi-compatible code, Delphi to use a char array or pchar dynamic memory allocation, Instead of using string types such as String



The array is nothing more than memory allocation on the stack, so the other aspects are similar to the following Pchar dynamic memory allocation situation



If you are allocating memory to the Pchar pointer (Getmem (p, size)) Then there are two things at this time (mainly one and two, and three is a special case of additional explanation)



    An array of characters ending with ' s ', which can be used as a string, not as an array of characters ending with ' pchar ' or as a string .



One, similar to array


var
    p: PChar;
    i: Integer;
begin
    GetMem(p, 3);
    for i:= 0 to 2 do
        p[i]:=‘j‘;
end;


This is an array of characters, and not ending with #0,



Second, or indicate that the last element is #0


var
    p: PChar;
    i: Integer;
begin
    GetMem(p, 3);
    for i:= 0 to 1 do
        p[i]:=‘j‘;
    p[2]:= #0; 
end;


This doesn't change the point of P.



Three, string


var
    p: PChar;
    i: Integer;
begin
    GetMem(p, 3);
    p:= ‘jo‘;
end;


Refer to the c/c+ section of the fourth example, in fact, here the Cheng and program two is the above-mentioned kind of operation Pchar pointer elements and direct operation Pchar Pointer situation



If you don't believe it, test it to verify


procedure TForm1.btn1Click(Sender: TObject);
Var
P: PChar;
I: Integer;
Begin
GetMem(p, 3);
i: = integer (P); / / address after dynamic memory allocation as p
p:= ‘jo‘;
//The following comparison P: = 'Jo'; the address of the memory pointed to by the preceding and subsequent p
ShowMessage(IntToStr(i)+‘ ‘+IntToStr(Integer(p)));
End; 


The results are as follows (obviously, as in C + +)






Compare strings and character arrays (arrays and dynamically allocating memory) in C + + and Delphi


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.