简体   繁体   English

Memory 在 c 中分配字符串

[英]Memory Allocation in c with string

void Strcat(char str1[], char str2[]){
long len1 = strlen(str1);
long len2 = strlen(str2);

char* str = (char*)malloc(((len1 + len2) + 1)*sizeof(char));
if(str == NULL){
    printf("No memory");
    exit(1);
}
for (int i = 0 ; str1[i] != '\0'; i++) {
    str[i] = str1[i];
}

str[strlen(str1)] = ' ';
for (long i = 0, j = strlen(str1)+1 ; str2[i] !='\0' ; i++, j++) {
    str[j] = str2[i];
    if(str2[i+1] == '\0')
        str[j+1] = '\0';
}

//puts(str);
printf("strlen STR -> %ld\n", strlen(str));
for (int i = 0; str[i] != '\0'; i++) {
    printf("%c",str[i]);
}

free(str);

} }

Ok I know the strcat function is a string between two strings.好的,我知道 strcat function 是两个字符串之间的字符串。 Suppose I put the input "ttt" into the first string And the second string the input "yyy".假设我将输入“ttt”放入第一个字符串,将第二个字符串放入输入“yyy”。 I am now using dynamic assignment using malloc Now I know we need to take the length of the first + second + 1 the 1 is for the '\0' character.我现在使用动态赋值 malloc 现在我知道我们需要取第一个 + 第二个 + 1 的长度,1 代表 '\0' 字符。

So my allocation is size 7.所以我的分配是 7 号。

but I need to make a space between the two strings Do I need my allocation to be 8?但我需要在两个字符串之间留一个空格 我需要分配为 8 吗? because when I do only sizeLength + 1 the program is still working and it still puts a space between the two strings and I feel like the compiler forgives me.因为当我只执行 sizeLength + 1 时,程序仍在运行,它仍然在两个字符串之间放置一个空格,我觉得编译器原谅了我。

I know the strcat function is a string between two strings我知道 strcat function 是两个字符串之间的字符串

Umm, no it isn't?嗯,不,不是吗? I don't know what that's even supposed to mean.我什至不知道那是什么意思。 strcat stands for string concatenation (and not "strangle the cat" as one may assume at a glance:) ). strcat代表字符串连接(而不是乍一看可能认为的“扼杀猫”:))。 Adding a space between them is not how standard library strcat works.在它们之间添加空格不是标准库strcat的工作方式。

So my allocation is size 7所以我的分配是 7 号

Yes, that is correct for implementing strcat with input lengths of 3 + 3, with space for the null terminator at the end.是的,这对于实现输入长度为 3 + 3 的strcat是正确的,末尾有 null 终止符的空间。

but I need to make a space between the two strings但我需要在两个字符串之间留一个空格

Not sure why you'd want to do that, but if you want a space character in between them you need to allocate one extra byte indeed.不确定你为什么要这样做,但如果你想在它们之间有一个空格字符,你确实需要分配一个额外的字节。

Do I need my allocation to be 8?我需要我的分配是 8 吗?

Only if you need to make room for the space.仅当您需要为空间腾出空间时。

because when I do only sizeLength + 1 the program is still working因为当我只执行 sizeLength + 1 时,程序仍在运行

It only works by (bad) luck, this time.这一次只能靠(坏)运气。 What is undefined behavior and how does it work?什么是未定义的行为,它是如何工作的?

I feel like the compiler forgives me我觉得编译器原谅了我

It's not the programmer's job to keep track of correct amounts of memory allocated.跟踪分配的 memory 的正确数量不是程序员的工作。 C doesn't have bounds-checking of arrays, nor does it have automatic (re)allocation etc as higher level languages might have. C 没有 arrays 的边界检查,也没有高级语言可能具有的自动(重新)分配等。

And as someone said in comments, malloc likely allocates larger chunks of aligned memory. Size 8 bytes seems like a very likely candidate for one such aligned chunk, regardless if you actually just use 7 bytes.正如有人在评论中所说,malloc 可能会分配更大的对齐块 memory。大小 8 字节似乎很可能是这种对齐块的候选者,无论您是否实际上只使用 7 字节。 But this is by no means guaranteed behavior and not something you can rely on.但这绝不是保证的行为,也不是您可以依赖的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM