What is raw memory in C & C++?

2020-07-18 11:28发布

Sorry for asking this question, but actually I don't know about this. I have read in the following FAQ entry: Can I free() pointers allocated with new? Can I delete pointers allocated with malloc()?

Furthermore, there is no guarantee that the mechanism used by new and delete to acquire and release raw memory is compatible with malloc() and free().

I just want to know that what is this "raw memory"?

标签: c++ memory
3条回答
Rolldiameter
2楼-- · 2020-07-18 11:57

In c you used malloc and free to allocate memory. If malloc rerurned a pointer casted to void, you would have more or less raw memory. The cpp pendant is new and delete. There is also the possibility with special casts to get untyped memory, so called raw memory pointed to by raw pointers. But it is fact that any ressource allocated by new or new[] must be freed by delete resp.delete[]. BUT NOT THE MEMORY ALLOCATED BY REPLACEMENT NEW.

查看更多
Summer. ? 凉城
3楼-- · 2020-07-18 12:06

"Raw memory" refers to blocks of memory, treated as unstructured arrays of bytes. Higher-level languages use these as the storage for objects; the program usually interacts with these objects, not the low-level byte values.

In C++, raw memory can be allocated dynamically using two different allocation functions:

  • operator new, used to allocate from the free store when you create an object using new
  • malloc from the C library

Memory allocated by new must be released with delete; and memory allocated with malloc must be released with free.

The line you quote explains that these might use different mechanisms to manage allocation; so that it's an error to use the wrong function to release memory (e.g. to allocate memory with new and try to release it with free).

查看更多
Anthone
4楼-- · 2020-07-18 12:07

Raw memory refers to the unmanaged memory in C and C++. malloc, calloc, realloc, and free are low-level functions that simply deal with raw memory.

You can also refer this Stanford document about Raw memory

查看更多
登录 后发表回答