565 questions
2
votes
0
answers
56
views
zero-ing PSRAM apparently consumes all memory
I'm struggling with an ESP32 library.
As premise, the ESP32 module I'm using has a 2 MB PSRAM on-board.
I wrote a test code to verify it's working:
void setup()
{
Serial.begin(115200);
while (!...
0
votes
3
answers
82
views
Multidimensional arrays in structs memory allocation for unknown sizes
I want to be able to use a struct where a member is a 2-dimensional array, and the struct is stored on the heap.
typedef struct Map
{
int xSize;
int ySize;
int mapMatrix[][];
} Map;
My ...
1
vote
1
answer
39
views
Access violation reading location 0x31322d50
I'm having an allocation problem in a C application
When debugging, I see that the problem occurs in this line:
long **matr=(long **)calloc(my_size, sizeof(long *));
Usually, my_size is less than ...
0
votes
2
answers
122
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 ...
2
votes
1
answer
132
views
free(): invalid pointer Aborted (code dumped) (ubuntu C)
I have a big project in C that has somewhere around 2 thousand lines of code.
I have in this project a linked list that I am using to store data of the program, at the end of the program I am calling ...
-3
votes
1
answer
70
views
calloc only return arrays of size 8 [duplicate]
I'm trying to create arrays of doubles in C, such that every array has d doubles in it (the user inputs d). I tried using calloc to allocate the memory for each array:
double *vec;
vec = (double*) ...
1
vote
1
answer
112
views
What's the reason for the occurrence of Segmentation fault (core dumped)?
I use C language, and apply Dynamic Programming to solve the Travelling salesman problem. There is such a problem on ZeroJudge, An Online Judge System For Beginners, but I get Segmentation fault (core ...
0
votes
2
answers
127
views
Calloc memory being initialized to 0
I have a basic understanding of calloc(). It initializes the memory to 0, so why does this code:
table_t *table = calloc(1, sizeof(table_t));
printf("%d", *table);
I would expect this code ...
0
votes
1
answer
231
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 ...
0
votes
2
answers
80
views
why I get strange value after using calloc function in c when i print each element and print the whole value?
I'm new to see and just experimenting some function of it. I came across calloc() function.
I wonder why the whole variable printed, is not same as each of the indexes . shouldn't the variable value ...
0
votes
2
answers
112
views
How to use calloc and snprintf
I'd like to use calloc and snprintf.
Can you review my simple code and tell me how to fix it?
I kept having an error that Access violation writing location 0xFFFFFFFFB8A2D1F0.
Thank you!
int main()
{
...
0
votes
1
answer
130
views
How to undef calloc() function from stdlib to use our own function
I have a header file calloc.h, where I defined calloc() function using stdlib's malloc() function. But I am getting multiple definition of 'calloc' error.
calloc.h:
#include <stdbool.h>
#include ...
1
vote
2
answers
145
views
What type can the result of calloc be assigned to, a pointer to an array, a pointer to the type contained within the array, or either?
According to the standard (C17 draft, 7.22.3.2), calloc
void *calloc(size_t nmemb, size_t size);
"allocates space for an array of nmemb objects, each of whose size is size" (and ...
0
votes
1
answer
305
views
Failure in allocating contiguous memory allocation using calloc
I'm running attached code in 64-bit system, so I expect for this buffer bytes (21B), I'd be able to allocate memory, but calloc fails to do that and so it returns a NULL pointer. For smaller buffer ...
0
votes
1
answer
84
views
How am I supposed to initialize a array using calloc inside a function?
While trying to solve a problem called spiral matrix, I encountered a problem that i was not able to initialize a array inside of a function using calloc.
/*
* Note: The returned array must be ...
1
vote
2
answers
142
views
Is there a function(s) to copy a string into a new space in memory [closed]
If I want to copy the string "Best School" into a new space in memory in C programming, What statement(s) can I use to reserve enough space for it?
I have tried using this:
malloc(strlen(&...
0
votes
2
answers
160
views
Can you dynamically allocate a partially fixed-size array in C (2d array)?
I wanted to dynamically allocate a partially fixed size array, but ran into errors which would lead me to believe I don't actually know how an array like this functions, so I'm trying to figure out ...
2
votes
2
answers
115
views
How to advance pointer in order to shorten a string?
I am creating a char array (representing a string) and adding characters to the array from the end of the array. But I don't know how many characters I will be adding in total ahead of time.
Once I ...
0
votes
1
answer
56
views
random seg fault with calloc
I'm writing a small command line tool to extract and sort comments in Z80 assembly sources.
I'm blocked with a random seg fault (on MacOS Ventura), lldb shows always the same line, a translation error ...
5
votes
1
answer
526
views
Why is np.zeros() faster than re-initializing an existing array in Numba with Python?
Why isnumpy.zeros() faster than re-initializing an existing array?
I work with computer modeling and use numba for my work. Sometimes it is necessary to have a zeroed array to accumulate the results ...
2
votes
1
answer
663
views
openldap ch_calloc core dump in docker containers
With docker 24, in the debian docker image, running openldap results in a malloc segmentation fault. Even a simple version check:
$ docker run --rm -it --entrypoint bash debian
# apt update && ...
1
vote
1
answer
52
views
List in C Error - Stack around the variable 'memory' was corrupted
I am trying to implement a dynamic List in C and I do not get why I get the "Stack around the variable 'memory' was corrupted." Error when I am trying to add a second Item to the list.
...
0
votes
0
answers
38
views
Assign a pointer to point to a struct
I'm trying to make an array of structs made according to user's input.
first of all - I initialise an array using calloc:
BusLine* head = calloc (num_of_lines, sizeof (BusLine));
Then in a loop I ...
2
votes
2
answers
364
views
"Memory allocated with calloc must be freed all at once"?
I read that memory allocated with calloc "must be freed all at once".
Does it mean that it's not the case with malloc?
If yes, could someone give me a real world example?
Many thanks!
0
votes
5
answers
235
views
Why does memset fail and calloc succeed?
I'm trying to initialise an array of 26 strings. I'd prefer not to place the array on the heap, but I get a segmentation fault when I try to assign memory to the array using memset. The code to ...
1
vote
3
answers
425
views
Can you dynamically initialize stack memory to zero?
I have an integer array that's only useful in a single code block. I'd like to declare it and initialise it to zero on the stack to avoid the overhead of the heap.
I'd like to do this dynamically and ...
2
votes
2
answers
789
views
CS50 pset 4 smiley - what's the meaning of code line from license taks?
RGBTRIPLE (*image)[width] = calloc(height, width * sizeof(RGBTRIPLE))
I don't fully understand the code. What I understand is that:
calloc(height, width * sizeof(RGBTRIPLE)) - we are organizing a ...
1
vote
1
answer
169
views
Why using both malloc/calloc/realloc and brk functions will results in undefined behavior?
Does this means using one of (malloc or calloc or realloc) and one of(brk/sbrk) concurrently results in UB or using both malloc and calloc can also cause UB?
This happends through the entire program ...
0
votes
1
answer
37
views
Segmentation fault in DAG
How to Fix this Segmentation fault in, I have tried Some alternatives but still it's not working.
strcpy(temp->label,'\0');
strcpy(temp->target,'\0');
0
votes
0
answers
61
views
How to free calloc memory inside a char * function?
I have a function char *getArg1(char *input), this function returns only the first argument of the input string. It callocs space for arg1 within the function and I can't seem to correctly free this ...
1
vote
1
answer
169
views
How do I read a large file of numbers and store its content in an array?
My original task is: Given a file of numbers, I have to find all pairs (a pair is just 2 numbers; they cannot be consecutive) that fulfil a certain condition.
To do that, I've decided to create an ...
0
votes
2
answers
131
views
Am using realloc function here two times in a row and it doesn't seem to work , I use it once and it works completely fine can someone tell me why?
#include <stdio.h>
#include <stdlib.h>
char *ptr;
int n;
int main()
{
ptr = (char *)calloc(n, sizeof(char));
// First ID
printf("Enter the length of your employ ID\n&...
0
votes
2
answers
59
views
Possible double realloc of dynamic struct array elements triggered by fgets, potential struct initialisation issue
I've created an array of structs (calloc for initial, realloc for subsequent "elements"). Realloc/initialisation is triggered by each line read from a text file using fgets.
My problem is ...
0
votes
2
answers
510
views
Error: free(): double free detected in tcache 2. Calloc/Realloc Array of Structs in C when triggered by fgets
I'm getting back into C after a long break, and as always the problems lie in use of pointers and general memory management.
My specific problem arises from the use of realloc on a struct in order to ...
2
votes
1
answer
129
views
Why am I getting errors freeing memory allocated by apriltags image_u8_create() and stored in 2D vector of pointers in C++?
I am attempting to detect apriltags in image streams. Because the images come in from multiple sources at a high rate, detecting the tags in the image callback will take too long, causing images to be ...
1
vote
2
answers
154
views
C - repeat a string for a specific number of times in anothervariable
I want to repeat a string - for example hello - for a specific number of imes - for example 3 times -, but it doesnt work :) The example should look like this: hellohellohello, but I get no output or ...
0
votes
0
answers
113
views
How to expand an array to a matrix
Iam Writing a program that takes as input, the dimension n of an array. After that enter the elements of the array. Create a function that creates and returns an n-by-n matrix with this definition:
...
0
votes
1
answer
113
views
valgrind thinks calloc allocated memory is ununitialized
From the linux manual page on calloc, we learn that:
"The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. ...
0
votes
0
answers
95
views
How to pass array to function and realloc memory in C
I started working on a program and I need to create function, that reads input, in format {number,number, ...} and puts numbers into dynamically allocated array. I am currently having trouble ...
0
votes
1
answer
52
views
My secondary nodes data gets replaced w garbage when adding a new first node in my doubly linked list
I am working on a lab for school and we are supposed to implement functions to practice using linked list. I cant get past an assert(getFirstElement(head) == 2) because the data is just garbage (16040,...
2
votes
2
answers
98
views
Value assignment using calloc
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
int main()
{
int size = 5 ;
int* c = (int*)calloc(size,sizeof(int));
memset(c,0,size*sizeof(int)); // when I ...
0
votes
1
answer
39
views
Why is a calloc function call messing up this struct value?
C beginner. I'm trying to implement a graph, and I have these structs:
typedef struct GraphNode{
void *key;
void *data;
void **edges;
} GraphNode;
typedef struct Graph {
...
0
votes
0
answers
624
views
calloc implementaion in tlsf assertion failed
after executing the below function on an embedded system (esp32), the assertion in heap_tlsf.c fails, my code is:
remained_data = (char *)calloc(lbws - where_to_insert + 2, sizeof(char));
the lbws - ...
0
votes
0
answers
37
views
Keep getting "bad addressing" when calling read()
I'm doing a client-server in C and I keep getting a bad address.
I think the problem is with the second argument of read(), here is the code:
Function definition:
int send_modbus_request(char *...
0
votes
1
answer
61
views
How to cast string array created by calloc, back into string array?
In below sample code, under GDB, I want to watch a dynamically created string array as a typical string array:
// dynamically create an array of 2 strings, each string has 21 characters.
char **...
0
votes
1
answer
112
views
Malloc / Calloc crashing on the second interaction
I have a program that should return an array of pointers to students that I have in memory.
The first time my program works as it should, the second time it runs (there is a 3 time loop where I call ...
0
votes
1
answer
60
views
Allocating matrix performances
I have two scenarios, in both i allocate 78*2 sizeof(int) of memory and initialize it to 0.
Are there any differences regards performances?
Scenario A:
int ** v = calloc(2 , sizeof(int*));
for (i=...
0
votes
0
answers
77
views
Is there an alternative to calloc() in SYCL?
Is there an alternative to the C function calloc() in SYCL?
Or the only alternative to imitate the behaviour is to use malloc_host() / malloc_device() and then memset()? I've tested out this last ...
-1
votes
1
answer
925
views
C malloc failure
I've stumbled upon a problem of memory allocation. I am writing a simple application that is supposed to read files and get information from them. It is supposed to be very simple (single threaded) so ...
0
votes
1
answer
896
views
resizing an existing array using `realloc`
I defined an array and I tried to resize using realloc() but it is not working. My question is can I do that using arr[] or do I have to use calloc() or malloc() first to define the array and then use ...