如何用memcpy复制?

发布于 2025-01-26 03:14:22 字数 887 浏览 2 评论 0原文

char *concat(char *num1, const char *num2, int index) {

    int length1 = strlen(num1);
    int length2 = strlen(num2); 
    int lengthNum = 0;                   
    char *num = malloc(length1 + length2 + 1);

    if (num == NULL) {
        free(num);
        return NULL;
    }
    // memcpy(num, num1, length1);
    // memcpy(num + length1, num + index, length2 + 1);    

    for (int i = 0; i < length1; i++) {
        num[lengthNum] = num1[i];
        lengthNum++;
    }
    for (int i = index; i < length2; i++) {
        num[lengthNum] = num2[i];
        lengthNum++;
    }
    return num;
}

我尝试使用memcpy,但是除了我的程序无法正常工作(副本错误,但valgrind没有显示错误)。

但是,当我使用两个for循环时,它可以正常工作,而是valgrind显示错误

非初始化的值是由堆分配创建的。

在这种情况下,如何正确使用memcpy

char *concat(char *num1, const char *num2, int index) {

    int length1 = strlen(num1);
    int length2 = strlen(num2); 
    int lengthNum = 0;                   
    char *num = malloc(length1 + length2 + 1);

    if (num == NULL) {
        free(num);
        return NULL;
    }
    // memcpy(num, num1, length1);
    // memcpy(num + length1, num + index, length2 + 1);    

    for (int i = 0; i < length1; i++) {
        num[lengthNum] = num1[i];
        lengthNum++;
    }
    for (int i = index; i < length2; i++) {
        num[lengthNum] = num2[i];
        lengthNum++;
    }
    return num;
}

I tried to use memcpy, but than my program doesn't work correctly (copies wrongly, but valgrind doesn't show an error).

But when I use two for loops instead, it works properly, but than valgrind shows an error

uninitialised value was created by a heap allocation.

How to use properly memcpy in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

臻嫒无言 2025-02-02 03:14:22
memcpy(num, num1, length1);
memcpy(num + length1, num2, length2 + 1); 
memcpy(num, num1, length1);
memcpy(num + length1, num2, length2 + 1); 
想你只要分分秒秒 2025-02-02 03:14:22

您的程序有多个问题:

  • malloc失败时,释放零指针是没有用的(但无害)。
  • 您分配length1 + length2 + 1字节,这很可能太大了,因为您打算从第二个字符串中的index复制。
  • 您应该使用类型size_t用于长度和偏移。
  • 您可以从num2 + index复制,而无需检查index是字符串num2内的有效偏移。
  • 您应该在新字符串的末尾设置一个空终结器。

这是一个修改后的版本:

#include <stdlib.h>
#include <string.h>

char *concat(const char *num1, const char *num2, size_t index) {
    size_t length1 = strlen(num1);

    /* skip index bytes in num2 */
    while (index --> 0 && *num2)
        num2++;

    size_t length2 = strlen(num2);
    char *num = malloc(length1 + length2 + 1);

    if (num != NULL) {
        size_t j = 0;

        while (*num1) {
            num[j++] = *num1++;
        }
        while (*num2) {
            num[j++] = *num2++;
        }
        num[j] = '\0';
    }
    return num;
}

和使用memcpy

#include <stdlib.h>
#include <string.h>

char *concat(const char *num1, const char *num2, size_t index) {
    size_t length1 = strlen(num1);
    /* skip index bytes in num2 */
    while (index --> 0 && *num2)
        num2++;
    size_t length2 = strlen(num2);
    char *num = malloc(length1 + length2 + 1);
    if (num != NULL) {
        memcpy(num, num1, length1);
        memcpy(num + length1, num2, length2 + 1);    
    }
    return num;
}

concat返回分配数组的指针。使用后,呼叫者的责任是释放此对象的责任。例如:

#include <stdio.h>

int main() {
    char *p = concat("Hello", "dear world", 4);
    if (p != NULL) {
        printf("%s\n", p);
        free(p);
    }
    return 0;
}

Your program has multiple issues:

  • freeing a null pointer when malloc failed is useless (but harmless).
  • you allocate length1 + length2 + 1 bytes, which is most likely too large as you intend to copy from index in the second string.
  • you should use type size_t for the lengths and offsets.
  • you copy from num2 + index without checking that index is a valid offset inside the string num2.
  • you should set a null terminator at the end of the new string.

Here is a modified version:

#include <stdlib.h>
#include <string.h>

char *concat(const char *num1, const char *num2, size_t index) {
    size_t length1 = strlen(num1);

    /* skip index bytes in num2 */
    while (index --> 0 && *num2)
        num2++;

    size_t length2 = strlen(num2);
    char *num = malloc(length1 + length2 + 1);

    if (num != NULL) {
        size_t j = 0;

        while (*num1) {
            num[j++] = *num1++;
        }
        while (*num2) {
            num[j++] = *num2++;
        }
        num[j] = '\0';
    }
    return num;
}

and using memcpy:

#include <stdlib.h>
#include <string.h>

char *concat(const char *num1, const char *num2, size_t index) {
    size_t length1 = strlen(num1);
    /* skip index bytes in num2 */
    while (index --> 0 && *num2)
        num2++;
    size_t length2 = strlen(num2);
    char *num = malloc(length1 + length2 + 1);
    if (num != NULL) {
        memcpy(num, num1, length1);
        memcpy(num + length1, num2, length2 + 1);    
    }
    return num;
}

concat returns a pointer to an allocation array. It is the responsibilty of the caller to free this object after use. For example:

#include <stdio.h>

int main() {
    char *p = concat("Hello", "dear world", 4);
    if (p != NULL) {
        printf("%s\n", p);
        free(p);
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文