malloc

malloc in a function doesn't work well

我的未来我决定 提交于 2021-02-05 06:43:27
问题 I can't understand why the whole thing doesn't work. I just want to do malloc in the function func , when I return from it, the malloc disappears... and I get * glibc detected ./test: free(): invalid pointer: 0xb76ffff4 ** #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h> int func(char *p) { p=(char*)malloc(1); *p='a'; printf("1. p= %c\n",*p); return 0; } int main() { char *p; func

Getting array size in C. Cannot understand output

时光怂恿深爱的人放手 提交于 2021-02-05 06:42:05
问题 I am curious why I am getting the following behaviour in my code. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int M=24; int arr[M]; int N=24; int* ptr=(int*) malloc(sizeof(int)*N); /*Allocate memory of size N */ printf("Size of your malloced array is %lu\n",sizeof(ptr)/sizeof(ptr[0])); /* Get the size of memory alloctaed. Should be the same as N?*/ printf ("Size of your normal arrays is %lu\n",sizeof(arr)/sizeof(arr[0])); /* Ditto */ free(ptr); return 0; } The

removing trailing and leading spaces from a file

雨燕双飞 提交于 2021-01-29 08:48:12
问题 I am trying to read lines from a text file of unknown length. In the line there can be leading and trailing white-spaces until the string occurs. So my first step is to read line by line and allocate memory for the strings. Then remove all the leading and trailing white spaces. After that I want to check if the string has any white space characters in it which is an invalid character. For example the string can not look like this "bad string" but can look like this "goodstring" . However when

segment fault, assigning to double pointer in c

邮差的信 提交于 2021-01-29 04:10:04
问题 I am getting a little confused as to how to properly allocate memory for double pointers. My code causes a segment fault the second time it attempts to store a value at the 2nd index of the first UCHAR pointer array. Any assistance would be appreciated. assigning my double pointer: width = BMP_GetWidth (bmp); height = BMP_GetHeight (bmp); depth = BMP_GetDepth (bmp); r = (UCHAR **) malloc(sizeof(UCHAR *) * height); g = (UCHAR **) malloc(sizeof(UCHAR *) * height); b = (UCHAR **) malloc(sizeof

malloc, recasting and free

人盡茶涼 提交于 2021-01-28 20:24:50
问题 I have some structures to store different kinds of lists: typedef int db_int; typedef char db_string[DB_STRING_LEN]; struct List_db_int { struct List_db_int *next; struct List_db_int *prev; db_int v; }; struct List_db_string { struct List_db_string *next; struct List_db_string *prev; db_string v; }; struct List_db_void { struct List_db_void *next; struct List_db_void *prev; }; I also have an union which can store any of this list pointers: union Uni_list { struct List_db_int *db_type_int;

Valgrind: Uninitialised value was created by a heap allocation

本秂侑毒 提交于 2021-01-28 20:22:18
问题 I'm getting a few Valgrind errors concerning Unitialised values and Conditional Jumps. Here is my Valgrind output ==28124== Conditional jump or move depends on uninitialised value(s) ==28124== at 0x4C2E0E9: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==28124== by 0x400AA7: append_character (in /home/i) ==28124== by 0x401319: refresh_address (in /home/) ==28124== by 0x402067: main (in /home/) ==28124== Uninitialised value was created by a heap allocation ==28124== at

realloc vs malloc in C

不羁的心 提交于 2021-01-28 14:23:06
问题 I'm fairly new to C, and just now starting to venture into the realm of dynamically allocated arrays. I think i've got mostly malloc down, but had some questions on realloc : Can realloc be used for anything else besides adding memory space to pointers? Does the size variable always have to be an int ? Would something like the below work? float *L = NULL; int task_count = 5; L = (float*) realloc (L, task_count * sizeof(float)); If I wanted to increase that space further (by one in this case),

malloc in do while block

一世执手 提交于 2021-01-28 06:10:24
问题 Can I put malloc in do...while block to ensure successful allocation? like: #define STR_SIZE 1024 * 1024 char *str; do str = (char *) malloc(STR_SIZE); while ( !str ); 回答1: It probably won't work, because malloc rarely fails, it fails when resources are exhausted, and these won't be available soon. the only common case when malloc could get more resources is if some other big process releases a lot of memory, and that seldom happens and may never happens; if you cared about that add at least

Printf function who returning an array of int

心不动则不痛 提交于 2021-01-28 05:34:09
问题 I'm trying this exercice but I don't know how to printf my function in main. Exercice: 1) Write a function who returning an int tab with all values between min and max #include <stdlib.h> #include <stdio.h> int *ft_range(int min, int max) { int len; int *tab; len = min; while (len < max) len++; tab = (int *)malloc(sizeof(*tab) * len + 1); while (min < max) { *tab = min; min++; } return(tab); } int main() { ft_range(0, 10); return(0); } 回答1: returning an int tab with all values between min and

How can I read a known number of strings of unknown size from a .txt file and store each line in a line of a matrix (in C)?

有些话、适合烂在心里 提交于 2021-01-28 01:02:22
问题 Title is pretty self explanatory. I'm almost sure that the end result wouldn't be a matrix as each line would have a different number of columns, so it's more like a array of arrays of variable sizes. It would also be interesting to sort the fragments by size, biggest first. This is what I've tried so far: int main() { char str[MAXLEN], **fragmentsList; int number_of_strings, i, max, k; printf("Enter .txt file name: "); scanf("%s", str); printf("How many strings does the file has? "); scanf("