calloc

How to use calloc() in C?

余生颓废 提交于 2021-02-16 19:02:50
问题 Shouldn't I get an error if my string goes over 9 characters long in this program? // CString.c // 2.22.11 #include <stdio.h> #include <stdlib.h> #include <string.h> main() { char *aString = calloc(10, sizeof(char)); if (aString == NULL) { return 1; } printf("PLEASE ENTER A WORD: "); scanf("%s", aString); printf("YOU TYPED IN: %s\n", aString); //printf("STRING LENGTH: %i\n", strlen(aString)); } Thanks blargman 回答1: You don't get a compiler error because the syntax is correct. What is

Make calloc opportunistic

微笑、不失礼 提交于 2021-02-10 14:26:28
问题 On linux malloc behaves opportunistically, only backing virtual memory by real memory when it is first accessed. Would it be possible to modify calloc so that it also behaves this way (allocating and zeroing pages when they are first accessed)? 回答1: It is not a feature of malloc() that makes it "opportunistic". It's a feature of the kernel with which malloc() has nothing to do whatsoever. malloc() asks the kernel for a slap of memory everytime it needs more memory to fulfill a request, and it

Make calloc opportunistic

我只是一个虾纸丫 提交于 2021-02-10 14:25:42
问题 On linux malloc behaves opportunistically, only backing virtual memory by real memory when it is first accessed. Would it be possible to modify calloc so that it also behaves this way (allocating and zeroing pages when they are first accessed)? 回答1: It is not a feature of malloc() that makes it "opportunistic". It's a feature of the kernel with which malloc() has nothing to do whatsoever. malloc() asks the kernel for a slap of memory everytime it needs more memory to fulfill a request, and it

free() function without malloc or calloc

送分小仙女□ 提交于 2021-02-07 17:24:20
问题 quick question Can you use the free() function without having to prior call a malloc ?? ei. void someFunc( void ) { char str[6] = {"Hello"}; //some processing here .... free(str); } I get no compiling errors but Does this work or is it correct at all ? Thank you, 回答1: This is not at all correct: You cannot free a static array such as char str[6] . free() should only be called on memory you allocated (or on NULL). 回答2: When you call malloc() or any other allocation function, memory will be

Does calloc() of a double field always evaluate to 0.0?

寵の児 提交于 2021-02-07 14:12:39
问题 Does calloc() of a double field always evaluate to 0.0 ? Furthermore : Does calloc() of a float field always evaluate to 0.0f ? Does calloc() of an int or unsigned int field always evaluate to 0 ? That is , will the assert() below always succeed on all platforms? double* d = calloc(1, sizeof(double)); assert(*d == 0.0); free(d); 回答1: The calloc sets all bytes of the allocated memory to zero. As it happens, that's also the valid IEEE754 (which is the most common format for floating point

Calloc a Two-Dimensional Array

好久不见. 提交于 2020-08-01 09:19:38
问题 To make a two dimensional array, I'm currently using the following: int * own; own = (int *)calloc(mem_size, sizeof(int)); for (i=0;i<mem_size;i++){ own[i] = (int *)calloc(3, sizeof(int)); } However, every time I reference own[i][j], I get an error saying that the subscripted value is neither array nor pointer nor vector. 回答1: Use: int ** own; // int**, not int* own = calloc(mem_size, sizeof(int*)); //int*, not int // And remove the explicit cast. 回答2: However, every time I reference own[i][j

Calloc a Two-Dimensional Array

北城以北 提交于 2020-08-01 09:18:09
问题 To make a two dimensional array, I'm currently using the following: int * own; own = (int *)calloc(mem_size, sizeof(int)); for (i=0;i<mem_size;i++){ own[i] = (int *)calloc(3, sizeof(int)); } However, every time I reference own[i][j], I get an error saying that the subscripted value is neither array nor pointer nor vector. 回答1: Use: int ** own; // int**, not int* own = calloc(mem_size, sizeof(int*)); //int*, not int // And remove the explicit cast. 回答2: However, every time I reference own[i][j

How to save the scanf input only if there's enough space in the array? How to reallocate array to let the scanf input fits in?

自作多情 提交于 2020-06-17 06:30:39
问题 #include <stdio.h> int main() { char *mystring = calloc(2, sizeof(char)); scanf("%10[^\n]s", mystring); printf("\nValue: %s\nSize of array: %d\nAllocated space: %d\n", mystring, 2 * sizeof(char), sizeof(char) * strlen(mystring)); free(mystring); } Output: $ ./"dyn_mem" laaaaaaaaaaa Value: laaaaaaaaa Size of array: 2 Allocated space: 10 This code can produce an undefined behavior if I enter in the scanf input a string bigger than array size. How can I handle this ? 回答1: There are multiple

How to save the scanf input only if there's enough space in the array? How to reallocate array to let the scanf input fits in?

时间秒杀一切 提交于 2020-06-17 06:30:27
问题 #include <stdio.h> int main() { char *mystring = calloc(2, sizeof(char)); scanf("%10[^\n]s", mystring); printf("\nValue: %s\nSize of array: %d\nAllocated space: %d\n", mystring, 2 * sizeof(char), sizeof(char) * strlen(mystring)); free(mystring); } Output: $ ./"dyn_mem" laaaaaaaaaaa Value: laaaaaaaaa Size of array: 2 Allocated space: 10 This code can produce an undefined behavior if I enter in the scanf input a string bigger than array size. How can I handle this ? 回答1: There are multiple

How to realloc some memory allocated using calloc?

百般思念 提交于 2020-05-16 10:41:11
问题 I've allocated a string with the calloc function: //string1 and string2 previously declared char *stringClone = calloc(strlen(string1) + 1, sizeof(char)); Now I want to do the same thing on stringClone with a different string. Doing: stringClone = calloc(strlen(string2) + 1, sizeof(char)); I'm gonna have some memory leak, right? How should I use the realloc in this case? 回答1: You can use realloc() to reallocate memory allocated by malloc() , calloc() , realloc() , aligned_alloc() or strdup()