2,612 questions
2
votes
1
answer
93
views
Is it undefined behaviour to free a `static restrict` array function parameter?
I'm asking this question from the perspective of a compiler developer – I want to know whether a particular potential optimisation is valid (because all programs that the optimisation would break have ...
-4
votes
0
answers
102
views
SIGSEV signal after method call - How do pointers work after pass-by-reference?
While implementing a Dijkstra-Search on a Graph, I call a seperate method to perform the search, handing it a pointer to my nodes and direct values for start/endpoint and array size. Notably,t he ...
-6
votes
2
answers
274
views
How to try catch finally in c
I want to try catch the free(hello); so that the free(world); can still be executed for freeing all allocated memory in the main program.
int main()
{
const char *hello = "Hello World";
...
4
votes
2
answers
224
views
How to free the value inside struct in c
I don't know how to reset the value field inside the Hello struct. It's a pointer pointed to an outside passed input argument.
typedef struct Hello {
void *value;
} Hello;
Hello* create_hello() {
...
2
votes
3
answers
206
views
Writing a Von Neumann Ordinal generator in C : Problem with malloc
I want to write a computer programme that will do the following things :
1a. It will make an array 3 characters long.
£££
2a. It will then initialize the array with the string "{_}" and ...
3
votes
2
answers
166
views
Is it safe to call free on std::string_view::data?
Is it safe to call free in the example below:
size_t len = 10;
char* buffer = static_cast<char*>(malloc(len));
std::string_view sview(buffer, len);
free(sview.data())
Why do I need this?
I have ...
0
votes
2
answers
156
views
How can I call free and set the pointer to NULL inside a function correctly and why?
I am confused about &str[0], which is equal to str.
If I can do str = NULL, why can’t I do &str[0] = NULL or why does it not work?
Also, since free(str) and free(&str[0]) both work to free ...
3
votes
2
answers
127
views
Custom free function doesn't release memory completely, Valgrind reports 'still reachable'
I've been following an article that walks through implementing primitive versions of malloc and free. After finishing the allocator, I wanted to test it using Valgrind, so I added the following lines ...
1
vote
2
answers
121
views
Leak errors in fifo application in c
I'm not sure why my valgrind is spitting heap errors and I've been tracing my code left and right. I have some code, but I'm not sure if more code is needed.
My jobs.h
#include "piper.h"
#...
3
votes
1
answer
119
views
Why cant I free the char buffer after assigning the last index to null terminator (\0) [duplicate]
I'm just confused as to why this keeps giving me an error.
If I just fread the file in and don't assign the last index to the null terminator, I can free the memory after using it. However, if I do ...
5
votes
6
answers
534
views
Decent ways to handle malloc failure?
Suppose that I need to malloc three times and I would like to make an early return if one of the malloc calls have failed.
My first try:
void test(void) {
int *a, *b, *c;
a = malloc(sizeof(*...
0
votes
0
answers
53
views
View office file viewer
I am facing a problem in my application. I'm using the Office Viewer to view office files, but when the document loads, it appears with its own options bar (for example, to create a copy or turn on ...
1
vote
0
answers
50
views
Not able to merge free blocks together in my simple, custom, memory allocator
I am trying to implement a simple memory allocator. Essentially trying to copy the function malloc() and free(void *ptr) in C.
I initialize a region of memory using sbrk(). In my custom malloc ...
1
vote
1
answer
136
views
What happens if you call free() on non allocated memory returned by a function
I am writing some random code and I wanted to know if there is there anything that defines how freeing memory returned like this works or if it is just undefined behavior.
struct a *get()
{
static ...
0
votes
2
answers
129
views
Freeing a dynamically allocated string with an internal null byte
The following code allocates 256 bytes for a character array and then replaces the space with \0 (similar to what strtok and strsep do).
#include <stdlib.h>
#include <string.h>
int main() ...
1
vote
1
answer
175
views
Using free() with data structures
I am not from a computer science background and have been trying to learn Data Structures using C programming.
I have made some binary tree programs and realised that I have used malloc() left and ...
0
votes
0
answers
45
views
M2M authentication but one side is public
I've looked at Auth0 and firebase, but both these options always either require you to store a client secret on both machines or active user interaction.
Basically the thing I had in mind is just the ...
2
votes
0
answers
94
views
"RegisterSecureMemoryCacheCallback" exception in C program
Could someone please explain this error? It craps out when I try to free the array. It works fine when I malloc inside of main so I'm guessing its because of the function scope? I thought if I ...
2
votes
3
answers
149
views
How to free memory when casting from a void pointer in C
Question: if I have a pointer to "ints" struct as seen below and I cast it to void and back does that mean I'm not allowed to free it anymore? How does free keep track of the allocation ...
3
votes
4
answers
181
views
Handling clean-up in function with multiple allocations
Say I have a function that allocates some struct (object) and has to allocate additional data when setting its fields. Since each allocation can fail, the object creation may only partly succeed. What ...
-1
votes
3
answers
122
views
Free appears to be corrupting buffer values [closed]
Ok, I know reading a buffer after free'ing it is wrong. I was trying to prove a point that free() does not touch the data stored in the buffer, but I am unable to prove it :)
Here is the code:
#...
2
votes
0
answers
223
views
How to Run Automated WhatsApp Birthday Bot Daily for Free Without Re-authentication Issues?
I built an automated WhatsApp birthday wishing bot using whatsapp-web.js that works perfectly on my local machine. The bot is scheduled to check a database at 8 AM every day for birthdays and sends ...
1
vote
1
answer
97
views
Ideally shouldnt this code free tmp before ending?
Im confused on why is this code freeing list but not tmp after setting list to point to where tmp points (list = tmp;)
// Implements a list of numbers with an array of dynamic size
#include <stdio....
0
votes
2
answers
103
views
Why is free() causing an error here without the else block?
This is the full code I have right now. I have tried everything that I could think of, but when I have the contents of the else block, despite the code skipping through it, the code runs smoothly with ...
1
vote
0
answers
95
views
Double free when trying to free memory allocated in a Chained Hashmap in C
Apologies in advance for the code dump, I can't really think of another way to show my issue properly.
For a school project I have to make a bakery simulation and since it has to fit within certain ...
0
votes
4
answers
169
views
Using free on nested structs makes program crash
I was recently exploring ll the oop features of C, when i came to this issue. I want to create a game engine with C, so i use nested structs to organize ewerything. But when freeing the memory, the ...
4
votes
2
answers
181
views
Function to clear allocated memory is not working
I'm trying to make a DTC in C. This is the structure of the nodes:
typedef struct node {
int value;
struct node *lower;
struct node *higher;
} node;
To free the memory allocated using ...
3
votes
2
answers
194
views
Malloc sometimes fails immediately after a free
I want to test my code in low memory situations.
I wrote this function with setrlimit to limit the available memory :
unsigned short int oom_enable = 0;
char* _oomfill = NULL;
uint32_t oom_setup(...
1
vote
2
answers
277
views
C free() on Ubuntu VM, a question regarding heap memory
A simple program to allocate and free heap memory:
int main(int argc, char **argv) {
char *b1, *b2, *b3, *b4, *b_large;
b1 = malloc(8);
memset(b1, 0xaa, 8);
b2= malloc(16);
memset(...
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)<<&...
2
votes
1
answer
111
views
in c: my special free function refuses to work
A function that returns a 2D list of pointers to every possible knight movements on a grid.
Everything works except my spacial free function that crashes in random locations:
Unhandled exception at ...
0
votes
1
answer
130
views
How to to clear/delete all controls on a TWebGridPanel in TMS WEB Core?
I've got a TWebGridPanel component that I'm dynamically populating with rows and columns as well as controls within those rows and columns.
My issue is that I need to clear/delete/destroy all controls ...
1
vote
1
answer
124
views
Invalid pointer own get_next_line implementation
In the last reading of the file, when it still has to return a line, and the line variable has the content of that line, an invalid pointer error occurs when doing free in the main but that line has ...
0
votes
1
answer
41
views
free of struct 1 with an array of struct 2, inside of struct 2 there is an array of int
typedef struct partition_struct {
int* elements;
int last; //last element index
int dim; //n allocated int
} partition;
typedef struct partitions_struct {
partition* partitions;
...
2
votes
1
answer
126
views
Freeing non allocated memory (c)
In the get_next_line function, I am using the pointer temp to free the memory allocated for leftover after it is being modified by strchr.
if I remove temp, the function leaks, but as it is now, I am ...
0
votes
1
answer
73
views
Linked list inside an array deallocation in C
I have these structs:
typedef struct Nodo{
int id_nodo;
struct Nodo *next;
} Nodo;
typedef struct{
Nodo *head;
} inmap;
//Struct Grafo
typedef struct {
int N; // numero dei nodi del ...
0
votes
2
answers
141
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 ...
1
vote
1
answer
144
views
How does my recursive function in C work?
I wrote a recursive function in C to free memory allocations made by a program that generates a tree of nodes. However, I can't work out how it works - but it does pass the check50. Perhaps I've not ...
-2
votes
1
answer
99
views
error: incompatible type for argument 1 of 'free'| [closed]
strutto *head,*cdm;
head=&a;
cdm=head;
a.next=&b;
b.next=&c;
c.next=NULL;
do{
printf("%d\n",cdm->dato);
cdm=cdm->next;
} while(cdm!=NULL);
free(a);
free(b);
...
0
votes
0
answers
101
views
Does malloc consider TLB hits?
I recently tried to examine how malloc() and free() populate the address space with differently sized chunks of memory on my system. It is a debian linux with the glibc malloc implementation and 4 GB ...
0
votes
0
answers
39
views
How to free chars* in a char** [duplicate]
I don't understand how to free() the strings in my array char**
When I try to do it in this code I got a segfault and 40 bytes lost with valgrind
int main(void)
{
char **strs;
char *...
1
vote
1
answer
50
views
Memory Management Issue with Linked List Freeing in C Program
I'm currently tackling the Advent of Code 2022 Day 9 challenge and encountered an unexpected issue with freeing a dynamically allocated linked list in my C program. Although my code runs smoothly ...
0
votes
1
answer
87
views
Freeing memory for dynamically allocated hashmap with structs as values - C
I have followed a few tutorials for hashmaps in c. From the code you can probably tell I relied heavily on this implementation which has a nice follow up video where he revisits it to show how he ...
1
vote
2
answers
63
views
Calling `free` syscall for the first element array of void* in C
Here's a snippet of C code:
int main()
{
void **values = calloc(10, sizeof(void *));
int *a = malloc(sizeof(int));
*a = 100;
int *b = malloc(sizeof(int));
*b = 200;
values[0] ...
1
vote
2
answers
93
views
How do I free a returned string value that is dynamically allocated in C?
This is my first post on StackOverFlow, I was writing the code for a linked list in C, when suddently stumble upon a situation i have no solution for (located in dupstring function). This is my ...
0
votes
1
answer
44
views
Who prints the stack information?
when my process double free, I get some print, I wonder those info print by who ?
if I donot need it, can I close the print?
// g++ hel.cpp
#include <iostream>
int main() {
int *p = new int;...
2
votes
1
answer
170
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 ...
0
votes
1
answer
97
views
How do I free memory allocated to a void* member of a struct in my c project without breaking my GoogleTest project?
I'm trying to free all the memory allocated in testing_malloc() with the method testing_free(). I am using a Google Test project to test my code and the MemoryLeakDetector mentioned in this answer.
...
-1
votes
2
answers
108
views
How to free a struct with a function pointer member?
I am allocating a struct with a function pointer. Obviously, I have to free the struct at the program end. However, freeing a function pointer leads to undefined behavior. How should I do it? I tried ...
2
votes
0
answers
94
views
The return value goes wrong if I release something else
I am trying to parse a query string, feeding the method with this:
char array[] = "address=NYC&port=3359&username=JDoe&password=Secret*";
Here is my code (I have included the ...