dynamic-memory-allocation

Using Dynamic Memory allocation for arrays

空扰寡人 提交于 2019-11-26 12:45:31
问题 How am I supposed to use dynamic memory allocations for arrays? For example here is the following array in which i read individual words from a .txt file and save them word by word in the array: Code: char words[1000][15]; Here 1000 defines the number of words the array can save and each word may comprise of not more than 15 characters. Now I want that that program should dynamically allocate the memory for the number of words it counts. For example, a .txt file may contain words greater that

dynamic allocating array of arrays in C

蓝咒 提交于 2019-11-26 12:41:41
问题 I don\'t truly understand some basic things in C like dynamically allocating array of arrays. I know you can do: int **m; in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be \"easily\" accessed by doing *(*(m + line) + column) . But how should I assign a value to an element from that array? Using gcc the following statement m[line][column] = 12; fails with a segmentation fault. Any article/docs will be appreciated. :-)

Why, or when, do you need to dynamically allocate memory in C?

徘徊边缘 提交于 2019-11-26 12:40:40
问题 Dynamic memory allocation is a very important topic in C programming. However, I\'ve been unable to find a good explanation of what this enables us to do, or why it is required. Can\'t we just declare variables and structs and never have to use malloc()? As a side note, what is the difference between: ptr_one = (int *)malloc(sizeof(int)); and int *ptr_one = malloc(sizeof(int)); 回答1: You need to use dynamic memory when: You cannot determine the maximum amount of memory to use at compile time;

Proper usage of realloc()

旧街凉风 提交于 2019-11-26 11:26:19
问题 From man realloc:The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. So in this code snippet: ptr = (int *) malloc(sizeof(int)); ptr1 = (int *) realloc(ptr, count * sizeof(int)); if(ptr1 == NULL){ //reallocated pointer ptr1 printf(\"Exiting!!\\n\"); free(ptr); exit(0); }else{ free(ptr); //to deallocate the previous memory block pointed by ptr so as not to leave

Is a destructor called when an object goes out of scope?

∥☆過路亽.° 提交于 2019-11-26 09:19:58
问题 For example: int main() { Foo *leedle = new Foo(); return 0; } class Foo { private: somePointer* bar; public: Foo(); ~Foo(); }; Foo::~Foo() { delete bar; } Would the destructor be implicitly called by the compiler or would there be a memory leak? I\'m new to dynamic memory, so if this isn\'t a usable test case, I\'m sorry. 回答1: Yes, automatic variables will be destroyed at the end of the enclosing code block. But keep reading. Your question title asks if a destructor will be called when the

What and where are the stack and heap?

早过忘川 提交于 2019-11-26 03:11:56
问题 Programming language books explain that value types are created on the stack , and reference types are created on the heap , without explaining what these two things are. I haven\'t read a clear explanation of this. I understand what a stack is. But, Where and what are they (physically in a real computer\'s memory)? To what extent are they controlled by the OS or language run-time? What is their scope? What determines the size of each of them? What makes one faster? 回答1: The stack is the

Difference between static memory allocation and dynamic memory allocation

£可爱£侵袭症+ 提交于 2019-11-25 23:36:23
问题 I would like to know what is the difference between static memory allocation and dynamic memory allocation? Could you explain this with any example? 回答1: There are three types of allocation — static, automatic, and dynamic. Static Allocation means, that the memory for your variables is allocated when the program starts. The size is fixed when the program is created. It applies to global variables, file scope variables, and variables qualified with static defined inside functions. Automatic