动态分配内存和i不能使用calloc或realloc



我还是个初学C的人。我需要创建一个名为resizeallocc的函数,它返回指向void的类型指针,并有三个参数。第一个是pOld,类型指向void,第二个和第三个是size_t类型,名称为newSize,oldSize

ResizeAlloc动态分配一个包含newSize字节的全新内存块,或者实际上,将pOld中包含oldSize字节的现有块的大小调整为包含newSize字节。当调整大小发生时,将保留所有适合newSize字节的现有数据。ResizeAlloc不能调用callocrealloc,或者任何你知道会调用它们的函数或宏。

还给出了以下推荐实现:

if newSize is zero
return a null pointer.
Else
Dynamically allocate a new block containing newSize UNINITIALIZED bytes.
If the allocation fails
Return a null pointer.
Else If pOld is a null pointer 
Return a pointer to the new block.
Else
If newSize is greater than oldSize
Copy oldSize bytes from pOld to the new block.
Else
Copy newSize bytes from pOld to the new block.
Free pOld
Return a pointer to the new block.

所以使用上面的实现,我如何创建类似于reallocmalloc的代码,而不实际使用它们?下面是所提供的代码,它调用了我应该创建的ResizeAlloc函数。

我失去了我们如何动态分配一个包含newSize未初始化字节的新块,正如上面的实现所示,让我开始。

提供代码:

#define INSTRUCTOR_FILE    // DO NOT DEFINE THIS MACRO IN ANY FILES YOU CREATE
/***  YOU DO NOT NEED TO UNDERSTAND THE CODE IN THIS FILE TO WRITE YOURS  ***/
/******************** DO NOT MODIFY THIS FILE IN ANY WAY ********************/
/******************** DO NOT MODIFY THIS FILE IN ANY WAY ********************/
/******************** DO NOT MODIFY THIS FILE IN ANY WAY ********************/
//****************************************************************************
// Everything in this file was written to help test/verify your code and must
// not be altered in any way.  Do not rename this file or copy anything from
// it into your file(s).  This file does not necessarily represent good coding
// technique, proper formatting/style, or meet the requirements your code must
// meet.  You do not need to understand the code in this file to write yours.
//****************************************************************************
#ifdef INSTRUCTOR_FILE
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define INIT_ARRAY_COUNT 128
void *ResizeAlloc(void *pOld, size_t newSize, size_t oldSize);
//
// Test: ResizeAlloc
//
int main(void)
{
int tests = 0, errors = 0;
signed char *initArray, *initArray1;
signed char *cp;
signed char value;
int ix;
++tests;
#if 0
#define INIT_ARRAY_COUNT_FAIL UINT_MAX
// Check for intentional allocation failure.
initArray = ResizeAlloc(NULL, INIT_ARRAY_COUNT_FAIL, 0);
if (initArray)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Array allocation "
"failure not detected when size was too large.nn");
free(initArray);
++errors;
}
#endif
// Check for intentional allocation rejection.
++tests;
initArray = ResizeAlloc(NULL, 0, 0);
if (initArray)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Array allocation "
"rejection not detected when 2nd argument was 0.nn");
free(initArray);
++errors;
}
// Check for successful allocation.
++tests;
initArray = ResizeAlloc(NULL, INIT_ARRAY_COUNT, 0);
if (!initArray)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Array allocation "
"failed.nn");
++errors;
}
// Check for correct write/read.
++tests;
value = SCHAR_MIN;
// Initialize all elements.
for (cp = initArray; cp < initArray + INIT_ARRAY_COUNT; ++cp)
*cp = value++;
value = SCHAR_MIN;
ix = 0;
// Test all elements.
for (cp = initArray; cp < initArray + INIT_ARRAY_COUNT; ++cp)
{
if (*cp != value++)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Incorrect value at "
"element %d.nn", ix);
++errors;
}
++ix;
}
// Check for correct size increase.
++tests;
initArray1 =
ResizeAlloc(initArray, INIT_ARRAY_COUNT * 2, INIT_ARRAY_COUNT);
// Initialize upper elements.
value = SCHAR_MAX;
++tests;
for (cp = initArray1 + INIT_ARRAY_COUNT;
cp < initArray1 + INIT_ARRAY_COUNT * 2;
++cp)
{
*cp = value--;
}
value = SCHAR_MIN;
// Test lower elements.
ix = 0;
for (cp = initArray1; cp < initArray1 + INIT_ARRAY_COUNT; ++cp)
{
if (*cp != value++)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Incorrect value at "
"element %d.nn", ix);
++errors;
}
++ix;
}
value = SCHAR_MAX;
// Test upper elements.
ix = INIT_ARRAY_COUNT / 2;
for (cp = initArray1 + INIT_ARRAY_COUNT;
cp < initArray1 + INIT_ARRAY_COUNT * 2;
++cp)
{
if (*cp != value--)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Incorrect value at "
"element %d.nn", ix);
++errors;
}
++ix;
}
// Check for correct size reduction.
++tests;
initArray =
ResizeAlloc(initArray1, INIT_ARRAY_COUNT / 2, INIT_ARRAY_COUNT * 2);
value = SCHAR_MIN;
// Test 1/2 of lower elements.
ix = 0;
for (cp = initArray; cp < initArray + INIT_ARRAY_COUNT / 2; ++cp)
{
if (*cp != value++)
{
fprintf(stderr, "***ERROR*** ResizeAlloc: Incorrect value at "
"element %d.nn", ix);
++errors;
}
++ix;
}
free(initArray);
const char *plural = errors != 1 ? "s" : "";
if (errors)
fputc('n', stderr);
fprintf(stderr, "C2A1E5: %d tests %d error%s.n",
tests, errors, plural);
return 0;
}
#endif

我在启动和动态分配一个包含未初始化字节newSize的新块时遇到了麻烦。

您可以使用malloc()函数来分配未初始化的内存。

最新更新