dynamic-memory-allocation

How are malloc and free implemented?

回眸只為那壹抹淺笑 提交于 2019-11-27 16:12:21
问题 I want to implement my own dynamic memory management system in order to add new features that help to manage memory in C++. I use Windows (XP) and Linux (Ubuntu). What is needed to implement functions like 'malloc' and 'free'? I think that I have to use lowest level system calls. For Windows, I have found the functions: GetProcessHeap, HeapAlloc, HeapCreate, HeapDestroy and HeapFree. For Linux, I have not found any system calls for heap management. On Linux, malloc and free are system calls,

Allocating struct with variable length array member

…衆ロ難τιáo~ 提交于 2019-11-27 09:17:21
I know I can do new char[n] to create an array of n chars. This works even when n is not a compile time constant. But lets say I wanted a size variable followed by n chars: My first attempt at this is the following: struct Test { std::size_t size; char a[]; }; However it seems new Test[n] doesn't do what I expect, and instead allocates n size s. I've also found that sizeof(std::string) is 4 at ideone, so it seems it can allocate both the size and the char array in one block. Is there a way I can achieve what I described (presumably what std::string already does)? While you can do this (and it

How do I use a structure?

白昼怎懂夜的黑 提交于 2019-11-27 08:39:34
问题 Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to

Simple C implementation to track memory malloc/free?

大兔子大兔子 提交于 2019-11-27 08:28:17
programming language: C platform: ARM Compiler: ADS 1.2 I need to keep track of simple melloc/free calls in my project. I just need to get very basic idea of how much heap memory is required when the program has allocated all its resources. Therefore, I have provided a wrapper for the malloc/free calls. In these wrappers I need to increment a current memory count when malloc is called and decrement it when free is called. The malloc case is straight forward as I have the size to allocate from the caller. I am wondering how to deal with the free case as I need to store the pointer/size mapping

dynamic allocation/deallocation of 2D & 3D arrays

独自空忆成欢 提交于 2019-11-27 07:20:28
I know about algorithms to allocate/deallocate a 2D array dynamically, however I'm not too sure about the same for 3D arrays. Using this knowledge and a bit of symmetry, I came up with the following code. (I had a hard time visualizing in 3D during coding). Please comment on the correctness and suggest any better alternative (efficiency-wise or intuitively), if any. Also, I think both these 2D and 3D arrays can be accessed normally like static arrays like arr2D[2][3] and arr3D[2][3][2]. Right? Code for 2D //allocate a 2D array int** allocate2D(int rows,int cols) { int **arr2D; int i; arr2D =

What are the contents of the memory just allocated by `malloc()`?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 07:02:50
问题 I was curious about what exactly a pointer holds, after malloc() was used to allocate memory space? The manpage tells me that calloc() initializes the allocated memory space with zero. The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized . If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free(). and The calloc() function allocates memory for an array of nmemb

ALLOCATABLE arrays or POINTER arrays?

和自甴很熟 提交于 2019-11-27 05:54:00
问题 I am writing a new code in Fortran and hesitating between using allocatable arrays or pointer arrays. I read somewhere that allocatable arrays have significant advantages over pointer arrays: 1) More efficient because they are always contiguous in memory 2) No memory leaks are possible Can someone confirm this? Which one would you advise to use? What are the results in term of execution speed of the code between these two alternatives? 回答1: Allocatable arrays can result in more efficient code

Proper usage of realloc()

落爺英雄遲暮 提交于 2019-11-27 04:59:58
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 orphaned blocks of memory when ptr=ptr1 executes and ptr moves on to another block ptr = ptr1; /

Using Dynamic Memory allocation for arrays

孤者浪人 提交于 2019-11-27 04:39:42
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 1000. Now I want that the program should count the number of words and allocate the memory accordingly

dynamic allocating array of arrays in C

一笑奈何 提交于 2019-11-27 03:59:43
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. :-) The m[line][column] = 12 syntax is ok (provided line and column are in range). However, you didn't write