calloc

Freeing a double pointer from a struct

依然范特西╮ 提交于 2019-12-25 18:26:27
问题 I have a problem with my delete_table function. So i have 2 structs struct _entry_ { int key; int data; struct _entry_* next; struct _entry_* prev; }; typedef struct _entry_ entry; struct _table_ { entry** entries; int size; }; typedef struct _table_ table; I initialise my table with calloc. void table_init(table* ht, int initial_size) { ht->entries = (entry**)calloc(initial_size, sizeof(entry*)); if (ht->entries) { ht->size = initial_size; } } Now my free function that i wrote void table

Why is reading a struct pointer field invalid?

a 夏天 提交于 2019-12-25 08:59:00
问题 Running the program in Valgrind, it says that there is an "Invalid read of size 8" at the transition pointer of the struct. It has something to do with the calloc? It is (nil) if it is read as is. Having a struct (called trie), it is used as follows: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> const int MAX_SIZE = 20; struct _trie { int maxNode; int nextNode; int** transition; char* fin; }; typedef struct _trie * Trie; Trie createTrie (int maxNode){ Trie

Failing to alloc memory received by a pointer linux

坚强是说给别人听的谎言 提交于 2019-12-25 02:53:06
问题 I have a function that receives by a pointer the location where will be stored. This place can have different other similar structs.The function has to read a file. This file have stored a struct, which i need to read. 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

Calloc inside function

让人想犯罪 __ 提交于 2019-12-24 07:47:38
问题 Looking at this question that has just been asked: Inconveniences of pointers to static variables would doing something like this be considered bad practice, then? 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] = string[i]; j++; } return strtemp; } Sorry it's written quickly, but the

gcc7.2: argument range exceeds maximum object size 9..7 [-Werror=alloc-size-larger-than=]

[亡魂溺海] 提交于 2019-12-24 01:44:11
问题 The program contains code like follows: int size; ... int *pi = (int*)calloc(size, sizeof(int)); ... Here is the error message when compiled with gcc7.2: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] When I change int *pi = (int*)calloc(size, sizeof(int)); to int *pi = (int*)calloc((unsigned int)size, sizeof(int)); The error disappeared. However, in the program, there are many malloc and

String parsing in C using strtok

半腔热情 提交于 2019-12-23 14:54:10
问题 I've got this little source code, made for testing the parsing of a string similar to variable string I need to use in other project #include <stdio.h> #include <stdlib.h> #include <string.h> 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

how can i know if calloc fails to initialize

眉间皱痕 提交于 2019-12-22 14:32:49
问题 I have read that calloc (malloc+init) will sometimes fail to init array with zero bytes (but will still return pointer to a malloc'ed array). but in documentation it does not specify that it will return NULL, is there a way to be sure that array was initialized to zero (better then going over array), if not what is the advantage of calloc over malloc ? 回答1: If calloc() returns a non-NULL pointer, the block of memory will be zero'ed. Unless you have a buggy library. In which case you should

how can i know if calloc fails to initialize

若如初见. 提交于 2019-12-22 14:29:22
问题 I have read that calloc (malloc+init) will sometimes fail to init array with zero bytes (but will still return pointer to a malloc'ed array). but in documentation it does not specify that it will return NULL, is there a way to be sure that array was initialized to zero (better then going over array), if not what is the advantage of calloc over malloc ? 回答1: If calloc() returns a non-NULL pointer, the block of memory will be zero'ed. Unless you have a buggy library. In which case you should

C博客作业05--2019-指针

烂漫一生 提交于 2019-12-19 00:34:23
0.展示PTA总分(0----2) 1.本章学习总结(2分) 1.1 学习内容总结 指针做循环变量做法:例如: for (p = a; p <= a + 9; p++)//使用指针求和 { sum = sum + *p; } 详情见课本195 例8-7 字符指针如何表示字符串:例如: char sa[] = "array"; char* sp = "point"; printf("%s", sa);//数组名sa作为printf的输出参数 printf("%s", sp);//字符指针sp作为printf的输出参数 printf("%s\n", "string");//字符串常量作为printf的输出参数 详细见课本201 8.4.2字符串和字符指针 动态内存分配: 动态分配内存的方式有两种: 1.动态存储分配函数malloc() 函数原型:void*malloc(unsigned size) 实现方式,例: //动态分配n个整数类型大小的空间 if ((p = (int*)malloc(n * sizeof(int))) == NULL) { printf("Not able to allocate memory.\n"); exit(1); } 每次动态分配都必须检查是否成功。 2.计数动态存储分配函数calloc() 函数原型:void * calloc(unsigned n

What is the difference between calloc(10,4) and calloc(1,40)?

穿精又带淫゛_ 提交于 2019-12-18 08:58:51
问题 What is the difference between calloc(10,4) and calloc(1,40)? I see this behavior: Thing** things = (Thing**)calloc(1, 10 * sizeof(Thing*)); // things[0] != 0 Thing** things = (Thing**)calloc(10, sizeof(Thing*)); // things[0] == 0 I would like to understand why. Edit: losing my mind is why, both seem to result in zero now... To at least make the question interesting, why doesn't calloc just take a single argument, like malloc? 回答1: In practice it's the same. But there's one important feature