Tag: malloc

插入二进制搜索树

我做了一个二叉搜索树 struct BTNode { int info; struct BTNode *left,*right; }; 我写了一个代码来在树中插入一个节点 void insert(struct BTNode *root,int data) { struct BTNode *ptr; struct BTNode *n=malloc(sizeof(struct BTNode)); n->info=data; n->left=NULL; n->right=NULL; if(root==NULL) root=n; else{ ptr=root; while(ptr!=NULL){ if(datainfo){ if(ptr->left==NULL) ptr->left=n; else ptr=ptr->left; } else if(data>ptr->info){ if(ptr->right==NULL) ptr->right=n; else ptr=ptr->right; } } } } 和main()函数 int main() { struct BTNode *root=NULL; […]

C中的分段错误与malloc

我在下面的场景中遇到了分段错误:当从文件中读取ip地址列表时,我将IP ADDRESS和端口存储在链接列表中。 因为我的文件读取循环重复自己,按照链接列表逻辑 – 当我再次malloc我的临时指针时,我面临分段错误。 请在下面找到代码段: struct woker_conf { int port; char *ip_address; struct worker_conf *next; } *head; void open(int8_t nbrwrk) { FILE *fp = NULL; char line[1024] = {0}; int i = 1; char *ch; struct worker_conf *config, *temp; head = NULL; fp = fopen(“abcd.txt”,”r”); if (fp == NULL) exit(1); while (fgets(line, sizeof line, […]

使用assert处理错误检查

我看了一遍,似乎有很多关于断言的混合观点。 例如,如果我是malloc的指针,并想确保它已正确分配,我会写: p = malloc(sizeof(int)); assert(p) 代替: p = malloc(sizeof(int)); if (p == NULL) { … send error message } 我知道断言它会终止程序,但出于测试目的 – 我想知道的是绝对最安全的方式: 测试像malloc正确完成的事情。 处理错误,如果某些东西不是正确的malloc 。

malloc:匿名映射和魔术区域

我只是在摆弄内存映射,并希望查看用户空间虚拟内存区域映射。 写了一些像 char *ptr = NULL; printf(“Allocating 300KB\n”); ptr = malloc (300*1024); printf(“Allocated at %p.. sleeping\n”, ptr); sleep (30); free (ptr); printf(“Freed… sleeping\n”); sleep (30); 在运行程序时,pid上的pmap将分配的区域显示为: 00007f73b1e57000 316K rw— [ anon ] 而程序o / p说: Allocated at 0x7f73b1e57010.. sleeping 对于我们称之为魔法区域的分配,这是16KB的额外分配吗? 在内核中,相应的vm_area_struct将保存程序可见范围或魔术区域起始的整个范围?

在实际指针的副本上使用free()是可接受/正确的吗?

这是: int *a = malloc (sizeof (int) ); int *b = a; free (b); 与此相同: int *a = malloc (sizeof (int) ); free (a); 如果是,不需要解释,但如果没有,请详细说明原因!

在c中正确使用malloc()和free()

我是C的新手,请原谅我,如果这太明显了,但我在查找代码中导致分段错误的错误时遇到了问题。 我相信问题可能在于malloc()的使用,但我并不积极。 这是代码: #include #include #include #define MAX_STRING 20 char* getFirstName (char* firstName ) { char* myfirstName = (char*)malloc(strlen(firstName)+1); printf(“Please enter your first name: “); fgets(firstName,MAX_STRING,stdin); return(myfirstName); } char* getLastName (char* lastName ) { char* mylastName = (char*)malloc(strlen(lastName)+1); printf(“Please enter your last name: “); fgets(lastName,MAX_STRING,stdin); return(mylastName); } char* getNickName (char* nickName ) { char* mynickName […]

连续的内存块与malloc

我试图在一个函数调用中创建一个连续的内存块,它将内存的第一部分作为指向其他块的指针数组。 基本上,我正在尝试: int **CreateInt2D(size_t rows, size_t cols) { int **p, **p1, **end; p = (int **)SafeMalloc(rows * sizeof(int *)); cols *= sizeof(int); for (end = p + rows, p1 = p; p1 < end; ++p1) *p1 = (int *)SafeMalloc(cols); return(p); } void *SafeMalloc(size_t size) { void *vp; if ((vp = malloc(size)) == NULL) { fputs("Out […]

免费的2Darrays列

我有一个2D数组的字符串,动态分配: char*** allocateArray(int line, int col) { char*** dictionary; int i=0,j=0; dictionary=(char***)malloc(sizeof(char**)*line); for(i=0;i<line;i++) { dictionary[i] = (char**)malloc(sizeof(char*)); for(j=0;j<col;j++) dictionary[i][j] = (char*)malloc(sizeof(char*)); } return dictionary; } 现在我想释放最后一栏(比如说),我该怎么办? 我使用free(dictionary[i][j]) ,但它实际上是免费的? 数组中的[i][j]单元格,或指向它的指针? 我需要释放两者。

使用malloc,struct和char *堆积腐败

我的C程序中似乎有内存损坏。 我用_ASSERTE( _CrtCheckMemory( ) ); 找到问题陈述,它在一行上说明了scep_conf->engine_str = NULL; 就在它之前。 所以,如果我理解正确,那之前的malloc就会破坏一些东西,对吗? 所以这是导致问题的代码的一部分: scep_conf = (SCEP_CONF *) malloc(sizeof(scep_conf)); scep_conf->engine = (struct scep_engine_conf_st *) malloc(sizeof(struct scep_engine_conf_st)); scep_conf->engine_str = NULL; 标题中的定义: typedef struct { struct scep_engine_conf_st *engine; char *engine_str; } SCEP_CONF; struct scep_engine_conf_st{ char *engine_id; char *new_key_location; int storelocation; char *dynamic_path; char *module_path; int engine_usage; }; SCEP_CONF *scep_conf; 基本上我不明白为什么它会破坏我的记忆。 […]

C调整动态数组的大小

我正在为C类创建动态数组数据结构。 我已经完成了大部分工作,现在我正在测试它。 它目前在resize时卡住了。 我用过printf来解决这个问题。 看起来resize正在工作,但是当我去添加我的下一个项目后resize时,它就停在那里。 我认为这可能与我的内存分配或我指向的方式有关,并在resize期间释放数组。 我是C的新手,所以这些都是我的观点。 #include #include #include “dynArray.h” #include struct DynArr { TYPE *data; /* pointer to the data array */ int size; /* Number of elements in the array */ int capacity; /* capacity of the array */ }; /* ************************************************************************ Dynamic Array Functions ************************************************************************ */ /* Initialize (including allocation of […]