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.

0
votes
2answers
43 views

Why malloc and sbrk returns address from seperate segments?

I am trying to implement understand how dynamic memory allocation is happening. So I thought of implementing malloc of my own using sbrk() system call. My Question here is when i try to allocate ...
-2
votes
1answer
13 views

How to fix “Pointer being realloc'd was not allocated” error

I have a structure called House and it includes a char array for neighborhood. I am trying to read a database of houses and create an array for the houses with the same neighborhood. Reading part is ...
0
votes
1answer
36 views

Can't scan in .txt file using fgets into flexible data structure

I have a homework task that requires me to process .txt files by scanning them into a flexible data structure and then searching the files for words with capital letters. I'm having issues scanning ...
0
votes
1answer
25 views

[strcat&malloc&free], when I use strcat(), pointer seems not NULL even I deliberately made it to be(no extra assignment)? [duplicate]

as the title, I made a demo to describe my confusion. if I replace [strcat ]to [stacpy], I can get what's obvious, which is @ @ @ so I suspected the problem comes up with [strcat]. void main(){ ...
0
votes
1answer
108 views

Malloc Signal: SIGABRT (signal SIGABRT) problem

I am having a problem with my code using malloc. It has been working perfectly until an hour before. It causes on this line temp2 = (Temp*)malloc(sizeof(Temp)); I've tried ...
2
votes
3answers
67 views

C++ programming, dynamical memory is not working properly using malloc and calloc

I have just started learning C++ and I came on a problem I couldn't find on the Internet so I hope you can help me with. This is my code: int* a; int* b; a = (int*)calloc(1, sizeof(int)); b = (int*)...
0
votes
2answers
74 views

UITableView is crashing if I call `insertRows` while rotating the device “Incorrect checksum for freed object”

I have to parse a lot of data to display in my Tableview. So, instead of parsing all the data and then reload the table view, I end up with parsing 50 rows of data and insert them. So, the user has ...
4
votes
2answers
128 views

Why does a function returning const char * with string literals work? [duplicate]

I found code in libcurl that looks like: const char * curl_easy_strerror(CURLcode error) { switch(error) { case CURLE_OK: return "No error"; case CURLE_UNSUPPORTED_PROTOCOL: return "...
0
votes
0answers
45 views

Three dimensional static array memory allocation

I am new to C++ programming, you might think its silly question but I tried everything. I want to allocate a memory using malloc for three dimensional array. I dont want to use pointers. I want size ...
0
votes
0answers
10 views

How a corrupted double linked list error is detected in malloc?

I don't understand a condition for gettint the error - corrupted double-linked list (not small) - There is a point in the program where it crashes due to corrupted double-linked list (not small). ...
1
vote
2answers
24 views

Taking ints with scanf into a memory allocation during a while loop and realloc'ing

I declare an int pointer and give it a memory allocation. I then feed ints into it until the user inputs Ctrl+d. The loop works fine unless I try to input 7 or more, at which point it gives me an ...
1
vote
1answer
33 views

How to preload shared library and wrap malloc alongside using functions which use malloc in the same wrapped function?

I want to wrap malloc in a function which would print the allocation size, pointer address and the time of the allocation. This can be achieved preloading my shared library with my implementation of ...
0
votes
0answers
31 views

Reallocating to the same size causes invalid next size error

So this is kind of complicated to explain but I'll try my best. I'm getting realloc(): invalid next size Aborted (core dumped) But only when I try to realloc the vector to the same old size. ...
0
votes
1answer
29 views

How can I free memory used by malloc() outside a function?

I am trying to free the memory allocated by my getSongInfo function, I have tried using a pointer to the function call but I get an error "cannt assign int to type int*" error. Any help would be great ...
0
votes
1answer
76 views

Segmentation fault - Trying to read binary file into memory

I can't seem to load a binary file into the memory for better read performance compared to the more expensive reads to the actual file. The file is 124 MB and should be able to fit entirely into ...
0
votes
0answers
33 views

Using strcpy() to copy a string into a struct, when memory is allocated using malloc()

Currently in my program I have the user enter both a Song Title, and Artist Title for some songs. Each string is stored in an array. I am then trying to use those entered strings in a function to ...
2
votes
2answers
56 views

“free(): invalid next size (fast)” error when trying to free memory of array

I am writing one of the merge sort variations. In my task I use two arrays: keys in arr array and values in brr array. Keys are integer numbers and values are strings. Merge function receives these ...
-1
votes
0answers
34 views

memset will not work with my dynamically created memory? [duplicate]

i have a dynamically created array of floats and i cant seem to get memset to fill them with dummy numbers. //variables on the stack are put next to each other. void populate(float ** arr, int len, ...
0
votes
1answer
65 views

Problems when dynamically allocating a matrix in memory

I'm sorry if the title isn't specific enough on the issue, but I'm having some really weird and inconsistent problems with double pointers and I don't know how to describe them in just a sentence. ...
0
votes
1answer
23 views

Double Ended Queue, Call by Reference (Create Header)

i have a simple problem with my c code .... I want to call the variable with call by reference. I have this function: void createHeader(HEADER *yourHeader) { yourHeader = malloc(sizeof(HEADER)); ...
0
votes
1answer
43 views

How to allocate memory to a string array in C - malloc error

Problem: I want to allocate memory to the elements of a fixed sized string array, however, I experience a crash 75% of the time. That is 25% of the time my program runs flawlessly, but the error ...
1
vote
5answers
83 views

Is there a way to change an array size using malloc and free functions only?

As homework, I have to write a function that changes an array's size using malloc and free functions only. I know how to do it with realloc, but I don't know how to do it with malloc. typedef ...
0
votes
3answers
41 views

How to write a function that allocate a complete data structure [duplicate]

As I wrote above I'm trying to write a function that allocate a data structure here is what I have done but when I'm trying to call the T with an index it throws an error typedef struct { float ...
2
votes
1answer
48 views

Dynamically allocate C struct and access members based on the struct memory

In a C (ANSI) program I have a struct defined as: typedef struct Contacts { char * name; char * email; char * phone; struct Contacts *next; } aContact; aContact *paContact; Based on ...
0
votes
1answer
17 views

How to add pointer char datas (created using malloc) to a char array in C?

In my MPI code in C, i'm receiving a word from each of my slave processes. I want to add all these words to an char array in master side (part of code below). I can print these words but not collect ...
0
votes
0answers
10 views

Possible causes of a malloc crash and will the core be useful in determining the cause?

A malloc sporadically crashes an application and generates a core file. The amount of memory being allocated is minimal. Can it be assumed that the malloc call is attempting to allocate memory ...
0
votes
1answer
77 views

Why does free() leaves stuff in memory?

I'm trying to understand how malloc-realloc and free works deeply in C. I've found this page and I was able to understand how a chunk is allocated, but I'm not entirely sure how the free function ...
-1
votes
1answer
103 views

copying void pointer to another void pointer?

I am trying to copy a void pointer to different indexes of another pointer array, things work fine for characters but there appears problem for integer and doubles Here is my strcture: typedef ...
0
votes
0answers
66 views

Error while using malloc and FILE pointer together

So, what I am trying to do is initialize an unsigned integer buffer and also using a file pointer(FILE *fp) to read from a file and store the contents of the file in the above mentioned buffer The ...
9
votes
1answer
209 views

How could this buffer be overrun?

I apologize in advance for the useless title of this question, but nothing seemed to fit better. The idea here is to replicate argv in another variable, essentially making a copy of it. So the basic ...
1
vote
0answers
36 views

Valgrind shows definitely lost bytes but should be free? [duplicate]

I want to have a function that reads a file and returns specific lines as a char(string) array of char strings. I ran Valgrind and it shows that blocks are definitely lost and I don't get why. if I ...
-1
votes
0answers
45 views

Why does this code work properly on Linux (Fedora) but gives me Segmentation fault: 11 on Mac?

I am building a program scheduler, and for now I only need my interpreter to read arguments (program names + CPU time used) from the command line and send them over to the scheduler itself through IPC....
0
votes
1answer
70 views

I am confused how to understand this code. contains double pointers

I don't understand why the code below changes the array b: int a[] = { 3, 6, 9 }; int b[] = { 2, 4, 6, 8, 10 }; int **c; int **d[2]; c = (int **)malloc (b[1] * sizeof(int *)); *c = &a[1]; c[1] = ...
0
votes
1answer
54 views

Updating a variable's value in a struct cause another variable from the same struct to be modified

I'm working on a code which handles traffic in intersections over time and returns how much traffic there is in an intersection at a given time. Basically the problem I have is with an array of ...
1
vote
3answers
58 views

Why malloc in WebAssembly requires 4x the memory?

I wrote a script in C to allocate memory with malloc() on a infinite loop. My aim was to realize a simple Denial of Service using WebAssembly by opening multiple tabs and make the browser crash. I ...
-1
votes
2answers
61 views

Can't get the reason for segfault

EDIT : Note that It's not that I can't access the memory allocated by storeContents() in main() if you think so. Program crashes during the execution of storeContents() The program fails here : ...
0
votes
2answers
46 views

Memory allocation and leaking issue?

I am working on the print_selected(int x) function which, takes x then extracts bits 5, 6, 7, and 8 (starting from bit 0 at the rightmost) then print them in both binaries then in hexadecimal. There ...
0
votes
0answers
26 views

How can I solve/fix the following error? ERROR: malloc: Incorrect checksum for freed object <hex>: probably modified after being freed [duplicate]

I'm making a program which detects modifications of files in a certain directory, I'm trying to use the stat method to access time_t st_mtime; and get a delta of time. My problem is that I'm trying ...
0
votes
0answers
47 views

How to properly de-allocate the memory in this code?

I am working on a C homework, and I am finding it pretty confusing to detect what memory was not de-allocated. I been trying to place free() function in different locations on my code just to check ...
1
vote
3answers
63 views

do vendors implement new and malloc as small object allocators?

Are implementations allowed to have new and/or malloc allocate far more memory than requested, so it can avoid overhead for later small allocations? In my experience, no one ever allocates single ...
-1
votes
1answer
33 views

Function pointer allocated in the heap

I want to declare a local function pointer, allocate space for the pointer on the heap, point to different functions with it on the fly. #include<stdlib.h> #include<string.h> #include<...
0
votes
1answer
35 views

Malloc assertion failed (malloc.c:2385)

So Im doing so Matrix calculations/conversions right now and now an assertion error showed up. I started implementing a Matrix-CRS-Conversion and the program works just fine: uint32_t size = 0; for (...
0
votes
2answers
37 views

Dynamically allocating an array of integers,storing them in a file and finding the average of them in c

I am currently solving an assignment about dynamic memory allocation and it says that i should dynamically allocate an array of integers ,store them in the form of table in a file,later find and print ...
1
vote
0answers
33 views

How to fix sysmalloc: Assertion error in C

Firstly, I'm sorry, because this really seems to be a common error, but what triggers it seems to be different in every thread I could find in stackOverflow. I'm having the error malloc.c:2405: ...
0
votes
2answers
99 views

Does freeing structures in C also delete its members? [duplicate]

I'm pretty much new to C and i have a question about allocating memory. So I tried this code below that should free the structure elem1. struct elem{ char *data1; char *data2; }; int main()...
0
votes
1answer
50 views

Understanding malloc(), realloc() and free() in C from reading an output

I need to write an assignement regarding how memory managment is implemented in order to understand what do the few non-zero numbers in the output of this code represent. I do know that the malloc() ...
0
votes
2answers
23 views

Assigning ints of pointer with scanf

I've a question similar to this one: Assigning char array of pointers with scanf Rather of assigning char values to an array of pointers I'd like to assign values to int of pointers with scanf. In ...
0
votes
0answers
64 views

Where is the bug causing a heap-buffer-overflow?

I'm a rookie with C, but I am trying to solve a problem from LeetCode to get some more experience with hashtables. Running on my own Linux machine (of course without activating the address sanitizer ...
0
votes
1answer
22 views

Warning: Incompatible pointer types assigning to 'node_t *' (aka 'struct node *') from 'mode_t' (aka 'unsigned int *')

Our project is about creating a movie using a robot and software developed by one of our professors here at our college. The library is called MyroC, and the robots utilize this library for many ...
1
vote
2answers
38 views

How to fix memmory allocation problem in the function below? (malloc)

I need to read a .txt file and to allocate each word from the file in a struct which is pointed from a vector of structs. I'll explain better below. I appreciate your help. My program is allocating ...

http://mssss.yulina-kosm.ru