dynamic-memory-allocation

Why use _mm_malloc? (as opposed to _aligned_malloc, alligned_alloc, or posix_memalign)

99封情书 提交于 2019-11-28 06:48:21
There are a few options for acquiring an aligned block of memory but they're very similar and the issue mostly boils down to what language standard and platforms you're targeting. C11 void * aligned_alloc (size_t alignment, size_t size) POSIX int posix_memalign (void **memptr, size_t alignment, size_t size) Windows void * _aligned_malloc(size_t size, size_t alignment); And of course it's also always an option to align by hand. Intel offers another option. Intel void* _mm_malloc (int size, int align) void _mm_free (void *p) Based on source code released by Intel, this seems to be the method of

how do I allocate one block of memory with new?

梦想与她 提交于 2019-11-28 06:02:24
问题 I have a two dimensional array that I've allocated dynamically using new. The problem is I want to allocate the memory as one connected block instead of in separated pieces to increase processing speed. Does anyone know if it's possible to do this with new, or do I have to use malloc? Here's my code: A = new double*[m]; for (int i=0;i<m;i++) { A[i]= new double[n]; } This code causes a segmentation fault phi = new double**[xlength]; phi[0] = new double*[xlength*ylength]; phi[0][0] = new double

CUDA new delete

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:01:16
Can someone give a clear explanation of how the new and delete keywords would behave if called from __device__ or __global__ code in CUDA 4.2? Where does the memory get allocated, if its on the device is it local or global? It terms of context of the problem I am trying to create neural networks on the GPU, I want a linked representation (Like a linked list, but each neuron stores a linked list of connections that hold weights, and pointers to the other neurons), I know I could allocate using cudaMalloc before the kernel launch but I want the kernel to control how and when the networks are

Thread '<main>' has overflowed its stack when allocating a large array using Box

若如初见. 提交于 2019-11-28 02:14:46
I'm implementing combsort . I'd like to create fixed-size array on the stack, but it shows stack overflow . When I change it to be on the heap (Rust by Example says to allocate in the heap we must use Box ), it still shows stack overflow . fn new_gap(gap: usize) -> usize { let ngap = ((gap as f64) / 1.3) as usize; if ngap == 9 || ngap == 10 { return 11; } if ngap < 1 { return 1; } return ngap; } fn comb_sort(a: &mut Box<[f64]>) { // previously: [f64] let xlen = a.len(); let mut gap = xlen; let mut swapped: bool; let mut temp: f64; loop { swapped = false; gap = new_gap(gap); for i in 0..(xlen -

free allocated memory in 2D dynamic memory allocation array in C++

℡╲_俬逩灬. 提交于 2019-11-28 02:11:44
问题 I recently posted a question about this, but this is a different question. I created a 2D arrays using dynamic memory allocation, after the matrix is used, we need to free the memory by delete it and I dont understand why we cant just use delete [] matrix to delete it instead of the method in the codes below int **matrix; // dynamically allocate an array matrix = new int *[row]; for (int count = 0; count < row; count++) matrix[count] = new int[col]; // free dynamically allocated memory for(

Assembly x86 brk() call use

亡梦爱人 提交于 2019-11-28 01:28:01
问题 i am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would i assign for example the value in a register to that first dynamic memory address? This is what i have so far:` push rbp mov rbp, rsp ;initialize an empy stack to create activation records for the rest of the subroutines mov rax, 0x2d ;linux system call for brk() mov rbx, 0x0 ;to get the adress of the first adress we are allocating we

Dynamic Memory Allocation in MPI

自古美人都是妖i 提交于 2019-11-28 00:35:12
I am new to MPI. I wrote a simple code to display a matrix using multiple process. Say if I have a matrix of 8x8 and launching the MPI program with 4 processes, the 1st 2 rows will be printed my 1st process the 2nd set of 2 rows will be printed by 2nd thread so on by dividing itself equally. #define S 8 MPI_Status status; int main(int argc, char *argv[]) { int numtasks, taskid; int i, j, k = 0; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &taskid); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); int rows, offset, remainPart, orginalRows, height, width; int **a; // int a[S][S]; if(taskid ==

Can a memory block allocated by using operator new/malloc persist beyond end of program execution? [duplicate]

霸气de小男生 提交于 2019-11-27 23:29:24
问题 Possible Duplicate: When you exit a C application, is the malloc-ed memory automatically freed? This question came to my mind when i was reading about how compulsory it is to use delete/free respectively when it comes to dynamic memory allocation in C/C++. I thought if the memory allocation persisted beyond the termination of my program execution, then yes it is compulsory; otherwise, why do i have to worry about freeing up the allocated space? Isn't the OS going to free it up automatically

How to read unlimited characters in C

时光总嘲笑我的痴心妄想 提交于 2019-11-27 19:24:24
问题 How to read unlimited characters into a char* variable without specifying the size? For example, say I want to read the address of an employee that may also take multiple lines. 回答1: You have to start by "guessing" the size that you expect, then allocate a buffer that big using malloc . If that turns out to be too small, you use realloc to resize the buffer to be a bit bigger. Sample code: char *buffer; size_t num_read; size_t buffer_size; buffer_size = 100; buffer = malloc(buffer_size); num

When and why to use malloc?

﹥>﹥吖頭↗ 提交于 2019-11-27 18:42:15
Well, I can't understand when and why it is needed to allocate memory using malloc . Here is my code : #include <stdlib.h> int main(int argc, const char *argv[]) { typedef struct { char *name; char *sex; int age; } student; //Now I can do two things student p; //or student *ptr = (student *)malloc(sizeof(student)); return 0; } Why is it needed to allocate memory when I can just use student p; ? taskinoor malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means you allocate the memory at run time. For example when you don't know the amount of memory during