memory-management

Does declaring a variable already occupy memory

ⅰ亾dé卋堺 提交于 2019-12-24 00:33:17
问题 Hi I have been searching for the answer to this quite long. I want to know things under the hood regarding when memory will be taken up by my code. Ex. int myVar; Does this code already take up memory? by memory i mean the stack? and initially in .NET I have noticed that this line will have a default value of ZERO(0). But what does the variable really store? the value ZERO or an address to where the value ZERO(0) is stored? Also from what I understand about reference type is that when i write

C - pointer to pointer array

百般思念 提交于 2019-12-24 00:29:15
问题 I am learning C and as I got stuck at the pointer chapter. Particullary at the pointer to pointer ( **p ) part, used with processing 2 dimensional arrays. The following function generates a 2 dimmensional array with all its elements equal to 0 . double** example(int rows, int cols){ static double tr[rows][cols]; for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) tr[j][i] = 0; return tr; } int main(void){ double **tr; int m = 2 ; int n = 2 ; tr = transpose(m, n); return 0;} Is the tr

NSCache holds strong pointer to UIImage instantiated with imageWithData: and does not remove from memory on unload

不打扰是莪最后的温柔 提交于 2019-12-24 00:04:26
问题 I have a View Controller with a property galleryCache and when an image is downloaded using GCD and imageWithData: the image is added to the cache successfully with a key. However, when the view controller is dismissed it keeps strong pointers to those downloaded images causing them not to be removed from memory. Even if I use the removeAllObjects method on the cache in viewDidDisappear: memory does not clear up. Does anyone know why this might be? Here is the code for the method which

Possible memory leak with malloc, struct, std::string, and free

邮差的信 提交于 2019-12-23 23:53:28
问题 I've a situation like the following, and I'm not sure whether or not the std::string elements of the struct leak memory or if this is ok. Is the memory allocated by those two std::strings deleted when free(v) is called? struct MyData { std::string s1; std::string s2; }; void* v = malloc(sizeof(MyData)); ... MyData* d = static_cast<MyData*>(v); d->s1 = "asdf"; d->s2 = "1234"; ... free(v); Leak or not? I'm using the void-pointer because I have another superior struct, which consists of an enum

How to correctly and safely free() all memory used a nested struct in C?

余生颓废 提交于 2019-12-23 23:51:08
问题 I have four different layers of struct nested. The code is as follows: typedef struct System system; typedef struct College college; typedef struct Student student; typedef struct Family family; #define MAX_COLLEGES 10 #define MAX_NAME_LEN 32 #define MAX_STUDENTS 10 struct System { college *Colleges[MAX_COLLEGES]; }; struct College { char name[MAX_NAME_LEN]; student *Students[MAX_STUDENTS]; }; struct Student { char name[MAX_NAME_LEN]; int id; family *fam; //was typo familiy }; struct Family {

Clear the memory after returning a vector in a function

风格不统一 提交于 2019-12-23 23:14:36
问题 I have a function which processes and stores a lot of data in it and then it returns the results as a vector of class. The amount of data stored in this function is tremendous and I want to clear the storage memory of the function after it finished its job. Is it necessary to do so (does the function automatically clear the memory) or should I clear the memory by some function? Update: vector<customers> process(char* const *filename, vector<int> ID) { vector<customers> list_of_customers;

Linux slab allocator and cache performance

回眸只為那壹抹淺笑 提交于 2019-12-23 22:05:15
问题 From the guide understanding linux kernel 3rd edition , chapter 8.2.10, Slab coloring- We know from Chapter 2 that the same hardware cache line maps many different blocks of RAM. In this chapter, we have also seen that objects of the same size end up being stored at the same offset within a cache. Objects that have the same offset within different slabs will, with a relatively high probability, end up mapped in the same cache line. The cache hardware might therefore waste memory cycles

'calloc' does not automatically consumes memory out of RAM

岁酱吖の 提交于 2019-12-23 21:15:05
问题 According to the answer to this question: Difference between malloc and calloc? Isak Savo explains that: calloc does indeed touch the memory (it writes zeroes on it) and thus you'll be sure the OS is backing the allocation with actual RAM (or swap). This is also why it is slower than malloc (not only does it have to zero it, the OS must also find a suitable memory area by possibly swapping out other processes) So, I decided to try it myself: #include <stdlib.h> #include <stdio.h> #define ONE

concurrent access and free of a data structure

懵懂的女人 提交于 2019-12-23 20:53:23
问题 The problem is like this: I have an array of 500 pointers which point to 500 elements in a doubly linked list. There are 10 threads which run in parallel. Each thread runs 50 loops, and tries to free some element in the list. The list is sorted (contain simple integers), and there are 10 other threads running in parallel, searching for the node that contains a particular integer and access the other satellite data in this node. So the node is like: struct node { int key; // Key used to search

Memory management in asynchronous C++ code

我们两清 提交于 2019-12-23 19:56:43
问题 I have been working with boost::asio for a while now and while I do understand the concept of the asynchronous calls I am still somewhat befuddled by the memory management implications. In normal synchrous code the object lifetime is clear. But consider a scenario similar to the case of the daytime server: There might be multiple active connections which have been accept ed. Each connection now sends and receives some data from a socket, does some work internally and then decides to close the