Questions tagged [free]

free is a function to deallocate memory obtained from malloc and other functions in C. Do not use this tag to refer to free software. Asking for software recommendation is off-topic on Stack Overflow. If you have a question about free software, you can ask here: https://softwarerecs.stackexchange.com/

free() is the Standard C (ISO 9899:1989) function declared in <stdlib.h> to deallocate memory allocated by a previous call to malloc, calloc, or realloc. On POSIX (IEEE Std 1003.1) systems, it is also used to free memory obtained from calls to posix_memalign and strdup.


NAME

free - free allocated memory

SYNOPSIS

#include <stdlib.h>

void free(void *ptr);

DESCRIPTION

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

RETURN VALUE

The free() function returns no value.


Wikipedia

References

Related Tags

2564 questions
10
votes
3 answers

How can I do automatic memory management in C?

In C memory allocation/deallocation done by malloc and free. In C++ memory allocation/deallocation done by new and delete. There are some solutions in C++ for automatic memory management like: Smart Pointers. RAII (Resource Acquisition Is…
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
10
votes
4 answers

What happens to memory after free()?

I know that on your hard drive, if you delete a file, the data is not (instantly) gone. The data is still there until it is overwritten. I was wondering if a similar concept existed in memory. Say I allocate 256 bytes for a string, is that string…
Lienau
  • 1,353
  • 1
  • 20
  • 38
10
votes
2 answers

"malloc in local function, free memory in main" is it ok? How?

I learned in book that if I need to return a pointer from a function, I use malloc() and get memory from the heap. I was wondering how I can free() up the memory allocated after the function. Is OK to do what I did in the following code to free that…
shinhwa
  • 129
  • 1
  • 4
10
votes
4 answers

Correct usage of free() function in C

I am new in C programming language so can you tell me if this is correct way to do. for example: program points on buffer and i use that pointer as parameter in free() function. So, what problems can this function cause ?
David Demetradze
  • 1,361
  • 2
  • 12
  • 18
10
votes
6 answers

Using realloc (X, 0) instead of free() and using malloc with length of a string +1

So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework". Mine is, but it's also completed and I just want to further…
keont
  • 653
  • 1
  • 9
  • 26
10
votes
1 answer

Why doesn't CudaFree seem to free memory?

I am trying to allocate device memory, copy to it, perform the calculations on the GPU, copy the results back and then free up the device memory I allocated. I wanted to make sure that I wasn't going over the limit and I wanted to see if I would…
Beau Bellamy
  • 461
  • 1
  • 8
  • 19
9
votes
4 answers

Problem with free() on structs in C. It doesn't reduce memory usage

I'm having a problem with free() on a struct in my C program. When I look at /proc//statm before and after the free it doesn't seem to reduce. Am I using free() wrong in this case, or am I reading /proc//statm wrong? Here is a test case which yields…
oprimus
  • 396
  • 3
  • 11
9
votes
2 answers

Free cross-platform library to convert numbers (money amounts) to words?

I'm looking for cross-platform library which I can use in my C application to convert money amounts (e.g. $123.50) to words (one hundred twenty three dollars and fifty cents). I need support for multiple currencies: dollars, euros, UK pounds…
bialix
  • 20,053
  • 8
  • 46
  • 63
9
votes
3 answers

Is it possible to partially deallocate memory?

In C (or C++) I'm wondering if it's possible to partially deallocate a block of memory. For example, suppose we create an array of integers a of size 100, int * a = malloc(sizeof(int)*100); and then later we want to resize a so that it holds 20…
Cam
  • 14,930
  • 16
  • 77
  • 128
9
votes
5 answers

Checking if free worked

I've read this question and answer: How do malloc() and free() work? A friend asked me how can I be sure a free worked. I replied that if it didn't work then the OS is probably crashing and it won't matter for much longer. But I'm more interested in…
Blue Granny
  • 772
  • 1
  • 8
  • 24
9
votes
2 answers

How to free recursive struct (trie)

FINAL EDIT My function that frees the memory works properly, and as milevyo has suggested, the problem lies in node creation, which I had fixed. I now have a separate problem where the program segfaults when run normally, but it cannot be reproduced…
jiewpeng
  • 333
  • 2
  • 8
9
votes
3 answers

Do I need to free char array of fixed length?

As far as I can tell from the answers to other SO questions, I don't need to free fixed-length arrays like the following: unsigned char buffer[16]; But in general one has to free memory whenever calling malloc, memcpy, etc. My Question is: Do I…
Sebastian S
  • 4,420
  • 4
  • 34
  • 63
9
votes
4 answers

Understanding of pointers with malloc and free

Pointers are a really tricky thing in C. For a lot of people is hard to understand it, so for a good understanding I wrote following code: #include #include int main(int argc, char *argv[]) { int *p; // pointer -> will be…
Mihail Feraru
  • 175
  • 1
  • 3
  • 14
9
votes
2 answers

Can I free the memory of the char* string when I assign it to std::string?

Can I free the memory of the char* pointed string after I have convert it to a std::string? For example: char* c_string; c_string = strdup("This is a test"); std::string cpp_string; cpp_string(c_string); free(c_string); /* can I call free here? */
SSC
  • 1,311
  • 5
  • 18
  • 29
8
votes
5 answers

Is there, besides hunting for memory leaks, another situation where I should free all objects when destroying an application?

Suppose an application with some forms and only one data module are created at start. In the DM1.OnCreate event, a TStringList is created to be used at runtime. We know that when the application is being terminated, all things will be destroyed and…
EMBarbosa
  • 1,473
  • 1
  • 22
  • 76