calloc

C语言的内存管理

匿名 (未验证) 提交于 2019-12-02 23:52:01
堆和栈的区别: 栈的特征 执行的速度相对较快; 空间较小; 生存期由系统决定; 作用域较小; 有名空间,可以通过变量名或者数据名访问; 堆的特征 执行的速度相对较慢; 空间较大; 生存期由“自己”决定,malloc申请,free释放; 作用域很大(整个程序都可以访问); 无名空间,只能通过指针使用; C语言空间的申请 malloc 功能: 分配 size 字节的未初始化内存。若分配成功,则返回指向分配内存块最低位(首位)字节的,为任何拥有基础对齐的对象类型对齐的指针。 头文件: #include<stdlib.h> 原型: void* malloc( size_t size ); 参数: size - 要分配的字节数 返回值: 成功时:返回指向新分配内存的指针。为避免内存泄漏,必须用 free() 或 realloc() 解分配返回的指针。 失败时:返回空指针。 说明: malloc申请的空间为连续空间;malloc申请的是没有初始化的空间; 返回值类型是void * 该类型表明malloc返回的地址空间中的数据类型是不确定,必须经过强制类型转换才可以使用。 realloc 功能: 先判断当前的指针是否有足够的连续空间,如果有,扩大mem_address指向的地址,并且将mem_address返回,如果空间不够,先按照newsize指定的大小分配空间

2d array, using calloc in C

*爱你&永不变心* 提交于 2019-12-02 17:59:45
问题 I'm trying to create a 2D array of chars to storage lines of chars. For Example: lines[0]="Hello"; lines[1]="Your Back"; lines[2]="Bye"; Since lines has to be dynamically cause i don't know how many lines i need at first. Here is the code i have: int i; char **lines= (char**) calloc(size, sizeof(char*)); for ( i = 0; i < size; i++ ){ lines[i] = (char*) calloc(200, sizeof(char)); } for ( i = 0; i < size; i++ ){ free(lines[i]); } free(lines); I know that each line can't go over 200 chars. I

Calloc for an array of array with negative index in C

我的未来我决定 提交于 2019-12-02 17:45:37
问题 I have an array of array with negative index. It is an array which has real dimensions [dim_y + 40][dim_x + 40] but the user uses the array like it has dimensions [dim_y][dim_x]. First i had global and already defined the dimensions dim_x, dim_y, so i had this int map_boundaries[dim_y + 40][dim_x + 40]; int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20]; It worked fine. Now i need the dimensions dim_y and dim_x to be variable, and with this i mean that i want the array 'map' to

2D array dynamic memory allocation crashes [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-02 08:14:23
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I correctly set up, access, and free a multidimensional array in C? I am trying to dynamically allocate memory for a 2D array using calloc. The columns are fixed as 2 so its only the rows that are dynamic. Here is what I have been trying : unsigned int **pts, rows; int main() { //some code pts = (unsigned int **)calloc(2*rows, sizeof (unsigned int **)); } //The code to access the array : for(k=1;k<=i;k++)

Calloc for an array of array with negative index in C

三世轮回 提交于 2019-12-02 07:50:11
I have an array of array with negative index. It is an array which has real dimensions [dim_y + 40][dim_x + 40] but the user uses the array like it has dimensions [dim_y][dim_x]. First i had global and already defined the dimensions dim_x, dim_y, so i had this int map_boundaries[dim_y + 40][dim_x + 40]; int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20]; It worked fine. Now i need the dimensions dim_y and dim_x to be variable, and with this i mean that i want the array 'map' to not have fixed size but dynamic, i need to read the dim_y, dim_x from a user and the array 'map' to be

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

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()

2D array dynamic memory allocation crashes [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-02 03:10:42
Possible Duplicate: How do I correctly set up, access, and free a multidimensional array in C? I am trying to dynamically allocate memory for a 2D array using calloc. The columns are fixed as 2 so its only the rows that are dynamic. Here is what I have been trying : unsigned int **pts, rows; int main() { //some code pts = (unsigned int **)calloc(2*rows, sizeof (unsigned int **)); } //The code to access the array : for(k=1;k<=i;k++) { printf("\nX%d=",k); scanf("%d",&pts[k][0]); printf("\nY%d=",k); scanf("%d",&pts[k][1]); } But the problem is, while accessing the array, the program crashes. I am

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

can calloc or malloc be used to allocate ONLY physical memory in OSX?

混江龙づ霸主 提交于 2019-12-01 11:26:35
I am playing around with the c functions malloc and calloc and I have some questions. I want to see if I can use these 2 functions to allocate only physical memory, my mac has 4gb or ram and when I use malloc I can allocate way more than 4gb, which means malloc allocate both physical and virtual memory. I have a couple of questions: is there any function I can use, so that I can only allocate the physical memory (w/o allocating the virtual mem) when calling malloc and calloc and when the pointers return, is there any way I can use the pointers to determine how much physical memory are