【问题标题】:Use realloc() after malloc() to change the size of unsigned char array在 malloc() 之后使用 realloc() 更改 unsigned char 数组的大小
【发布时间】:2019-12-03 05:36:56
【问题描述】:

在 main 函数中,我使用 malloc() 创建一个 unsigned char 数组:

int main()
{
  int length = 64;
  unsigned char *array = (unsigned char *)malloc(length * sizeof(unsigned char));
  ...
  change_size(array, length);
}

在.h中定义的change_size():

void change_size(unsigned char* arr, int len);

在change_size函数中,我会使用realloc()来增加数组大小:

void change size(unsigned char* arr, int len)
{
  printf("%d\n", len);
  len = len + 16;
  printf("%d\n", len);
  arr = (unsigned char *)realloc(arr, len * sizeof(unsigned char));
  int new_len = sizeof(arr)/sizeof(arr[0]);
  printf("%d\n", new_len);  
}

printf() 告诉我:

64
80
8

main() 中的数组大小也需要更新。

那么如何正确改变这个数组大小呢?

【问题讨论】:

  • 请注意,realloc 可以返回 NULL,但如果失败,则原始指针保持不变。
  • 什么是ciphertext,它是如何创建的?
  • @ToninGuy3n:我将密文更改为 arr,错字
  • sizeof(arr)/sizeof(arr[0]); 只是计算 actual 数组元素数量的有效方法。由于arr 是指针,而不是数组,所以sizeof(arr) 是指针的大小。
  • 顺便说一下,这感觉更像是 C 代码而不是 C++ 代码,但我可能有点挑剔。

标签: c arrays malloc realloc


【解决方案1】:

First C 不是保姆语言, 你只需要基本的东西,然后你就可以做任何事情, 只要努力完全理解基本知识。

#include <stdio.h>
#include <stdlib.h>



int main(){
    int G1_Len=20;
    int G2_Len=40;
    char* G1=(char*)malloc(sizeof(char)*G1_Len);
    char* G2=(char*)malloc(sizeof(char)*G2_Len);
    printf("This is G1's Size:%d,Becuz G1 is Pointer\n",sizeof(G1));
    printf("%d\n",sizeof(G2));

    printf("This is what you need just add a variable remainber your size\n%d\n",G1_Len);
    printf("%d\n",G2_Len);
    /*alloc and free is a pair of memory control you need,remember least function thinking more is tip of C*/
    /*if you need alot of function but you cant control all try c++*/
    /*and if in c++ using new and delete dont use malloc free*/
    free(G1);
    free(G2);
    G1=NULL;
    G2=NULL;



    G1_Len=22;
    G1=(char*)malloc(sizeof(char)*G1_Len);
    //Now you have 22 bytes of char array




    free(G1);



    return 0;
}

好的,我来回答。 @奇普斯特

#include <stdio.h>
#include <stdlib.h>

int change_size(char** arr, int len)
{
    char* nar=(char*)malloc(sizeof(char)*(len+16));
    if(nar){
        free(* arr);
        *arr=nar;
        nar[10]='K';//this will let you know its right
        return len+16;
    }
    return len;
}

int main(){
    int G1_Len=20;
    int G2_Len=40;
    char* G1=(char*)malloc(sizeof(char)*G1_Len);
    char* G2=(char*)malloc(sizeof(char)*G2_Len);
    printf("This is G1's Size:%d,Becuz G1 is Pointer\n",sizeof(G1));
    printf("%d\n",sizeof(G2));

    printf("This is what you need just add a variable remainber your size\n%d\n",G1_Len);
    printf("%d\n",G2_Len);
    /*alloc and free is a pair of memory control you need,remember least function thinking more is tip of C*/
    /*if you need alot of function but you cant control all try c++*/
    /*and if in c++ using new and delete dont use malloc free*/
    free(G1);
    free(G2);
    G1=NULL;
    G2=NULL;



    G1_Len=22;
    G1=(char*)malloc(sizeof(char)*G1_Len);
    //Now you have 22 bytes of char array


    printf("%d\n",G1);
    G1_Len=change_size(&G1,G1_Len);
    printf("%c\n",G1[10]);
    printf("%d\n",G1);
    printf("%d\n",G1_Len);
    free(G1);



    return 0;
}

【讨论】:

  • 我的意思是,C++ 不会牵你的手,但你可能不想这样回答。此外,好的答案试图解释为什么某些事情会起作用或发生。尝试将此纳入您的答案中。
  • 此答案并未尝试解决原始问题中的任何要点。相反,它似乎走上了一些奇怪的切线,提供建议,因为 cmets 埋在一个甚至与原始代码无关的代码块中。
【解决方案2】:

如果您想将参数值更改回调用方,则需要将参数作为指针传递。这也意味着您将数组指针作为指针传递,因为realloc 可能会更改它:

int change_size(unsigned char **arr, int *len)
{
    int new_len = *len + 16;
    unsigned char *new_arr = realloc(*arr, new_len);
    if (new_arr) {
        *len = new_len;
        *arr = new_arr;
    }
    return new_arr != NULL;
}

这里我修改了change_size 以适应,还添加了一个返回值来表示成功,因为realloc 可能无法调整内存大小。为清楚起见,我删除了 printf 调用。哦,我还删除了演员表,因为这在 C 中无效。

示例用法:

if (!change_size(&array, &len))
{
    perror("change_size failed");
}

最后一点是,您也可以使用change_size 函数进行第一次分配,而不是调用malloc。如果realloc 的第一个参数为NULL,它的作用与malloc 相同。

【讨论】:

  • 我应该如何在 main() 中声明 len 和 arr?
  • 他们现在的样子。我在示例用法中打错了字。它应该是 length 以匹配您在 main 中使用的名称。
  • 哦,我明白了我的问题,我应该调用 change_size(&array, len)。如果我也更改声明 int *length,我应该调用 change_size(&array, &len),是吗?
  • 没有。你问我如何定义这些值。我说“完全一样”,而你似乎将其解释为“不同”。我的意思是没有变化您当前已在main 中定义它们。您使用&amp; 获取指针。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
相关资源
最近更新 更多