1,754 questions
2
votes
2
answers
178
views
GCC warns of pointer reuse after realloc
I have this code in C11 (skipping references to pre-declared variables and functions):
// [...]
char *old_buf = it->buf;
shift += it->buf_size;
it->buf_size *= ...
0
votes
3
answers
219
views
Realloc in function that deletes a row in a matrix
I have just encountered a case in this C program:
void row_del(int**& a, int& row, int col, int k) {
for (int j = 0; j < col; j++) {
for (int i = k; i < row - 1; i++) {
...
1
vote
1
answer
180
views
realloc: invalid next size, error while reallocating memory
Can you kindly review both code fragments and tell me why shifting the line scanf("%d", (access + csize)); after if statement is solving the issue (see CODE2)? Ideally this line should give ...
3
votes
4
answers
150
views
What's wrong with this code, which gets content (chars) from file and stores it in char**?
I have a small project (ed-like text editor for myself. Just want to edit some config files using it) and I have some problems with it.
workspace_file->flc = malloc(sizeof(char *));
...
1
vote
1
answer
200
views
Getting exception "unknown Signal" with realloc in C [closed]
I have a function which I give a char** input. Inside the function I am calling realloc. I am calling the function multiple times without any issues, but it always crashes at the same point with
&...
0
votes
2
answers
174
views
Problem using realloc() to increase size of buffer in a C function
This is all developed with Visual Stodio 2022 on a Windows 10 platform.
I am having some problems getting the library function realloc() to work correctly. I have the function adjustRGBA(), which ...
1
vote
1
answer
141
views
Getting C++ memory allocation pointing to same location which crashes the program
I am programming an RP2040 chip using the Pico-Arduino library. In this problem, I am using the Ardunio String class and the Adafruit_NeoPixel class. After setting up some "Strings" and a ...
-6
votes
1
answer
129
views
How to realloc a null pointer? [closed]
I have an array of pointers that I declared like this:
struct vallue {has variables but it shouldn't be important atm};
struct array {
struct vallue** array_of_pointers_to_structs;
int size;
}...
0
votes
2
answers
162
views
Is this truly best way to delete last element in C? [closed]
I have a code where I need to delete last element of array. I made this code but I am wondering is there a more optimised way of doing this?
My version:
void DeleteLast(int** array, int* size)
{
...
1
vote
0
answers
75
views
Handling multiple memory mangers in C++
My C++ application is using own library for memory management. It defines malloc and free and it is statically linked. My application uses few other 3rd party libraries also. I got a crash in one of ...
2
votes
1
answer
101
views
realloc function dynamic array size more than stated
I am trying to add memory of my dynamic array using realloc. But in my code, realloc is added extra memory to the array.
Suppose initially I want to create array of 5 items than I want to add 3 more ...
0
votes
2
answers
132
views
Why does realloc(NULL, size) call my malloc wrapper, but realloc(ptr, 0) does not call my free wrapper? [closed]
I have wrapped my dynamic memory functions (malloc, calloc, realloc, and free) to track memory usage in my program.
I have encountered the following:
Calling realloc(NULL, 5) goes directly to my ...
0
votes
1
answer
142
views
realloc fails when running ./out.exe via Makefile, but works when run manually
I'm having an issue where, when I run my already compiled program ./main.exe (gcc main.c -o main.exe) through a Makefile, a realloc call fails when the size goes over 40 bytes. However, if I run ./...
0
votes
1
answer
73
views
How realloc works passed 0 size as argument
How realloc works passed 0 size as argument?
from man page:
Unless ptr is NULL, it must have
been returned by an earlier call to malloc(), calloc(), or realloc().
Why it needs to be?
compile this ...
2
votes
1
answer
102
views
Issue with using realloc - not getting expected amount of memory [duplicate]
I have to write a program that is able to take in a file of grades, read them, and then provide some options to be able to edit that file. I decided to write it in C (for the challenge, and to learn ...
0
votes
3
answers
91
views
How can i do a double pointer (char **ptr) like an array of infinity pointers (pointers with no length limit)?
Sorry for my bad English. I have an exercise from school. I need to teach my friends to work with pointers in C. So i want to narrate them about a double pointer (type **ptr). I tried to write a code ...
0
votes
1
answer
114
views
Is there any way at all to get the size of a dynamic array with sizeof()? [duplicate]
I'm a computer science student and one of the questions on a test asked to implement realloc in C using malloc. My teacher said the correct answer would be to get the sizeof of the previous array and ...
1
vote
1
answer
114
views
Realloc fails, but only when a printf statement is used: "invalid next size"
I am trying to write a ThreadPool library for C; mostly for educational purposes, but also perhaps for practical usage later. I have an implementation that "works", in that I can spin up ...
0
votes
2
answers
80
views
realloc zeroes parts of previously allocated memory [closed]
As I know, realloc is supposed to make a bigger/smaller allocation and copy the data given to it here. I was making a sprisheet atlas parser and encountered this, then reproduced it with very simple ...
0
votes
2
answers
144
views
Is it efficient to use realloc to free up small chunks of memory C++ [duplicate]
I'm writing an application that generates a bunch of n strings (bit)length l and then does a comparison between all possible pairs.
I first allocate memory for n strings of length l
char* pStr = (char*...
0
votes
1
answer
71
views
Getting error: "munmap_chunk(): invalid pointer" for the following code
compiled the following code snippet with command: gcc -g -std=c99 src.c
src.c:
#include <stdio.h>
#include <stdlib.h>
void fill(int* arr, int len, int val) {
for (int i = 0; i < ...
0
votes
1
answer
85
views
C Double Pointer Reallocation program hangs upon being ran twice in a row
Whenever I try to run this code I've written, it works fine the first time. The second time, the execution hangs up at the end ( it executes everything in the function, but never exits the function ). ...
0
votes
0
answers
86
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[...
32
votes
2
answers
1k
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
131
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
57
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?
...
1
vote
0
answers
55
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
143
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
98
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
90
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
135
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
79
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
94
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
96
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
382
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
341
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 ...
1
vote
1
answer
74
views
Value of n sets to 0 or 1 (undefined behavior) while I'm trying to learn use of realloc and malloc
I'm working on a C program to create an interactive interface for a dynamic array of strings. The program is menu-driven and follows these steps:
Asks the user for the initial length of the array.
...
1
vote
2
answers
2k
views
Why am I getting a heap-use-after-free error?
Why am I getting a segmentation fault? When I compile with sanitize=address I get a heap-use-after-free which I don't quite understand (the reason for).
I get heap-use-after-free on address xyz.
Read ...
0
votes
1
answer
113
views
Error during memory reallocation inside a function
I wrote a function to append pointer to an object to array, but realloc can't properly reallocate memory when it's called from a nested function. However, when I'm reallocating memory from function, ...
2
votes
1
answer
131
views
Segmentation fault by realloc, while creating dynamic generic array
I tried to implement a dynamic generic array. However, when I tested my code, the result I got was "segmentation fault". I know that this error happens by realloc in a function ...
-1
votes
1
answer
97
views
why is my malloc(1) segfaulting my program?
I have a problem in my C program that makes it so that it segfaults on malloc(1). I've spent many hours trying to find out why it segfaults but
I just can't figure it out. This portion of my program ...
1
vote
2
answers
72
views
Using realloc with pointer to pointer in another function
I'm trying to use realloc in this program to increase an array's size, using pointer to pointer. But it doesn't end up doing the task:
#include <stdio.h>
void push(int** data)
{
*data = (...
1
vote
1
answer
144
views
Segmentation Fault when assigning value to a pointer after `realloc`
I am very new to C (started 4 days ago, do have a background in Python and Java). I tried to create a dynamic array (python list) to solve advent of code day 4 part 2. The own list and win list works ...
0
votes
2
answers
130
views
Dynamically expandable array in c
I wanted to expand an array dynamically as the user enters the values using below code.
#include<stdio.h>
#include<stdlib.h>
int main(){
int *number=(int *)malloc(sizeof(int)),i=0,*...
1
vote
1
answer
61
views
c stack (using dynamic array) realloc memory leak problem
#include <stdio.h>
#include <stdlib.h>
int LENGTH = 1;
int TOP = -1;
void push(char**, char);
char pop(char*);
char peek(char*);
int isEmpty();
int isFull();
void convertToRPN(char*, ...
0
votes
1
answer
49
views
segfault trying to find bottom nodes of tree and adding to dynamic array in C
when calling init_model() function I am trying to recursively travel through a tree and append the pointers to the lowest treenodes in an array of treenode pointers. however I am getting segfault when ...
0
votes
0
answers
83
views
Minimizing realloc calls in a custom dynamically scaled array module
I'm developing a custom dynamically scaled array module that aims to minimize the number of realloc calls while maintaining efficient memory usage. The module utilizes a strategy of doubling the ...
0
votes
0
answers
49
views
C: realloc(): invalid next size when reallocating vector
I'm implementing a dynamic unsigned array struct (equivalent to C++ std::vector) and I'm facing some issues when reallocating for push_back.
Here is the relevant code:
#include <stdio.h>
#...
0
votes
1
answer
131
views
realloc() : invalid next size Aborted (core dumped)
I'm Tryng to write a code that have to expand the memory of a malloc array of 1 for some cicles of a loop (i need to store the zeros of a function). but after compiling this error occurs:
realloc(): ...
0
votes
1
answer
180
views
Why +1 in realloc for string in C? If it's for null terminator, then how does strlen() measures len of the str if there's no null byte at the end? [duplicate]
This code is from a problem in HackerRank (Q: Printing Tokens in C).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char *s = malloc(1024 * sizeof(char))...