TJCLK
TJCLK

Reputation: 183

Use realloc() after malloc() to change the size of unsigned char array

In the main function, I use malloc() to create an unsigned char array:

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

change_size() defined in .h:

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

In the change_size function, I will use realloc() to increase the array size:

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);  
}

The printf() show me:

64
80
8

The array size in the main() also needs to be updated.

Then how to change this array size correctly?

Upvotes: 0

Views: 1714

Answers (2)

J CHEN
J CHEN

Reputation: 494

First C is not babysitter language, You only need basic things then you can do everything, Just try hard to totally understand basic.

#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;
}

Okay I answer it. @Chipster

#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;
}

Upvotes: 0

paddy
paddy

Reputation: 63481

You need to pass your parameters as pointers if you want to change their value back in the caller. That also means you pass your array pointer as a pointer, because realloc might change it:

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;
}

Here I've modified change_size to suit, and also added a return value to indicate success, since realloc can fail to resize the memory. For clarity, I removed the printf calls. Oh, and I also removed the cast, since that is not valid in C.

Example usage:

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

One final note is that you can use your change_size function for the first allocation too, rather than calling malloc. If the first argument to realloc is NULL, it does the same thing as malloc.

Upvotes: 2

Related Questions