我有两个基本相同的操作:
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行正/负值检查?
答案 0 :(得分:1)
不...你的删除功能不知道前面有多少malloc'ed。