dynamic-memory-allocation

How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux

ぃ、小莉子 提交于 2019-12-02 19:02:44
I'm looking for some good code examples of dynamic memory allocation using an assembly language under Linux and using system calls, not malloc and friends. What are some of the simplest but effective ways to do this? On Intel 386+ computers. brk(2) . And take a look at ELF . On Linux mmap2 is a sensible system call to use for this at a low level. It takes 6 arguments, so in IA32 you can call it using: mov eax, 192 ; mmap2 xor ebx, ebx ; addr = NULL mov ecx, 4096 ; len = 4096 mov edx, $7 ; prot = PROT_READ|PROT_WRITE|PROT_EXEC mov esi, $22 ; flags = MAP_PRIVATE|MAP_ANONYMOUS mov edi, -1 ; fd =

C - pointer is not null after freeing it

余生颓废 提交于 2019-12-02 13:32:55
Does the value of pointer become NULL after freeing it? int* p = malloc(sizeof(*p)); free(p); if(p==NULL) printf("Null\n"); else printf("Not null\n"); Output: Not null Well, I assume not; Anyway, I have asked a question earlier today : Check it out here: C - How can I free dynamically allocated memory? List* head1 = NULL; insertFront(&head1, 1); insertFront(&head1, 2); print(head1); while (head1) { List *temp = head1; head1 = head1->next; free(temp); } if(head1 == NULL) printf("Null\n"); else printf("Not null\n"); Output in this case: Null In this case after freeing head1 (nodes also) the

Fortran allocatable array lifetime

前提是你 提交于 2019-12-02 10:02:25
Say I have the below code: program test call foo call foo contains subroutine foo integer(8),dimension(:),allocatable:: var1 allocate(var1(10)) ... return end subroutine foo end will the variable var1 get allocated twice? (I guess YES). If it is allocated for each call, will the memory allocated during the first call becomes free? var1 will (attempt to) be allocated every time the ALLOCATE statement is executed (i.e. every time the foo procedure is called). Under the rules of Fortran 90 (only) the allocation status of foo becomes undefined when the procedure ends. A variable with undefined

Why does not free() deallocate all allocated memory locations? [duplicate]

守給你的承諾、 提交于 2019-12-02 07:42:00
问题 This question already has answers here : free() not deallocating memory? (5 answers) Closed 3 years ago . I don't know if I am doing something wrong or if my concept is somewhat wrong #include<stdio.h> #include<stdlib.h> int main() { int *p; p=calloc(3,sizeof(int)); p[0]=10; p[1]=15; p[2]=30; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); free(p); //p=NULL; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); return 0; } When the 2nd, printf() is run, it shows p

Accessing out-of-bounds elements of dynamically allocated arrays / w/o SegFault

有些话、适合烂在心里 提交于 2019-12-02 07:25:33
问题 I'm developing a program in C that uses an array of linked lists (a primitive hash table) as a data type to represent certain date information. The array has twelve elements corresponding to the months of the year, and each month has a linked list that contains nodes of data. I developed the module that used this data type and it worked fine. I later discovered that I was accessing array elements that were out of bounds (for example accessing the 12th element by the index 12 instead of 11).

understand passing parameters by reference with dynamic allocation

自古美人都是妖i 提交于 2019-12-02 05:30:32
I'm trying understand how to pass a parameter by reference in C language. So I wrote this code to test the behavior of parameters passing: #include <stdio.h> #include <stdlib.h> void alocar(int* n){ n = (int*) malloc( sizeof(int)); if( n == NULL ) exit(-1); *n = 12; printf("%d.\n", *n); } int main() { int* n; alocar( n ); printf("%d.\n", *n); return 0; } Here is printed: 12. 0. Example 2: #include <stdio.h> #include <stdlib.h> void alocar(int* n){ *n = 12; printf("%d.\n", *n); } int main() { int* n; n = (int*) malloc(sizeof(int)); if( n == NULL ) exit(-1); alocar( n ); printf("%d.\n", *n);

Why does not free() deallocate all allocated memory locations? [duplicate]

ε祈祈猫儿з 提交于 2019-12-02 04:48:11
This question already has an answer here: free() not deallocating memory? 5 answers I don't know if I am doing something wrong or if my concept is somewhat wrong #include<stdio.h> #include<stdlib.h> int main() { int *p; p=calloc(3,sizeof(int)); p[0]=10; p[1]=15; p[2]=30; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); free(p); //p=NULL; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); return 0; } When the 2nd, printf() is run, it shows p[2]=30, whereas p[0]=p[1]=0 (in gcc ubuntu and some arbitary values in Code::Blocks windows). I have 2 questions. Why free()

Accessing out-of-bounds elements of dynamically allocated arrays / w/o SegFault

蹲街弑〆低调 提交于 2019-12-02 04:08:36
I'm developing a program in C that uses an array of linked lists (a primitive hash table) as a data type to represent certain date information. The array has twelve elements corresponding to the months of the year, and each month has a linked list that contains nodes of data. I developed the module that used this data type and it worked fine. I later discovered that I was accessing array elements that were out of bounds (for example accessing the 12th element by the index 12 instead of 11). But the program worked consistently without incident. I never received a segmentation fault. I have

Windows memory allocation questions

ε祈祈猫儿з 提交于 2019-12-01 18:12:07
I am currently looking into malloc() implementation under Windows. But in my research I have stumbled upon things that puzzled me: First, I know that at the API level, windows uses mostly the HeapAlloc() and VirtualAlloc() calls to allocate memory. I gather from here that the Microsoft implementation of malloc() (that which is included in the CRT - the C runtime) basically calls HeapAlloc() for blocks > 480 bytes and otherwise manage a special area allocated with VirtualAlloc() for small allocations, in order to prevent fragmentation. Well that is all good and well. But then there are other

calloc() and NULL

时光总嘲笑我的痴心妄想 提交于 2019-12-01 17:54:17
问题 I know that calloc allocates memory and writes zeroes to each cell, so my question is: is there a difference between using calloc or using malloc and running over the cells writing NULL to them? Are the zeroes of calloc equivalent to NULL? 回答1: No, they are not always equivalent, but on most popular machines you'll be fine. calloc writes a bit pattern of all-zeros to the allocated memory, but the null pointer value might not be all-bits-zero on some machines (or even just for some types on