dynamic-memory-allocation

C - How can I free dynamically allocated memory?

爷,独闯天下 提交于 2020-01-15 08:32:06
问题 Have a look at this piece of codes, it's part of a linked list. int main() { List* head1 = NULL; insertFront(&head1, 1); insertFront(&head1, 2); print(head1); free(head1); return 0; } another function is: void insertFront(List** head, int value) { List* node = (List*)malloc(sizeof(List)); node->data = value; node->next = NULL; node->next = *head; *head = node; //free(node); essentially I am not freeing node } My questions are: Is my code going to cause memory leak problem? Should I need to

C - How can I free dynamically allocated memory?

断了今生、忘了曾经 提交于 2020-01-15 08:31:53
问题 Have a look at this piece of codes, it's part of a linked list. int main() { List* head1 = NULL; insertFront(&head1, 1); insertFront(&head1, 2); print(head1); free(head1); return 0; } another function is: void insertFront(List** head, int value) { List* node = (List*)malloc(sizeof(List)); node->data = value; node->next = NULL; node->next = *head; *head = node; //free(node); essentially I am not freeing node } My questions are: Is my code going to cause memory leak problem? Should I need to

Why can't I dynamically allocate memory of this string of a struct?

佐手、 提交于 2020-01-06 19:49:13
问题 Let's say for example, I have a struct: typedef struct person { int id; char *name; } Person; Why can't I do the following: void function(const char *new_name) { Person *human; human->name = malloc(strlen(new_name) + 1); } 回答1: You need to allocate space for human first: Person *human = malloc(sizeof *human); human->name = malloc(strlen(new_name) + 1); strcpy(human->name, new_name); 回答2: You have to allocate memory for the structure Person . The pointer should point to the memory allocated

Runtime allocation of multidimensional array

别说谁变了你拦得住时间么 提交于 2020-01-06 06:09:41
问题 So far I thought that the following syntax was invalid, int B[ydim][xdim]; But today I tried and it worked! I ran it many times to make sure it did not work by chance, even valgrind didn't report any segfault or memory leak !! I am very surprised. Is it a new feature introduced in g++? I always have used 1D arrays to store matrices by indexing them with correct strides as done with A in the program below. But this new method, as with B, is so simple and elegant that I have always wanted. Is

What can I use instead of std::aligned_alloc in MS Visual Studio 2013?

為{幸葍}努か 提交于 2020-01-04 02:16:27
问题 I would like to use C++11's std::aligned_alloc , but unfortunately it isn't available with Microsoft Visual Studio 2013. I'm considering, intsead, implementing aligned_alloc on my own. How should an implementation look like? The following for example doesn't compile, because it cannot convert from void* to void*& . template<typename T> T* aligned_alloc( std::size_t size, std::size_t align ) { T* ptr = new T[size + align]; std::align(align, size, reinterpret_cast<void*>(ptr), align + size);

C++ dynamically allocated array of statically dimensioned arrays

自作多情 提交于 2020-01-02 15:48:03
问题 I need to create a structure that holds a variable number of 'char[2]'s, i.e. static arrays of 2 chars. My question is, how do I allocate memory for x number of char[2]. I tried this (assuming int x is defined): char** m = NULL; m = new char[x][2]; ... delete [] m; (it didn't work) I realise I could use std::vector<char[2]> as a container, but I'm curious as to how it would be done with raw pointers. I am very new to C++ and trying to learn. 回答1: In your code, the type of 'm' doesn't match

C++ dynamically allocated array of statically dimensioned arrays

不羁岁月 提交于 2020-01-02 15:47:30
问题 I need to create a structure that holds a variable number of 'char[2]'s, i.e. static arrays of 2 chars. My question is, how do I allocate memory for x number of char[2]. I tried this (assuming int x is defined): char** m = NULL; m = new char[x][2]; ... delete [] m; (it didn't work) I realise I could use std::vector<char[2]> as a container, but I'm curious as to how it would be done with raw pointers. I am very new to C++ and trying to learn. 回答1: In your code, the type of 'm' doesn't match

Can i allocate memory faster by using multiple threads?

最后都变了- 提交于 2020-01-02 05:59:33
问题 If i make a loop that reserves 1kb integer arrays, int[1024], and i want it to allocate 10000 arrays, can i make it faster by running the memory allocations from multiple threads? I want them to be in the heap. Let's assume that i have a multi-core processor for the job. I already did try this, but it decreased the performance. I'm just wondering, did I just make bad code or is there something that i didn't know about memory allocation? Does the answer depend on the OS? please tell me how it

Dynamic memory allocation question

人走茶凉 提交于 2020-01-01 04:29:34
问题 when you allocate dynamic memory on the heap using a pointer, char *buffer_heap = new char[15]; it would be represented in memory as: ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þþþ why doesn't there be a NULL terminating character at the end instead of ýýýý««««««««þþþ? 回答1: Í is byte 0xCD, which the Windows debug allocator writes into your 15 bytes of memory to indicate that it is uninitialised heap memory. Uninitialized stack would be 0xCC. The idea is that if you ever read memory and unexpectedly get this

Realloc equivalent in C++

拟墨画扇 提交于 2019-12-29 07:44:08
问题 Yes, another realloc vs. std::vector question. I know what you're going to say, and I agree, forget manual memory allocation, and just use a std::vector . Well unfortunately my professor has forbidden me to use anything from the STL for this assignment. So yeah, I have a dynamic array of T and I need it to be resizable, and I can't use std::vector . I could return to the dark ages and do the whole thing with malloc and family, but if I could use new that would be totally awesome. I've read