如何正确使用memcpy与不同类型的数组 [英] How to use memcpy correctly with different types of arrays
问题描述
我有一个名为 Stack
的模板类,它有一个私有方法 resize
。
每当我使用 int
或 double
模板版本这个类,它工作按照需要。但是,每当我使用 float
或 string
版本的模板时,它会崩溃。我相信这是一个问题,我的 memcpy
函数调用。我应该如何正确使用它?
模板< class Type>
void Stack< Type> :: resize(int capacity){
if(capacity> = MAX_SIZE)
capacity = MAX_SIZE;
Type * copy = new Type [capacity];
for(int i = 0; i copy [i] = s [i];
}
s = new类型[capacity];
memcpy(s,copy,sizeof(Type)* capacity);
size = capacity;
删除副本;
}
堆分配的成员变量数组
类型
。
,使用 memcpy
复制非POD类型是不正确的。只需使用for循环,或 std :: copy
。
其次,和你有内存泄漏)。
void Stack< Type> :: resize(int capacity){
if (capacity> = MAX_SIZE)
capacity = MAX_SIZE;
Type * copy = new Type [capacity];
for(int i = 0; i copy [i] = s [i];
}
到此为止,你没关系。你已经分配了一个新数组,并从旧数组的元素中分配。我假设 N
是有效元素的数量。
s = new Type [capacity];
假设 s
数组,这是一个内存泄漏。首先,您需要删除以前的数据。
delete [] s;
然后,您不需要分配另一个数组。
s = copy;
这些功能现在看起来像这样:
template< class Type>
void Stack< Type> :: resize(int capacity){
if(capacity> = MAX_SIZE)
capacity = MAX_SIZE;
Type * copy = new Type [capacity];
for(int i = 0; i copy [i] = s [i];
}
delete [] s;
s = copy
size = capacity;
}
如果仍然有问题,代码的其他部分被破坏。 / p>
I have a templated class named Stack
that has a private method resize
.
Whenever I use the int
or double
template version of this class, it works as desired. But, whenever I use the float
or string
versions of the template, it crashes. I believe that it's a problem with my memcpy
function call. How am I supposed to use it correctly?
template <class Type>
void Stack<Type>::resize(int capacity) {
if(capacity >= MAX_SIZE)
capacity = MAX_SIZE;
Type* copy = new Type[capacity];
for (int i = 0; i < N; i++) {
copy[i] = s[i];
}
s = new Type[capacity];
memcpy(s, copy, sizeof(Type) * capacity);
size = capacity;
delete copy;
}
s
is a heap allocated member variable array of type Type
.
First of all, it is incorrect to use memcpy
to copy non-POD types. Just use a for loop, or std::copy
.
Second, you are doing more work than necessary (and you have a memory leak).
void Stack<Type>::resize(int capacity) {
if(capacity >= MAX_SIZE)
capacity = MAX_SIZE;
Type* copy = new Type[capacity];
for (int i = 0; i < N; i++) {
copy[i] = s[i];
}
Up to this point, you're okay. You've allocated a new array, and assigned over the elements from the old array. I'm assuming N
is the number of valid elements.
s = new Type[capacity];
Assuming that s
previously pointed to an allocated array, this is a memory leak. First, you need to delete the previous data.
delete [] s;
Then, you don't need to allocate another array. Use the one you just allocated.
s = copy;
All together, the function now looks like this:
template <class Type>
void Stack<Type>::resize(int capacity) {
if(capacity >= MAX_SIZE)
capacity = MAX_SIZE;
Type* copy = new Type[capacity];
for (int i = 0; i < N; i++) {
copy[i] = s[i];
}
delete [] s;
s = copy;
size = capacity;
}
If there are still problems, some other part of your code is broken.
这篇关于如何正确使用memcpy与不同类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!