gpt4 book ai didi

c - 通过 'insert bytes' 使 'remove bytes' 和 'insert bytes' 一起工作,并具有正/负偏移量

转载 作者:行者123 更新时间:2023-11-30 16:02:48 30 4
gpt4 key购买 nike

我有两个基本相同的操作:

insert_bytes(from, count)
delete_bytes(start, stop) -> delete_bytes(from, count)

insert_bytes 实现示例:

unsigned char* new_bytes = (unsigned char*)malloc((old_length+count)*sizeof(unsigned char));
memcpy(new_bytes, old_bytes, from); // negative value can't go to from here
memcpy(new_bytes+count, old_bytes+from, old_length-from);
return new_bytes+from; // pointer to write

有没有安全的方法来实现delete_bytes作为对insert_bytes(带有负偏移量)的调用,而无需编写5-6行正/负值检查?

最佳答案

不...您的删除函数不知道之前分配了多少内存。

  • 如果您创建了一个 C++(这被标记为 C)类,您可以封装当前大小。
  • 您可以创建一个函数:resize(from, current_size, delta),这样就可以了。

关于c - 通过 'insert bytes' 使 'remove bytes' 和 'insert bytes' 一起工作,并具有正/负偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846621/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com