Questions tagged [malloc]
The malloc function performs dynamic memory allocation in C and is part of the standard library. Use this tag for questions about usage, behavior and implementations of malloc.
9,199
questions
0
votes
1
answer
32
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
28
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>
#...
2
votes
1
answer
88
views
Mysterious (I think) buffer over run in C
Sorry if this is a duplicate post.
I am currently having an issue where I am getting a warning on this piece of code saying C6386: Buffer overrun while writing to 'titleTextPointer' but I'm fairly ...
0
votes
0
answers
9
views
Mac zsh terminal unable to connect to postgres servers
psql "host=backbone-dev.postgres.database.azure.com port=5432 dbname=postgres user=postgres@backbone-dev password=blabblahblah sslmode=require"
psql: error: connection to server at "...
0
votes
1
answer
67
views
Malloc: double free error on M3 Macbook pro
I am working on a Django python project with a postgres db hosted with render.com.
The code works fine on server and my imac. I recently got a Macbook Pro M3 (running sonoma). I have replicated the ...
-1
votes
0
answers
22
views
jvm server memory leak troubleshooting: malloc of glibc has any bugs?
I have a java server running on jdk8, centos7 environment. The jvm heap on this server is set to 6GB, and when analyzed through native memory tracking, it uses about 9GB of memory, including off-heap ...
2
votes
1
answer
88
views
Why is this allocation not well done?
I have point.h and polygon.h files, with their associated .c files. In point.h
// point.h
#ifndef POINT_H
#define POINT_H
typedef struct Point point;
point *point_alloc(void);
void *point_free(point ...
0
votes
0
answers
17
views
How to make jemalloc to use transparent huge pages?
I want jemalloc to use transparent huge pages.
My environment parameter are as follows:
echo $MALLOC_CONF
"thp:always,prof:false,prof_active:false,trust_madvise:false"
However, kernel THP ...
-1
votes
0
answers
42
views
My program always return "Segmentation Fault" (3d Array)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Fonction Recyclable :
double getRandomNumber() {
return rand() % 101; // Generates a random number between 0 and 100
}
...
0
votes
1
answer
57
views
C++ character pointer string allocated with malloc contains gibberish when printed
As far as I can tell, I'm using malloc correctly to allocate a string as a character pointer.
char* test = (char*) malloc (sizeof (char) * 4);
test[0] = 't';
test[1] = 'e';
test[2] = 's';
...
2
votes
2
answers
59
views
Valgrind error when writing (fwrite) a struct array into binary file
The goal of my program is to read a list of people from the standard input and be able to write and read them onto binary files. Each person in the list is a tPessoa structure containing three string ...
1
vote
3
answers
119
views
Segmentation fault when splitting a memory block in C
I'm currently recoding my own version of malloc(), I'm currently only using sbrk() and would like to implement a version with mmap() later on.
Here's the block metadata structure:
typedef struct ...
-1
votes
1
answer
68
views
Why does malloc throw a "corrupted top size" error when memory is allocated for a struct?
I have a struct that I am using, and I have written a method to create and initialize it. The method throws a corrupted top size error when I malloc memory for the struct. Here is the code for the ...
0
votes
2
answers
90
views
How to compile my version of Malloc in C?
I'm currently coding my implementation of the malloc() function, so I'm compiling with the following flags: -m64 -fPIC -pedantic -Wall -Wextra -Werror -nostdlib -ggdb3.
For the time being, I'm ...
0
votes
0
answers
16
views
Virtual memory mapping reported by pmap -X does not come from mmap system call
I have a problem with excessive memory consumption of the process and tried to catch system calls and stack traces with strace to get a grasp on who mapped the memory. So runnin strace -f myapp I got ...
1
vote
2
answers
77
views
Memory slice using malloc by slicing one big malloc'd memory
Consider an application that invokes malloc() and free() many times. The application allocates a total of say 1000 bytes in total.
I am trying to write a code where we reduce number of malloc / free ...
0
votes
0
answers
25
views
Parallel KNN Implementation using MPI giving errors using 3 processes and working fine with 1 process
I am implementing parallel KNN algorithm using MPI.
modified code files i am using from a git repo are as follows:
I have X_train.csv, X_test.csv, y_train.csv, y_test.csv files in the same directory ...
0
votes
1
answer
70
views
Is it possible to create a dynamic array without explicitly defining a variable to specify the length?
I’m trying to create a dynamic array which can be modified using the functions Array_Push(array, val) & Array_Del(array, index). Now the current way I have this I need a variable to keep track of ...
0
votes
3
answers
127
views
In C is it possible to allocate a `char[]` on the heap?
I am currently trying to learn C so apologies if this is a stupid question. There are many previous questions that highlight specifically that char* != char[].
Example 1 - link
Example 2 - link
...
0
votes
0
answers
11
views
How does the libhugetlbfs library replace the memory allocation process with huge pages?
According to the document, "libhugetlbfs can be used to make an existing application use hugepages for all its malloc() calls". And it is done through LD_PRELOAD environment variable.
...
0
votes
0
answers
46
views
C I have a database project that hasn't been running due to the error "zsh: illegal hardware instructions ./main"
/* databaseLogic.c */
#include <stdlib.h>
#include <stdio.h>
const int MAX_STR_LEN = 20;
int roomAvailability(int roomChecked)
{
FILE* fileAvailable;
char filename[46];
...
0
votes
0
answers
19
views
Don't understand how sbrk works and why my structure cannot be accessed
// SPDX-License-Identifier: BSD-3-Clause
#include "osmem.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <assert.h>
#include "block_meta.h"
#define ...
3
votes
1
answer
92
views
Linked list without malloc [duplicate]
I'm starting to learn C myself, and based on the book "Programming in C (4th Edition)", the author defined a linked list as following:
struct entry
{
int value;
struct entry *next;
};...
0
votes
1
answer
107
views
What is the meaning of `malloc(): corrupted top size`
I am trying to append a struct pointer to a dynamically allocated array. After that I return the struct pointer that was added to the array. This is then dereferenced and put into a variable that I ...
-1
votes
1
answer
64
views
How to use TCMalloc by it's header but not instead of malloc, new
I'm adding TCMalloc to my project. My method is linking .a to my project, so all of malloc, new, free... will be replaced by implement of TCMalloc. But some of malloc in my project can't be replaced ...
-1
votes
0
answers
55
views
In C program, how do I know how much free memory in OS(CentOS 7)? [duplicate]
I use the malloc() function in a C program to get the available memory.
I often encounter a problem where the malloc() function doesn't return NULL when the CentOS7 OS can't allocate any more memory, ...
1
vote
1
answer
120
views
Could allocation size of zero in malloc result in conflicting memory locations?
So from the explanation of malloc(0) question. I find it counterintuitive that the notion of the zero size of the allocated memory chunk when returning a not-NULL value. (concluded from here)
Since, ...
-1
votes
2
answers
131
views
C malloc works, but it should not
For my linear algebra code I need to allocate memory for a lower triangular matrix,
represented by a one dimensional array. I was experimenting with how many elements I can allocate and
stumbled upon ...
0
votes
0
answers
57
views
How to replace function calls in binary files with self implemented malloc?
My code is written in C/C++. I want to replace the original call with my own implemented malloc function without modifying the code or recompiling. It seems that LD_PRELOAD can do this thing, but it ...
1
vote
2
answers
51
views
Fscanf prints everything but exits program with code 1 in C
I have this function which reads some strings in this format:
2
London Berlin 220 1.5
London Milan 280 2.5
...
and same stuff should be read againg...
2 is the number of strings after itself. For this ...
0
votes
1
answer
56
views
Syscall param getcwd(buf) points to unaddressable byte(s)
I am testing a program using valgrind for the first time. I haven't found any help regarding this error. What am I doing wrong? how do I solve it?
Minimal reproducible example:
#include <sys/stat.h&...
0
votes
4
answers
139
views
Proper memory free for pointers inside struct C
I have a struct list which contains a dynamic array of type struct pair, and each element of this array points to two variable of type struct element. In the main
of my program I allocate, via a ...
1
vote
1
answer
45
views
Trying to understand a `pointer being freed was not allocated` error in C
So the error is self explanatory, but I don't understand how I'm getting it. I made the malloc and now the free complains.
I'm trying to build a map/hashtable for myself. I'm newish to C but not ...
1
vote
1
answer
18
views
Trouble using Pointers or Malloc outside of initial function
I am working on this one project which asks to call inputs and output them in a separate display function. For the life of me I can not understand what is causing the issue in this code segment. My ...
1
vote
1
answer
223
views
C++ header files changes program break
I can't understand why this code prints different program breaks
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#...
0
votes
2
answers
96
views
How to free dynamically allocated memory in a linkedlist in C
I have tried to implement a linked list in C, and I have some memory leaks due to not freeing some of the malloc'd variables. I am not sure when and how to free them, as I cannot free them before ...
1
vote
0
answers
61
views
I have free'd dynamically allocated memory, but I am still getting memory leaks
I am currently doing an assignment in C, and I have dynamically allocated my structs, linkedlist, and also a 2d array. At the end of the code I have also free'd these. I cannot show all of my code, ...
0
votes
0
answers
12
views
how to understand the relation between malloc size and process's VmSize and VmRSS?
I have a simple c program:
void t_simple_case() {
int *p = (int *)malloc(1024 * sizeof(int));
for (int i = 0; i < 1024; i++) {
p[i] = i + 1;
}
getchar();
delete []p;
...
0
votes
0
answers
38
views
Why do I keep getting a segmentation fault when I assign 'beginning' to variable currentPos of type memHead*?
Here is mymalloc.c:
#include <stdio.h>
#include <stdlib.h>
#include "mymalloc.h"
static double memory[MAX_MEMORY]; //sets maximum memory sizeOfMem to 4096 bytes as defined in &...
1
vote
0
answers
52
views
The wrap_malloc function is unable to intercept certain functions, such as backtrace_symbols
Here's an example of the code I wrote
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
void __real_free(void *ptr);
void *__real_calloc(size_t num, size_t size);
void *...
-2
votes
1
answer
86
views
Cannot access memory allocated with malloc in a function from my main
As far as I know allocating memory with malloc in a function allows you to use that memory in main until you manually free it. I have a function that reads a MNISTI image file and creates a vector for ...
2
votes
2
answers
95
views
Malloc and alignment
As far as I know, every type - like int - can only be stored at memory addresses which start at a power of 2. Now my question is, how can malloc cope with this requirement? For instance, if I call
...
1
vote
1
answer
104
views
'malloc(): corrupted top size' After allocating more than 200K int
I got assignment to do Bucket Sort with openMP, and I decided to do Quick Sort on each bucket. The requirement wants me to test by keep increasing the amount of integers and change the number of ...
1
vote
1
answer
63
views
Use malloc to define a string address
I get no output when I run this code. I'm expecting the file to be written to with uppercase values and also uppercase values to be printed to the screen.
#include <stdio.h>
#include <stdlib....
4
votes
0
answers
178
views
How to program a malloc() implementation with correct pointer alignment in C
I'm currently trying to recode my own implementation of malloc() in C language. I followed several tutorials but each time the pointer alignment was inccorect (aligned to a power of 4) but according ...
1
vote
3
answers
179
views
Why malloc doesn't malloc?
Here's a C program to introduce the problem.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
if(argc != 2) {
printf("...
0
votes
0
answers
48
views
Fail to release the memory after 2D array multiplication with python ctypes
I tried to call functions in DLL with python ctypes module. The functions perform 2D array multiplication successfully and created by C. The following source code would be packaged into "...
0
votes
1
answer
72
views
I need help in the program by c
#include <stdio.h>
#include <stdlib.h>
typedef int Datatype;
typedef struct singlyLinkedList
{
Datatype data;
struct singlyLinkedList *next;
}node;
typedef node *linkList;
void ...
2
votes
4
answers
218
views
MISRA C advises against malloc, does this mean calloc is far more secure?
I have used C for a few years but only recently made a serious effort to understand undefined behaviour and the pitfalls of C memory leaks etc. This is because now I use calloc several times in some ...
3
votes
2
answers
93
views
initial or terminal malloc buffer possible?
Suppose I do something as follows:
size_t length = 1000;
char* p = malloc(length);
and then I want to loop over the elements, so most basic would be:
for (size_t i = 0; i < length; ++i) {
p[i] = ...