malloc

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

Why is the return value of malloc(0) implementation-defined?

China☆狼群 提交于 2020-06-10 07:33:07
问题 ISO/IEC 9899:TC2 (i.e. the C99 standard), §7.20.3 states: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. In other words, malloc(0) may either return NULL or a valid pointer which I may not dereference. What is the rationale behind this behavior? And wouldn't it be easier to just define that

Understanding “corrupted size vs. prev_size” glibc error

 ̄綄美尐妖づ 提交于 2020-06-07 04:59:30
问题 I have implemented a JNA bridge to FDK-AAC. Source code can be found in here When bench-marking my code, I can get hundreds of successful runs on the same input, and then occasionally a C-level crash that'll kill the entire process, causing a core-dump to be generated: Looking at the core dump, it looks like this: #1 0x00007f3e92e00f5d in __GI_abort () at abort.c:90 #2 0x00007f3e92e4928d in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7f3e92f70528 "*** Error in `%s': %s: 0x%s

C: Ordersystem for Fastfood Project

只谈情不闲聊 提交于 2020-05-17 06:57:05
问题 i have to do a order system for a fastfood restaurant (its just a school project). I have finished the tool already. But the teacher told us to use getch instead of scanf. But i dont understand, how to implent it.. Can i just replace my scanf in the code with getch? I dont need the Echo from Scanf and dont need confirmation with the ENTER-Key. The task was: Create a CMD-Tool in the Programming Language C. NO GUI! The Solution must include the commands „malloc“, „realloc“, „getch“, and free.

What is wrong with how I'm dynamically allocating space for this 2d array?

∥☆過路亽.° 提交于 2020-05-17 05:44:11
问题 I'm trying to create a 2D array that will store be able to store each character of a .txt file as an element in the 2D array. How do I dynamically allocate space for it? This what I've done so far to malloc it. (this was copied of GeeksForGeeks) char *arr[rownum2]; for (i = 0; i < rownum2; i++) { arr[i] = (char *)malloc(colnum * sizeof(char)); However, I think this is the source of serious memory related issues later on in my program, and I've also been told some parts of this are unnecessary

What is wrong with how I'm dynamically allocating space for this 2d array?

…衆ロ難τιáo~ 提交于 2020-05-17 05:43:26
问题 I'm trying to create a 2D array that will store be able to store each character of a .txt file as an element in the 2D array. How do I dynamically allocate space for it? This what I've done so far to malloc it. (this was copied of GeeksForGeeks) char *arr[rownum2]; for (i = 0; i < rownum2; i++) { arr[i] = (char *)malloc(colnum * sizeof(char)); However, I think this is the source of serious memory related issues later on in my program, and I've also been told some parts of this are unnecessary

Is there really no version of realloc() supporting alignment?

南笙酒味 提交于 2020-05-10 07:08:06
问题 There exist several aligned versions of the venerable malloc() , e.g.: #include <stdlib.h> int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include <malloc.h> void *memalign(size_t alignment, size_t size); (originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc() which supports alignment. Has it really never been implemented? It seems pretty trivial to

Is there really no version of realloc() supporting alignment?

*爱你&永不变心* 提交于 2020-05-10 07:07:26
问题 There exist several aligned versions of the venerable malloc() , e.g.: #include <stdlib.h> int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include <malloc.h> void *memalign(size_t alignment, size_t size); (originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc() which supports alignment. Has it really never been implemented? It seems pretty trivial to

Is there really no version of realloc() supporting alignment?

心不动则不痛 提交于 2020-05-10 07:06:26
问题 There exist several aligned versions of the venerable malloc() , e.g.: #include <stdlib.h> int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include <malloc.h> void *memalign(size_t alignment, size_t size); (originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc() which supports alignment. Has it really never been implemented? It seems pretty trivial to

Both dynamic column and row sized 2D array updated in recursive function in c programming

ぃ、小莉子 提交于 2020-04-16 08:34:29
问题 I am growing a 2D array by it's column and row size in c programming. #include <stdio.h> #include <malloc.h> //#include <conio.h> //#include <stdlib.h> void func(int** p, int d, int** sizes) { static int count = 0; int* item = NULL; int* temp = NULL; if (d == 5) return; *sizes = (int*) realloc(*sizes, (d + 1) * sizeof(*sizes)); *sizes[count] = d + 1; // <=== in second recursive call it throws memory allocation errors here in runtime //p = (int**) realloc(p, (count + 1) * sizeof(int*)); /