allocation

malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

大憨熊 提交于 2019-11-27 18:11:39
I acknowledge that all three of these have a different meaning. But, I don't understand on what particular instances would each of these apply. Can anyone share an example for each of these? Thank you. malloc(sizeof(int)) malloc(sizeof(int *)) (int *)malloc(sizeof(int)) malloc(sizeof(int)) means you are allocating space off the heap to store an int . You are reserving as many bytes as an int requires. This returns a value you should cast to int * . (A pointer to an int .) As some have noted, typical practice in C is to let implicit casting take care of this. malloc(sizeof(int*)) means you are

Memory Allocation/Deallocation? [closed]

自作多情 提交于 2019-11-27 17:13:15
I have been looking at memory allocation lately and I am a bit confused about the basics. I haven't been able to wrap my head around the simple stuff. What does it mean to allocate memory? What happens? I would appreciated answers to any of these questions: Where is the "memory" that is being allocated? What is this "memory"? Space in an array? Or something else? What happens exactly when this "memory" gets allocated? What happens exactly when the memory gets deallocated? It would also really help me if someone could answer what malloc does in these C++ lines: char* x; x = (char*) malloc (8);

How to avoid long chain of free's (or deletes) after every error check in C?

百般思念 提交于 2019-11-27 14:41:05
Suppose I write my code very defensively and always check the return types from all the functions that I call. So I go like: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b = get_binder('regular binder'); // second allocation if (!b) { free(mem); return NULL; } struct file* f = mk_file(); // third allocation if (!f) { free(mem); free_binder(b); return NULL; } // ... } Notice how quickly free() things get out of control. If some of the function fails, I have to free every single allocation before. The code quickly becomes ugly and all

CUDA allocating array of arrays

爷,独闯天下 提交于 2019-11-27 14:32:15
I have some trouble with allocate array of arrays in CUDA. void ** data; cudaMalloc(&data, sizeof(void**)*N); // allocates without problems for(int i = 0; i < N; i++) { cudaMalloc(data + i, getSize(i) * sizeof(void*)); // seg fault is thrown } What did I wrong? fabrizioM You have to allocate the pointers to a host memory, then allocate device memory for each array and store it's pointer in the host memory. Then allocate the memory for storing the pointers into the device and then copy the host memory to the device memory. One example is worth 1000 words: __global__ void multi_array_kernel( int

Escape analysis in Java

烂漫一生 提交于 2019-11-27 11:13:25
As far as I know the JVM uses escape analysis for some performance optimisations like lock coarsening and lock elision. I'm interested if there is a possibility for the JVM to decide that any particular object can be allocated on stack using escape analysis. Some resources make me think that I am right. Is there JVMs that actually do it? benmmurphy I don't think it does escape analysis for stack allocation. example: public class EscapeAnalysis { private static class Foo { private int x; private static int counter; public Foo() { x = (++counter); } } public static void main(String[] args) {

In what cases should I use memcpy over standard operators in C++?

那年仲夏 提交于 2019-11-27 10:31:38
问题 When can I get better performance using memcpy or how do I benefit from using it? For example: float a[3]; float b[3]; is code: memcpy(a, b, 3*sizeof(float)); faster than this one? a[0] = b[0]; a[1] = b[1]; a[2] = b[2]; 回答1: Efficiency should not be your concern. Write clean maintainable code. It bothers me that so many answers indicate that the memcpy() is inefficient. It is designed to be the most efficient way of copy blocks of memory (for C programs). So I wrote the following as a test:

How to allocate a 2D array of pointers in C++

一笑奈何 提交于 2019-11-27 07:20:17
I'm trying to make a pointer point to a 2D array of pointers. What is the syntax and how would I access elements? By the letter of the law, here's how to do it: // Create 2D array of pointers: int*** array2d = new (int**)[rows]; for (int i = 0; i < rows; ++i) { array2d[i] = new (int*)[cols]; } // Null out the pointers contained in the array: for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { array2d[i][j] = NULL; } } Be careful to delete the contained pointers, the row arrays, and the column array all separately and in the correct order. However, more frequently in C++ you'd

Why does the compiler allocate more than needed in the stack?

和自甴很熟 提交于 2019-11-27 05:37:18
I have a simple C program. Let's say, for example, I have an int and a char array of length 20. I need 24 bytes in total. int main() { char buffer[20]; int x = 0; buffer[0] = 'a'; buffer[19] = 'a'; } The stack needs to be aligned to a 16 bytes boundary, so I presume a compiler will reserve 32 bytes. But when I compile such a program with gcc x86-64 and read the output assembly, the compiler reserves 64 bytes. ..\gcc -S -o main.s main.c Gives me: .file "main.c" .def __main; .scl 2; .type 32; .endef .text .globl main .def main; .scl 2; .type 32; .endef .seh_proc main main: pushq %rbp # RBP is

Correct way to cap Mathematica memory use?

心不动则不痛 提交于 2019-11-27 05:22:44
问题 Under a 32-bit operating system, where maximum memory allocated to any one program is limited, Mathematica gracefully terminates the kernel and returns a max memory allocation error. On a 64-bit OS however, Mathematica will freely use all the memory available and grind the system to a halt. Therefore, what is the correct way to cap memory usage? One could use MemoryConstrained combined with $Pre or CellEvaluationFunction but I would rather not tie up either of those for this purpose, or have

Declare large array on Stack

风格不统一 提交于 2019-11-27 04:40:30
问题 I am using Dev C++ to write a simulation program. For it, I need to declare a single dimensional array with the data type double . It contains 4200000 elements - like double n[4200000] . The compiler shows no error, but the program exits on execution. I have checked, and the program executes just fine for an array having 5000 elements. Now, I know that declaring such a large array on the stack is not recommended. However, the thing is that the simulation requires me to call specific elements