2,611 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
2
votes
2
answers
121
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 ...
-6
votes
2
answers
282
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
228
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
209
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
168
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
162
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
130
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
122
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
120
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
692
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
54
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
59
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
139
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
140
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 ...