dynamic-memory-allocation

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

那年仲夏 提交于 2019-12-17 18:16:06
问题 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

Dynamically allocate user inputted string

末鹿安然 提交于 2019-12-17 14:58:08
问题 I am trying to write a function that does the following things: Start an input loop, printing '> ' each iteration. Take whatever the user enters (unknown length) and read it into a character array, dynamically allocating the size of the array if necessary. The user-entered line will end at a newline character. Add a null byte, '\0' , to the end of the character array. Loop terminates when the user enters a blank line: '\n' This is what I've currently written: void input_loop(){ char *str =

Dynamic memory access only works inside function

∥☆過路亽.° 提交于 2019-12-16 19:05:26
问题 This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data outside the function, I get crashes or other unexpected program behavior. Here is a MCVE: #include <stdlib.h> #include <stdio.h> void create_array (int* data, int size) { data = malloc(sizeof(*data) * size); for(int i=0; i<size; i++) { data[i] = i

Dynamic memory access only works inside function

末鹿安然 提交于 2019-12-16 19:05:23
问题 This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data outside the function, I get crashes or other unexpected program behavior. Here is a MCVE: #include <stdlib.h> #include <stdio.h> void create_array (int* data, int size) { data = malloc(sizeof(*data) * size); for(int i=0; i<size; i++) { data[i] = i

Why does the top code work and the bottom code not in c++ for dynamic matrix allocation? [closed]

扶醉桌前 提交于 2019-12-14 03:33:27
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I've gotten rid of the codes that do work as they were distracting. What I'm trying to do is allocate a single block of memory for a 3D array. I could

Overhead to using std::vector?

不打扰是莪最后的温柔 提交于 2019-12-14 00:16:37
问题 I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector ? To give a crude example, if I had to store an array of n integers, where n <= 16, say. I could implement it using int* data = new int[n]; //assuming n is set beforehand or using a vector: std::vector<int> data; Is it absolutely always a better idea to use a std::vector or could there be practical situations where manually allocating the dynamic memory

Why the address of structure and next is not same?

一笑奈何 提交于 2019-12-13 23:16:27
问题 #include<stdio.h> #include<stdlib.h> #include<malloc.h> struct node { int id; struct node *next; }; typedef struct node NODE; int main() { NODE *hi; printf("\nbefore malloc\n"); printf("\naddress of node is: %p",hi); printf("\naddress of next is: %p",hi->next); return 0; } The output is: before malloc address of node is: 0x7ffd37e99e90 address of next is: 0x7ffd37e9a470 Why both are not same? 回答1: TL;DR Your code provokes Undefined Behavior , as already mentioned in Morlacke's Answer. Other

Can I Allocate a Block of Memory with new?

一笑奈何 提交于 2019-12-13 18:54:27
问题 So given this c structure: typedef struct { int* arr1; int* arr2; } myStruct; This answer described using a single malloc to allocate a myStruct and it's arrays at the same time: myStruct* p = malloc(sizeof(*p) + 10 * sizeof(*p->arr1) + 10 * num * sizeof(*p->arr2); if(p != NULL) { p->arr1 = (int*)(p + 1); p->arr2 = p->arr1 + 10; } What I'd like to know is there a similar way to do this with new ? Obviously I want to be able to allocate to a size that I receive at runtime as is done with the C

What is the difference between malloc array and regular array when in both I have to specify memory size?

淺唱寂寞╮ 提交于 2019-12-13 18:11:36
问题 What is the difference between malloc() -ed array and regular array when in both I have to specify memory size, for example char* arr = malloc(50 * sizeof(char)) vs int arr [50] 回答1: Well, there are too many differences. To start with, read about arrays are not pointers and vice versa . That said, three major differences from the usability point of view ( which I feel you're interested about ) An array has a scope limited to its enclosing block, but dynamically allocated memories live unless

Is Big-O of the C++ statement 'delete [] Q;' O(1) or O(n)?

☆樱花仙子☆ 提交于 2019-12-13 12:27:19
问题 Title is self-explanatory. Very easy question. I think it's O(n) but wanted to verify before my final tomorrow. 回答1: The short answer is... it depends. If Q is a pointer to an array of objects that have destructors, then delete[] Q will need to call all of those destructors. This will call O(n) destructors, where n is the number of elements in the array. On the other hand, if Q points to an array of objects that don't have destructors (for example, int s or simple struct s), then no