malloc

Correct use of free() when deallocating a 2d matrix in c

十年热恋 提交于 2021-01-27 09:10:58
问题 I'm just starting to learn coding in c, and I have a few questions regarding 2d matrices in combination with the free() command. I know that you first need to create an array with pointer, pointing to the different columns of the matrix: double **array = (double **)malloc(5*sizeof(double *)); for(int n = 0; n<5; n++){ array[n] = (double *) malloc(6*sizeof(double)); I know that the correct way to then deallocate this matrix is to first deallocate the individual rows and then the array pointer

Cannot pass my matrix of structs to a function after using malloc function

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-20 10:41:20
问题 I need to create a matrix containing structs. My struct: typedef struct { int a; int b; int c; } myStruct; My method to create matrix: myStruct (*matrix)[WIDTH] = malloc(HEIGHT * sizeof *matrix); How can I pass my matrix to a function to do something? 回答1: In C89, WIDTH must be a constant and you can simply pass the matrix this way: #include <stdlib.h> #define WIDTH 5 typedef struct { int a; int b; int c; } myStruct; void init_matrix(myStruct matrix[][WIDTH], int height) { for (int i = 0; i <

CUDA/C - Using malloc in kernel functions gives strange results

自古美人都是妖i 提交于 2021-01-18 07:32:26
问题 I'm new to CUDA/C and new to stack overflow. This is my first question. I'm trying to allocate memory dynamically in a kernel function, but the results are unexpected. I read using malloc() in a kernel can lower performance a lot, but I need it anyway so I first tried with a simple int ** array just to test the possibility, then I'll actually need to allocate more complex structs. In my main I used cudaMalloc() to allocate the space for the array of int * , and then I used malloc() for every

CUDA/C - Using malloc in kernel functions gives strange results

混江龙づ霸主 提交于 2021-01-18 07:30:30
问题 I'm new to CUDA/C and new to stack overflow. This is my first question. I'm trying to allocate memory dynamically in a kernel function, but the results are unexpected. I read using malloc() in a kernel can lower performance a lot, but I need it anyway so I first tried with a simple int ** array just to test the possibility, then I'll actually need to allocate more complex structs. In my main I used cudaMalloc() to allocate the space for the array of int * , and then I used malloc() for every

CUDA/C - Using malloc in kernel functions gives strange results

僤鯓⒐⒋嵵緔 提交于 2021-01-18 07:30:08
问题 I'm new to CUDA/C and new to stack overflow. This is my first question. I'm trying to allocate memory dynamically in a kernel function, but the results are unexpected. I read using malloc() in a kernel can lower performance a lot, but I need it anyway so I first tried with a simple int ** array just to test the possibility, then I'll actually need to allocate more complex structs. In my main I used cudaMalloc() to allocate the space for the array of int * , and then I used malloc() for every

Why does it prints that the size of char is 4 byte? [duplicate]

时光毁灭记忆、已成空白 提交于 2020-11-29 21:06:44
问题 This question already has answers here : malloc in C: same sizeof before and after? (5 answers) Determine size of dynamically allocated memory in C (15 answers) Closed 6 days ago . In this program I have located char pointer. I have located 1 byte of memory in heap. But when I print its size it shows 4 bytes. please help. //CODES// #include <stdio.h> #include <stdlib.h> int main(){ char *ptr; ptr=(char*)malloc(1); printf("The size is %d.",sizeof(ptr)); } 来源: https://stackoverflow.com