memory-management

Why are the contents pointed to by a pointer not changed when memory is deallocated using free()?

佐手、 提交于 2020-01-10 05:15:20
问题 I am a newbie when it comes to dynamic memory allocation. When we free the memory using void free(void *ptr) the memory is deallocated but the contents of the pointer are not deleted. Why is that? Is there any difference in more recent C compilers? 回答1: Computers don't "delete" memory as such, they just stop using all references to that memory cell and forget that anything of value is stored there. For example: int* func (void) { int x = 5; return &x; } printf("%d", *func()); // undefined

Do I conserve memory in MATLAB by declaring variables global instead of passing them as arguments?

浪尽此生 提交于 2020-01-10 02:43:11
问题 I am new to MATLAB, it wasn't in the job description and I've been forced to take over for the person who wrote and maintained the code my company uses. Life's tough. The guy from which I'm taking over told me that he declared all the big data vectors as global , to save memory. More specifically, so that when one function calls another function, he doesn't create a copy of the data when he passes it over. Is this true? I read Strategies for Efficient Use of Memory, and it says that When

Is there something like malloc/free in java?

蓝咒 提交于 2020-01-10 01:58:18
问题 I've never seen such statements though,does it exist in java world at all? 回答1: Java's version of malloc is new -- it creates a new object of a specified type. In Java, memory is managed for you, so you cannot explicitly delete or free an object. 回答2: Java has a garbage collector. That's why you never see such statements in your code(which is nice if you ask me) In computer science, garbage collection (GC) is a form of automatic memory management. It is a special case of resource management,

malloc behaviour on an embedded system

半腔热情 提交于 2020-01-09 19:39:43
问题 I'm currently working on an embedded project (STM32F103RB, CooCox CoIDE v.1.7.6 with arm-none-eabi-gcc 4.8 2013q4) and I'm trying to understand how malloc() behaves on plain C when the RAM is full. My STM32 has 20kB = 0x5000Bytes of RAM, 0x200 are used for the stack. #include <stdlib.h> #include "stm32f10x.h" struct list_el { char weight[1024]; }; typedef struct list_el item; int main(void) { item * curr; // allocate until RAM is full do { curr = (item *)malloc(sizeof(item)); } while (curr !=

Examples of forcing freeing of native memory direct ByteBuffer has allocated, using sun.misc.Unsafe?

你。 提交于 2020-01-09 12:52:53
问题 JDK provides abillity to allocate so-called direct ByteBuffers, where memory is allocate outside of Java heap. This can be beneficial since this memory is not touched by garbage collector, and as such does not contribute to GC overhead: this is a very useful for property for long-living things like caches. However, there is one critical problem with existing implementation: underlying memory is only allocated asynchronously when the owning ByteBuffer is garbage-collected; there is no way to

Resizing 2D Arrays in C

耗尽温柔 提交于 2020-01-09 10:33:36
问题 currently I am trying to resize a 2D Array in C using this code snippet array = (int**) realloc(array, s * 2 * sizeof(int)); Where s is the size of the array in rows and colums. However, when trying to access the new areas of the array like this, array[3][0] = x; I only get a segfault. The old areas of the array work fine. How can I solve this issue? 回答1: Assuming you declared array as int **array; and allocated as array = malloc( sizeof *array * ROWS ); if ( array ) { for ( size_t i = 0; i <

Resizing 2D Arrays in C

谁都会走 提交于 2020-01-09 10:33:13
问题 currently I am trying to resize a 2D Array in C using this code snippet array = (int**) realloc(array, s * 2 * sizeof(int)); Where s is the size of the array in rows and colums. However, when trying to access the new areas of the array like this, array[3][0] = x; I only get a segfault. The old areas of the array work fine. How can I solve this issue? 回答1: Assuming you declared array as int **array; and allocated as array = malloc( sizeof *array * ROWS ); if ( array ) { for ( size_t i = 0; i <

Creating and deallocating a Qt widget object

我们两清 提交于 2020-01-09 05:01:09
问题 I heard that the widgets should be allocated on the heap (using new), and then there are no needs to delete them (it is done automatically). Can someone explain why? What happens if a widget is not allocated that way, but on a stack? I am not sure if it matters, but all widgets I am creating have a parent. This says : If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is

Why do I get different results when I dereference a pointer after freeing it?

杀马特。学长 韩版系。学妹 提交于 2020-01-09 03:57:12
问题 I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work according a well-defined principle. I've a trivial program like this, that shows how to return an array dynamically allocated: #include <stdio.h> #include

Is it good practice to free a NULL pointer in C? [duplicate]

天大地大妈咪最大 提交于 2020-01-09 02:00:54
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Does free(ptr) where ptr is NULL corrupt memory? I'm writing a C function that frees a pointer if it was malloc() ed. The pointer can either be NULL (in the case that an error occured and the code didn't get the chance to allocate anything) or allocated with malloc() . Is it safe to use free(ptr); instead of if (ptr != NULL) free(ptr); ? gcc doesn't complain at all, even with -Wall -Wextra -ansi -pedantic , but