allocation

Memory allocated with alloca gets freed at end of function or at end of scope?

拜拜、爱过 提交于 2019-12-23 07:31:21
问题 If I have a function like this: void bla(int size) { while(b){ char tmp[size]; ...... } } tmp gets freed at each iteration of the while loop, right? If I write this function: void bla(int size) { while(b){ char* tmp = alloca(size); ...... } } tmp gets freed at end of scope or at end of function? 回答1: It will be freed at end of function, but since you call alloca() inside the loop you'll likely get stack overflow. If size doesn't change within the function you should call alloca() before the

How to create a Large Distance Matrix?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 03:57:13
问题 How to allocate a huge distance matrix in an appropriate way to avoid " allocation is unable " error. Imagine you have a 100.000 points randomly spreaded over some space. How can one cleverly create a matrix or "dist"-object, which represents the the half of DistMatrix. Maybe it should be another object, which will be able efficiently allocate the large number of distances. You can get the polygonial object from the following link: https://www.dropbox.com/sh/65c3rke0gi4d8pb/LAKJWhwm-l # Load

How to create a Large Distance Matrix?

半世苍凉 提交于 2019-12-23 03:57:06
问题 How to allocate a huge distance matrix in an appropriate way to avoid " allocation is unable " error. Imagine you have a 100.000 points randomly spreaded over some space. How can one cleverly create a matrix or "dist"-object, which represents the the half of DistMatrix. Maybe it should be another object, which will be able efficiently allocate the large number of distances. You can get the polygonial object from the following link: https://www.dropbox.com/sh/65c3rke0gi4d8pb/LAKJWhwm-l # Load

How to dynamically allocate 2d array that's 16B aligned

北城以北 提交于 2019-12-23 03:42:26
问题 I'd like to allocate 2D array (square matrix) using memalign with 16B instead of using just malloc . I have A =(float **) malloc( (*dim) * sizeof(float*)); for ( i = 0 ; i < (*dim) ; i++) { A[i] = (float*) malloc(sizeof(float)*(*dim)); } how can I change code above with memalign . 回答1: With malloc() you need to request 15 extra bytes and then round-up the returned pointer to the nearest multiple of 16, e.g.: void* p = malloc(size + 15); void* paligned; if (!p) { /* handle errors */ } paligned

To preallocate or not to preallocate lists in Python

徘徊边缘 提交于 2019-12-22 04:30:35
问题 When should and shouldn't I preallocate a list of lists in python? For example, I have a function that takes 2 lists and creates a lists of lists out of it. Quite like, but not exactly, matrix multiplication. Should I preallocate the result, X = Len(M) Y = Len(F) B = [[None for y in range(Y)] for x in range(X)] for x in range(X): for y in range(Y): B[x][y] = foo(M[x], F[y]) return B or dynamically create it as I go? B = [] for m in M: B.append([]) for f in F: B[-1].append(foo(m, f)) return B

Is there an alternative way to free dynamically allocated memory in C - not using the free() function?

走远了吗. 提交于 2019-12-21 16:54:50
问题 I am studying for a test, and I was wondering if any of these are equivalent to free(ptr): malloc(NULL); calloc(ptr); realloc(NULL, ptr); calloc(ptr, 0); realloc(ptr, 0); From what I understand, none of these will work because the free() function actually tells C that the memory after ptr is available again for it to use. Sorry that this is kind of a noob question, but help would be appreciated. 回答1: Actually, the last of those is equivalent to a call to free() . Read the specification of

HeapCreate, HeapAlloc in Linux, private allocator for Linux

╄→尐↘猪︶ㄣ 提交于 2019-12-21 04:55:29
问题 In Windows, for very demanding applications, a programmer may use HeapCreate, HeapAlloc in order to better manage and control the allocation of memory- speed it up (aka private allocators). What is the equivalent in Linux c++ programming? 回答1: If you want to use your own private allocator, then use mmap() to map an amount of memory into your process, then you can use that memory as you like. Open a file descriptor to /dev/zero , and then use that as the 'fildes' parameter to mmap() . See man

Why use [ClassName alloc] instead of [[self class] alloc]?

南楼画角 提交于 2019-12-21 03:25:18
问题 I'm reading through Mark Dalrymple's Learn Objective-C on the Mac (only at the chapter on Protocols, so still relatively newbish) and trying to figure something out: Why would you ever reference a class by its own name? If I had a class called Foo , why would I ever want to write, say, [[Foo alloc] init] and not [[[self class] alloc] init] If I had a subclass Bar, wouldn't the first option invalidate me from writing [[Bar alloc] init] whereas the second option would allow it? When would the

HDF5 struct with pointer array

我与影子孤独终老i 提交于 2019-12-21 02:28:54
问题 I am trying to write a HDF5 file with a structure which contains an int and a float* typedef struct s1_t { int a; float *b; } s1_t; However, upon allocating the float* and putting values into it, I still can't output the data in my hdf5 file. I believe this is because the write function assumes that the compound data type is contiguous when a dynamically allocated array will not be. Is there any way around this problem by still using a pointer array? /* * This example shows how to create a

Python function slows down with presence of large list

浪尽此生 提交于 2019-12-21 01:45:15
问题 I was testing the speeds of a few different ways to do complex iterations over some of my data, and I found something weird. It seems that having a large list local to some function slows down that function considerably, even if it is not touching that list. For example, creating 2 independent lists via 2 instances of the same generator function is about 2.5x slower the second time. If the first list is removed prior to creating the second, both iterators go at the same spee. def f(): l1, l2