Tag: calloc

使用未分配的空间calloc

我只是想知道当我使用非分配空间时编译器不会抛出exception,这里是一个代码例如: #include #include int main() { int i, n; int *a; printf(“Number of elements to be entered:”); scanf(“%d”,&n); a = (int*)calloc(n, sizeof(int)); printf(“Enter %d numbers:\n”,n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("The numbers entered are: "); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } […]

无法分配指针linux接收的内存

我有一个函数,通过指针接收将存储的位置。 这个地方可以有不同的其他类似结构。该函数必须读取一个文件。 这个文件存储了一个我需要阅读的结构。 typedef struct user_manage_t{ short int user_id; char permission; long int other_id; long int check; }user_manage_t; typedef struct holder_t{ user_manage_t *user_manage; user_manage_t *user_manage_backup; //(…)and a lot of stuff }holder_t; holder_t holder; int db_read_from_file(user_manage_t *prt){ DEBUG_PRINT(“READ_FROM file started”); FILE *fd_read; char buffer[480]; int read, bytesRead=0; int num; const struct user_manage_t *header; fd_read = fopen(“/home/user/user_list”,”r+b”); […]

为什么读取结构指针字段无效?

在Valgrind中运行该程序,它表示在结构的转换指针处存在“无效读取大小为8”。 它与calloc有关吗? 如果按原样读取它是(零)。 有一个结构(称为trie),它使用如下: #include #include #include #include const int MAX_SIZE = 20; struct _trie { int maxNode; int nextNode; int** transition; char* fin; }; typedef struct _trie * Trie; Trie createTrie (int maxNode){ Trie trie; trie = (Trie) malloc(sizeof(Trie)); printf(“size of trie: %lu, size of the struct: %lu, size of _trie: %lu\n”,sizeof(trie),sizeof(Trie), sizeof(struct _trie)); […]

Calloc里面的function

看看刚刚被问过的这个问题: 对静态变量指针的不便会做这样的事情被认为是不好的做法,那么呢? char* strpart(char* string, int start, int count) { char* strtemp; int i = 0; int j = 0; int strL = strlen(string); if ( count == 0 ) { count = strL; } strtemp = (char*) calloc((count + 1), sizeof(char)); for ( i = start; i < (start+count); i++ ) { strtemp[j] = […]

通过写入2D数组来分割错误

我的程序中有一个小的内存访问问题,我没有找到错误,也许有人可以帮助我。 我创建了一个新类型来存储rgb颜色值。 那种类型看起来像: typedef struct pixel { unsigned char r; unsigned char g; unsigned char b; } pixel; 在我的主程序中,我用calloc创建了一个2D动态数组,用于存储红色信息。 pixel **pixelvalue = (pixel **) calloc(imginformation.width, sizeof(pixel)); for (i = 0; i < imginformation.width; i++) { pixelvalue[i] = (pixel *) calloc(imginformation.height, sizeof(pixel)); } 之后我调用我的函数,它读取颜色值,谁应该将它们安全地保存到数组中。 该函数作为参数获取数组。 ReadFile(file, imginformation (Stuff like height and so one), pixelvalue (The calloc […]

使用strtok在C中解析字符串

我有这个小源代码,用于测试解析类似于我需要在其他项目中使用的变量string #include #include #include int main (void) { char string[] = “C-AC-2C-3C-BOB”; char* s; char* hand[3]; char* usr; s = (char*) calloc(1, sizeof(char)); hand[1] = (char*) calloc(3, sizeof(char)); hand[2] = (char*) calloc(3, sizeof(char)); hand[3] = (char*) calloc(3, sizeof(char)); usr = (char*) calloc(21, sizeof(char)); s = strtok (string,”-“); hand[1] = strtok (NULL, “-“); hand[2] = strtok […]

我怎么知道calloc是否无法初始化

我已经读过calloc(malloc + init)有时会无法使用零字节初始化数组(但仍会返回指向malloc数组的指针)。 但是在文档中它没有指定它将返回NULL,是否有一种方法可以确保数组初始化为零(然后更好地遍历数组),如果不是calloc优于malloc的优势是什么?

calloc v / s malloc和时间效率

我饶有兴趣地阅读了malloc和calloc之间的C后差异 。 我在我的代码中使用了malloc,想知道我将使用calloc有什么区别。 我目前的(伪)代码与malloc: 情景1 int main() { allocate large arrays with malloc INITIALIZE ALL ARRAY ELEMENTS TO ZERO for loop //say 1000 times do something and write results to arrays end for loop FREE ARRAYS with free command } //end main 如果我使用calloc而不是malloc,那么我将: Scenario2 int main() { for loop //say 1000 times ALLOCATION OF ARRAYS […]

calloc的两个参数

为什么calloc采用两个参数而不是像malloc一样? 具体来说,由于以下表达式之间没有区别(或存在?): calloc (a, b); calloc (b, a); calloc (a * b, 1); calloc (1, a * b); 为什么不接受要分配的总字节数? 这个界面背后的理由是什么? 为什么这不适用于malloc?

Calloc用于C中具有负索引的数组数组

我有一个带负索引的数组数组。 它是一个具有实际尺寸[dim_y + 40] [dim_x + 40]的数组,但是用户使用的数据类似于维度[dim_y] [dim_x]。 首先,我有全局,已经定义了维度dim_x,dim_y,所以我有这个 int map_boundaries[dim_y + 40][dim_x + 40]; int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20]; 它工作正常。 现在我需要维度dim_y和dim_x是可变的,这意味着我希望数组’map’没有固定大小但是动态,我需要从用户读取dim_y,dim_x和数组’map’是全球性的,所以我有 int **map_boundaries; 我在main()使用calloc map_boundaries = (int **)calloc(dim_y + 40,sizeof(int*)); for(i = 0; i < dim_y + 40; i++){ map_boundaries[i] = (int *)calloc(dim_x + 40,sizeof(int)); } ,但我不知道如何申报第二行 为了更好地理解边界,我在第2条评论中发布了这些内容: http : //everything2.com/title/Negative+array+indexing