[Solved] How to use realloc in a function in C

You want to modify the value of an int* (your array) so need to pass a pointer to it into your increase function: void increase(int** data) { *data = realloc(*data, 5 * sizeof int); } Calling code would then look like: int *data = malloc(4 * sizeof *data); /* do stuff with data */ increase(&data); … Read more

[Solved] How to grow a pointer or an array in C at runtime (without knowing the end length at compile time)

OP’s mostly has it. It is just printing the first element each time. // printf(“%d”, *arr); printf(“%d”, arr[n-1]); is it possible to do it with an array? No. In C an array cannot change size once it is defined. The memory size allocated and referenced by a pointer can change though. solved How to grow … Read more

[Solved] How to shrink at runtime a struct matrix (using realloc() for example)

You should reduce the number of columsn, not rows. Not mat1 but mat1->array should be reallocated. Not nrow and ncol (not updated) but mat1->nrows and mat1->ncols (updated) should be used for the new size. The elements are size_t, so allocating for int mayn’t be enough. Using the variable for calculating size is safe. In the … Read more

[Solved] malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated

The function is not updating the caller’s value of client_details, you need one more level of pointer: void func(struct client_detail **client_details,int *size){ *client_details = realloc(*client_details, (*size + 1) * sizeof(**client_details)); *size = *size + 1; } int main() { struct group_detail *group_details = malloc(0 * sizeof(*group_details)); struct client_detail *client_details = malloc(1 * sizeof(*client_details)); int size … Read more

[Solved] malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated

Introduction The malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated is a common error encountered when attempting to reallocate memory in C programming. This error occurs when a pointer is passed to the realloc() function, but the pointer was not allocated with malloc() or calloc() beforehand. This error can be caused … Read more

[Solved] Run-time error: *** glibc detected *** [closed]

Let’s look at the first two lines of your code: str = (char *) malloc(15); strcpy(str, “63tczfqV4nqB2YnH9iFJbGvGyyDkvK341rrj0G0mo1PEYniOVHejVIFIQnJzHSSMRbuyGMCZ4M5HFMV4y1q4QgYqyxp2XkTjxaolKTkaw1r25S2Emz061tw1”); At this point, you have broken the rules of the C language. strcpy will write past the end of str which causes undefined behavior. Everything that happens after this point is kinda up in the air. 5 solved … Read more

[Solved] C: allocate a pointer

head=malloc(sizeof(node*)); Will allocate a space in memory to hold a POINTER to a object of type node. head=malloc(sizeof(node)); Will allocate space for the structure itself, and return to you the address of that memory space, and you will hold that in the head variable For more info, check this other question about Malloc. How do … Read more