dynamic-memory-allocation

Copying Struct to a Pointer array in a function C

☆樱花仙子☆ 提交于 2019-12-11 11:08:53
问题 i have a huge problem allocating memory in C i have this struct typedef struct{ int x; int y; }T; i want to create a function that dynamically adds a structs to a pointer. something like: int main() { T* t; f(&t); free(t); } up to this point i think everything is ok, now the function is where i get lost void f(T** t) { T t1; T t2; T t3; //first i malloc *t=malloc(sizeof(T)*T_MAX_SIZE);//i want another function to make the array bigger, but this is not as important as the problem t1.x=11; t1.y

Dynamically Allocating a Matrix from Input - C

大憨熊 提交于 2019-12-11 08:49:00
问题 I've been trying this code and it's not working out very well. void *matrix_allocate_variable (int size) { void *p1; if ((p1=(int*)malloc(size))==NULL){ printf("out of memory.\n"); exit(1); } return p1; } Here I created a function that call's malloc and exits upon error, so that I could use it in my next function: void matrix_new(int **matrices, int *row_counts, int *column_counts, char specifier) { int index, i; index= (int)(specifier-'A'); scanf("%d",&row_counts[index]); scanf("%d",&column

Dynamic array with Frama-C and Eva

情到浓时终转凉″ 提交于 2019-12-11 06:59:07
问题 In https://stackoverflow.com/a/57116260/946226 I learned how to verify that a function foo that operates on a buffer (given by a begin and end pointer) really only reads form it, but creating a representative main function that calls it: #include <stddef.h> #define N 100 char test[N]; extern char *foo(char *, char *); int main() { char* beg, *end; beg = &test[0]; end = &test[0] + N; foo(beg, end); } but this does not catch bugs that only appear when the buffer is very short. I tried the

How to detect where a block of memory was allocated?

佐手、 提交于 2019-12-11 06:45:47
问题 A block of memory can be allocated statically, in the stack or in the heap. I want to know a way to detect if a pointer points to the heap. I work with Windows and Linux and it is not a problem a different solution for each OS. I use GCC and Mingw. If I could know where the heap begins and where it ends, I think the problem can be solved. I think that I can detect the bottom and the top of the stack in order to know if the block is in the stack, but if there are multiple threads, then there

array of pointers and allocate memory for the strings dynamically

你离开我真会死。 提交于 2019-12-11 04:53:54
问题 This question is connected to this question. I am defining an array of characters, each of 150b, and copy a string to it as: const gchar *strAuth; gchar *strings[18][150]; strcpy(strings[0],strAuth); which is huge memory wastage for most of the cases, and may be insufficient for some extreme cases. As suggested in question referenced, it is a better idea to "make an array of pointers and allocate memory for the strings dynamically." How I can achieve this? Kindly help. 回答1: You want to use

Dynamic memory allocation in c without malloc

时光毁灭记忆、已成空白 提交于 2019-12-11 03:56:40
问题 Here's a C program one of my friends had written. From what I know, arrays had to be initialised at compile time before C99 introduced VLA's, or using malloc during runtime. But here the program accepts value of a const from the user and initialises the array accordingly. It's working fine, even with gcc -std=c89 , but looks very wrong to me. Is it all compiler dependent? #include <stdio.h> int main() { int const n; scanf("%d", &n); printf("n is %d\n", n); int arr[n]; int i; for(i = 0; i < n;

C/C++ : Deallocating or deleting a block of dynamically created memory [duplicate]

主宰稳场 提交于 2019-12-11 03:48:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++ delete - It deletes my objects but I can still access the data? I am trying to deallocate a block of dynamically created memory in C/C++. But both the standard methods I used (malloc/free and new/delete) seems to be dysfunctional. The o/p of both the codes given below is similar. Here is code using malloc/free : #include<stdio.h> #include<stdlib.h> int main(){ int * arr; arr = (int *)malloc(10*sizeof(int));

Use strlen with scanf(%ms)

泄露秘密 提交于 2019-12-11 01:42:36
问题 Is it possible to use strlen() over a dynamically allocated string? FOR EXAMPLE : #include <stdio.h> #include <string.h> int main () { char *input=NULL; printf ("Enter a sentence: "); scanf("%ms", &input); //Is this legit? printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(input)); return 0; } 回答1: You can use strlen() on any sequence of char s ended by a '\0' , the null-character aka NUL *1 , which in fact equals 0 . It does not matter how the memory has been allocated.

realloc/HeapRealloc fails eventhough enough memory is available

好久不见. 提交于 2019-12-11 01:11:36
问题 My machine has 8GB of RAM and is running Windows Server 2008. malloc/realloc() fails to allocate more memory when my application has 1.5~1.7GB already allocated. I tried switching to HeapAlloc/HeapRealloc instead and the same situation happens. Is there something I am missing here? What could be causing my application to be unable to allocate more memory when there is clearly available RAM? 回答1: What could be causing my application to be unable to allocate more memory when there is clearly

What algorithm to apply for continuious reallocation of small memory chunks?

走远了吗. 提交于 2019-12-10 23:50:02
问题 In C program I face transactions that require to have alot of memory chunks, I need to know if there is an algorithm or best practice teqnique used to handle all these malloc/free, I've used arrays to store these memory chunks but at some point the array itself gets full and reallocating the array is just more waste, what is the elegant way to handle this issue? 回答1: The best algorithm in this case would be free list allocator + binary search tree. You are asking one big memory chunk from