memory-management

using unallocated memory without error?

纵饮孤独 提交于 2019-12-28 06:47:11
问题 Why does that work? #include <iostream> using namespace std; int main() { float* tab[3]; int i = 0; while(i < 3) { tab[i] = new float[3-i]; i++; } cout << tab[2][7] << endl; tab[2][7] = 6.87; cout << tab[2][7] << endl; i = 0; while(i < 3) delete[] tab[i]; } while this one doesn't? #include <iostream> using namespace std; int main() { float* tab = new float[3]; cout << tab[7] << endl; tab[7] = 6.87; cout << tab[7] << endl; delete[] tab; } I tried both programs on Win XP with MS VS 2008, both

free() on stack memory

泄露秘密 提交于 2019-12-28 06:46:33
问题 I'm supporting some c code on Solaris, and I've seen something weird at least I think it is: char new_login[64]; ... strcpy(new_login, (char *)login); ... free(new_login); My understanding is that since the variable is a local array the memory comes from the stack and does not need to be freed, and moreover since no malloc/calloc/realloc was used the behaviour is undefined. This is a real-time system so I think it is a waste of cycles. Am I missing something obvious? 回答1: You can only free()

Initialize device array in CUDA

╄→гoц情女王★ 提交于 2019-12-28 06:38:13
问题 How do I initialize device array which is allocated using cudaMalloc() ? I tried cudaMemset , but it fails to initialize all values except 0.code , for cudaMemset looks like below, where value is initialized to 5. cudaMemset(devPtr,value,number_bytes) 回答1: As you are discovering, cudaMemset works like the C standard library memset . Quoting from the documentation: cudaError_t cudaMemset ( void * devPtr, int value, size_t count ) Fills the first count bytes of the memory area pointed to by

Freeing memory allocated in a different DLL

喜欢而已 提交于 2019-12-28 06:33:06
问题 I have an EXE file using a DLL file which is using another DLL file. This situation has arisen: In DLL file 1: class abc { static bool FindSubFolders(const std::string & sFolderToCheck, std::vector< std::string > & vecSubFoldersFound); } In DLL file 2: void aFunction() { std::vector<std::string> folders; std::string sLocation; ... abc::FindSubFolders(sLocation, folders) } In release mode, everything works fine. But in debug mode, I come up with an assertion failure in the destructor of one of

Allocating A Large (5000+) Array

喜夏-厌秋 提交于 2019-12-28 06:24:19
问题 I am working on an application were there are three possible sizes for the data entered: small: 1000 elements medium= 5000 elements large= 500,000 elements The problem is that I can't allocate the large array. It seems that a size larger than 5000 is not accepted. I get a run time error when I do the following: long size=1000; char ch; int arr[size]; ch=getch(); if(ch==..) size=...; Sizes of 1000 and 5000 seem to work fine, but how can I make an array of size 500k in this way? 回答1: You can

Is there a way to retrieve a C# app's current memory usage?

核能气质少年 提交于 2019-12-28 06:17:52
问题 I am automating some profiling tasks and want to log heap space and generation sizes real-time. The profiling API seems awfully complicated for what I need, and it seems to listen in on individual allocations and collections, which isn't that important to me. Profiling tools are a great help of course, but I was looking for a more flexible, programmable interface. 回答1: The term 'current memory usage' is a little loosely defined. Do you mean the working set? Whatever it means, you can use

What is the array form of 'delete'?

对着背影说爱祢 提交于 2019-12-28 05:59:10
问题 When I compiled a code using the array name as a pointer, and I deleted the array name using delete , I got a warning about deleting an array without using the array form (I don't remember the exact wording). The basic code was: int data[5]; delete data; So, what's the array form of delete? 回答1: The array form of delete is: delete [] data; Edit: But as others have pointed out, you shouldn't be calling delete for data defined like this: int data[5]; You should only call it when you allocate

Call destructor and then constructor (resetting an object)

拥有回忆 提交于 2019-12-28 05:57:19
问题 I want to reset an object. Can I do it in the following way? anObject->~AnObject(); anObject = new(anObject) AnObject(); // edit: this is not allowed: anObject->AnObject(); This code is obviously a subset of typical life cycle of an object allocated by in placement new: AnObject* anObject = malloc(sizeof(AnObject)); anObject = new (anObject) AnObject(); // My step 2. // ... anObject->~AnObject(); // My step 1. free(anObject) // EDIT: The fact I used malloc instead of new doesn't carry any

Do Kernel pages get swapped out?

允我心安 提交于 2019-12-28 05:51:46
问题 Pertaining to the Linux kernel, do "Kernel" pages ever get swapped out ? Also, do User space pages ever get to reside in ZONE_NORMAL ? 回答1: No, kernel memory is unswappable. 回答2: Kernel pages are not swappable. But it can be freed. UserSpace Pages can reside in ZONE_NORMAL. Linux System Can be configured either to use HIGHMEM or not. If ZONE_HIGHMEM is configured , then the userspace processes will get its memory from the HIGHMEM else userspace processes will get memory from ZONE_NORMAL. 回答3:

Lazy initialisation and retain cycle

淺唱寂寞╮ 提交于 2019-12-28 05:37:31
问题 While using lazy initialisers, is there a chance of having retain cycles? In a blog post and many other places [unowned self] is seen class Person { var name: String lazy var personalizedGreeting: String = { [unowned self] in return "Hello, \(self.name)!" }() init(name: String) { self.name = name } } I tried this class Person { var name: String lazy var personalizedGreeting: String = { //[unowned self] in return "Hello, \(self.name)!" }() init(name: String) { print("person init") self.name =