Questions tagged [realloc]
C library function for reallocating a dynamically allocated memory region.
realloc
1,738
questions
0
votes
0
answers
67
views
How do I solve "realloc(): invalid next size" error?
Here is the section of code where my program crashes:
if (fromNode != toNode) {
pthread_mutex_lock(&mtxs[fromNode]);
g->out[fromNode]++;
if (g->in[toNode].index == g->in[...
17
votes
0
answers
319
views
realloc(ptr, 0) in C23 - now what?
According to the change log of C23, proposal n2464 was voted-in and implemented, making realloc(ptr, 0) explicitly undefined behavior, whereas it was supposedly implementation-defined in previous ...
0
votes
1
answer
103
views
How to free memory for an array allocated by malloc?
I have 2 variants of code:
1st:
void PrintMem(const int* memarr,const size_t size) {
for (size_t index = 0; index < size; ++index) {
std::cout << '<'<<(index+1)<<&...
1
vote
0
answers
43
views
Downsizing an array using realloc() [duplicate]
In C you can do something like this:
int size = 10;
int *foo = (int*)calloc(size, sizeof(int));
size = 5;
foo = realloc(foo, size * sizeof(int));
So where does the memory for cropped 5 elements go?
...
0
votes
0
answers
19
views
Losing the first element of a pointer due to bad memory allocations (i think)
First of all, sorry for the long code block. This is my current project where I need to manipulate graphs that simulate routes between locations. However, when I try to add a new node to the graph, it ...
1
vote
0
answers
30
views
Detecting dangling pointer caused by realloc using static analyze tools
I use some external lib written in C that implement hash table.
When hash table size need to grow it uses realloc to double the memory space for keys/values.
I familiar with this behavior but others ...
0
votes
2
answers
83
views
Problem using realloc() after calloc().Getting runtime error I do not know how to fix
I have tried solving this problem by googling about calloc() and realloc().
I saw videos about it.I read similar cases in stackoverflow but due to C++ and struct usage I had trouble understanding how ...
0
votes
1
answer
83
views
Realloc doesn't shrink memory? (C)
I have this simple code snippet as seen below:
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
int *a, *a2;
size = 5;
a = malloc(size * sizeof(int));
if(...
-2
votes
1
answer
85
views
I'm encountering a puzzling issue in my C program while working with dynamic memory allocation using realloc() [closed]
I'm encountering an issue while reading an file that contains records with length indicators and fields separated by '|'. In this program, I'm reading information about the quantity and price of a ...
0
votes
4
answers
107
views
What realloc() actually does in this code?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
...
0
votes
1
answer
73
views
Dynamic memory allocation in c arrays of structs [closed]
void addWorkerToProject(Worker *worker, Project *project) {
worker->projects = malloc(sizeof (strlen(project)+1));
worker->projects[worker->projectCount]->name = project->name;...
1
vote
1
answer
86
views
Faced problem to add a new row to a matrix in C. corrupted size vs. prev_size [duplicate]
I am trying to develop a linear algebra library from scratch for various matrix operations. I faced a problem while adding a new row to a matrix. I store the matrix using the Column major formula, and ...
1
vote
1
answer
55
views
Combined usage of getline, strcat and realloc functions
Good afternoon everyone! I wrote my implementation of the cat utility in C and ran into some problems. Here's my code:
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#...
0
votes
2
answers
162
views
Using realloc() instead of fixed-length buffers in c?
I have some code that was written to be as conservative as possible with memory use, so it does things like use realloc() for building strings a character at a time instead of a one-time fixed-length ...
0
votes
1
answer
152
views
"double free detected in tcache 2" Error while reallocating a pointer to a dynamic array of strings
I have written this code to store a dynamic array of strings on which different operations can be performed. It works correctly when I enter the strings initially and when I add 2 strings, but on the ...