C++中的内存操作之memset和memcpy

在 C++ 中提供了两个库函数——memsetmemcpy,来进行内存的操作。

memset 用于将一块内存设置为指定的值,而 memcpy 则用于从源位置复制一块内存到目标位置。

例如:

#include <cstring>
#include <iostream>

int main() {
    int arr[5] = {0, 1, 2, 3, 4};
    std::cout << "Before memset: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    memset(arr, 0, sizeof(arr));

    std::cout << "After memset: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    int arr2[5];
    memcpy(arr2, arr, sizeof(arr));

    std::cout << "After memcpy: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << arr2[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

在上面的例子中,我们首先用一些数字来初始化了整型数组arr,然后将这些数字进行打印;之后调用了memset,并将参数设为0,这意味着将之前设置好的数字又全部用0值进行了覆盖;最后,我们使用memcpy来将数组arr的内容全部拷贝到了数组arr2里,然后再进行打印,自然arr2的值也就是arr里的值,即全部为0。

尽管 memsetmemcpy都是功能非常强大的函数,但是它们的行为是低级别的行为(操作内存),如果使用不当的话可能会导致内存上出现问题。必须确保被操作的内存是有效的,并且所操作的内存块的大小是正确的,这类情况需要格外的小心。此外,如果正在处理的是具有构造函数或析构函数的对象,则应该使用 C++ 特定的构造函数和赋值运算符,而不是使用 memset 和 memcpy 函数。

更具体的来说,就是涉及到自定义类型(用class关键字申明的类型)的时候:

  • 使用构造函数初始化:可以使用构造函数将对象初始化为默认值,而不是使用 memset 来初始化。
#include <iostream>

class MyClass {
public:
    int myInt;
    MyClass() : myInt(0) {} // Constructor to initialize myInt to 0
};

int main() {
    MyClass obj;
    std::cout << "myInt = " << obj.myInt << std::endl; // Output: myInt = 0
    return 0;
}

  • 拷贝构造函数:可以使用拷贝构造函数创建一个新对象,该对象是现有对象的副本,而不是使用 memcpy 来复制一个对象。
#include <iostream>

class MyClass {
public:
    int myInt;
    MyClass(int value) : myInt(value) {} // Constructor to initialize myInt with a value
    MyClass(const MyClass& other) : myInt(other.myInt) {} // Copy constructor to copy the value of myInt from another object
};

int main() {
    MyClass obj1(42);
    MyClass obj2 = obj1; // Copy constructor called to create obj2 from obj1
    std::cout << "obj2.myInt = " << obj2.myInt << std::endl; // Output: obj2.myInt = 42
    return 0;
}

  • 赋值运算符:可以定义一个赋值运算符来将一个对象的内容复制到另一个对象中,而不是使用 memcpy 来复制对象内容。
#include <iostream>

class MyClass {
public:
    int myInt;
    MyClass(int value) : myInt(value) {} // Constructor to initialize myInt with a value
    MyClass& operator=(const MyClass& other) {
        myInt = other.myInt; // Copy the value of myInt from the other object
        return *this;
    }
};

int main() {
    MyClass obj1(42);
    MyClass obj2(0);
    obj2 = obj1; // Assignment operator called to copy the value of obj1.myInt to obj2.myInt
    std::cout << "obj2.myInt = " << obj2.myInt << std::endl; // Output: obj2.myInt = 42
    return 0;
}


总结来说,在可能的情况下,使用适当的 C++ 构造函数和赋值运算符总是更安全和更表达性的选择,低级函数 memsetmemcpy虽然功能强大,但是使用不当会导致内存级别的错误。对其功能建立认知,对其使用心怀谨慎。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Claude的羽毛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值