- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个使用 DMA 的函数 (changeSize),我可以在其中选择将其(数组)大小更改为我想要的任何大小,其中 oldEls 是原始大小,newEls 是新大小。如果 newEls 大于 oldEls,我将在末尾添加零,如果它小于 oldEls,我将截断。 “ptr”参数需要指向新数组。据我了解,这类似于 C 语言的 realloc() 函数。
使用下面的代码,我输出以下内容:0, 0, 3, 6, 0, 0, 0, 0,其中正确的输出应该是 4, 2, 3, 6, 0, 0, 0 , 0。我还意识到我的 show 函数可能不是输出新数组的最佳函数,因为我必须明确说明数组元素的大小。
提前致谢。
#include <iostream>
#include <cstdlib>
using namespace std;
void show( const int a[], unsigned elements );
int * copy( const int a[], unsigned els );
void changeSize( int * & ptr, int newEls, int oldEls );
void die(const string & msg);
int main()
{
int arr[4] = {4, 2, 3, 6};
show(arr, 4);
int * newArr = copy(arr, 4);
cout << endl << endl;
changeSize(newArr, 8, 4);
show(newArr, 8);
}
void show( const int a[], unsigned elements )
{
for (int i = 0; i < elements; i++)
cout << a[i] << endl;
}
int * copy( const int a[], unsigned els )
{
int *newArr;
try
{
newArr = new int[els];
}
catch(const bad_alloc &)
{
die("Copy: Alloc Failure");
}
for (int i = 0; i < els; i++)
newArr[i] = a[i];
return newArr;
}
void changeSize( int * & ptr, int newEls, int oldEls )
{
int * newArr;
try
{
newArr = new int[newEls];
for (int i = 0; i < oldEls; i++)
{
newArr[i] = ptr[i];
}
if (newEls > oldEls)
{
for (int k = oldEls; k < newEls; k++)
newArr[k] = 0;
}
}
catch(const bad_alloc &)
{
die("changeSize: Alloc Failure");
}
ptr = newArr;
delete[] newArr;
}
void die(const string & msg)
{
cerr << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
最佳答案
首先,您在 changeSize
末尾对 newArr
调用 delete。您需要删除 ptr 的旧值(您当前丢弃的)。这就是(可能)问题
虽然我在这里,但我想指出您对 std::vector
的兴趣。它基本上是一个可调整大小的数组。
此外,复制原始内存块仍然最好使用 memcpy
完成,不要浪费时间编写 for 循环来复制 int
s,只对 C++ 类执行此操作。
编辑:使用std::copy
是C++ 的最佳解决方案,它尽可能使用memcpy
,否则与复制对象的 for 循环。
干杯!
关于C++:使用动态内存分配编写类似于 C realloc() 函数的函数(即更改其大小),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096154/
如果 realloc 失败并返回 NULL 是前一个缓冲区被释放还是保持不变?我没有在手册页中找到那条特定的信息,我不确定该怎么做。如果内存被释放,那么双重释放可能会有风险。如果没有,就会发生泄漏。
OS: Linux CC: GCC 4.8.2 目标:改变 char* 的大小 -> 变小 问题:更改后的大小相同... 行是带有数据的字符串... 代码片段: char * tmp = NUL
在一个函数中我使用了 malloc : void name1(struct stos* s) { s = malloc (4 * sizeof (int)); } 一切正常。但是后来我用了rea
我知道有一个 realloc允许我调整内存块大小的函数(它与一个免费函数配对)。但是,我正在尝试对一些成员指针使用 new 而不是 realloc 分配内存的 c++ 类执行相同的操作。在 C++ 中
我正在尝试在 C 中创建一个动态整数数组,它应该在填满后自动将其大小加倍。 要扩展数组的大小,我想使用 realloc 函数。不幸的是,指向我的 DynamicArray 和 GCC 崩溃的数据的指针
这是我被教导使用的方式 realloc() : int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a
我尝试在每个循环中使用 realloc(),因此我只为 C 中的 int 数组使用必要的内存,但输出值已更改。尽管如此,在我的代码中使用 Valgrind 时,我得到了正确的值。 我在做 Advent
平台:Linux 3.2.0 x86 (Debian Wheezy) 编译器:GCC 4.7.2 (Debian 4.7.2-5) 我想知道如果我尝试 realloc() 一个已递增的指针会发生什么。
我知道可以在内核中使用 malloc 在 GPU 的全局内存上分配内存。是否也可以使用realloc? 最佳答案 您可以为您的数据类型编写自己的 realloc 设备函数。 只需为新数组分配新空间,将
我在对数组使用 malloc/realloc 命令时遇到了一些问题。我创建了一个包含一些整数的小数组,并尝试通过使用 realloc 扩展大小并添加值来为其添加一个值,但是当我这样做时,0 索引的值不
背景: 我使用 calloc() 创建了一个数组,一切都运行良好。然后我使用 realloc() 使数组更大。它似乎只是创建一个没有任何内容的新指针,并在我尝试访问数组中的元素时调用运行时错误。 我的
假设我已经使用 malloc() 分配了内存,如果我在我的代码中这样做: char *newline = realloc ( oldline , newsize ); // Assuming oldl
我正在尝试在下面的程序中使用 realloc 重新分配内存,并在我使用 malloc(i = (int*)malloc(5 * sizeof(int))) 使用react的 realloc 初始内存之
为什么下面的代码输出两次 4,而不是 8 和 20?谢谢 int size = 0; int *pointer; pointer = malloc(2 * sizeof(int)); size = s
我正在尝试将一堆 WCHAR 添加到缓冲区。这个函数就是将它添加到我的缓冲区中的原因.. DWORD add_to_buffer(BYTE *databuffer, WCHAR *path, WCHA
可能我的大脑现在不能正常工作......我想知道为什么我在我的代码中收到提到的错误: int ** zm; zm = (int**)calloc(1, sizeof(int*)); *zm = (in
我正在尝试用 C 语言编写代码,但遇到了 realloc 的问题。该代码在某个时间点工作正常,但在另一时间重新分配期间因堆损坏错误而崩溃。我已将填充数据的结构和函数粘贴到其中。谁能告诉我我是否在这里做
realloc 会改变它的第一个参数吗? 改变第一个参数是否取决于实现? 有什么理由不应该是const吗?作为反例,memcpy 将其 src 参数设为 const。 ISO C 标准,第 7.20.
我在 realloc 中遇到此错误,该错误仅发生在我学校的实验室计算机上,而不发生在我的计算机上。 在此程序中,我将行号存储在 File_Node 结构中。 File_Node 是一个链表的一部分,每
来自 man realloc:realloc() 函数返回一个指向新分配的内存的指针,该指针适合任何类型的变量,可能与 ptr 不同,如果请求失败,则返回 NULL . 因此在此代码片段中: ptr
我是一名优秀的程序员,十分优秀!