malloc

ANSI C Dynamic Memory Allocation and when exactly we should free the memory

大憨熊 提交于 2020-01-06 15:40:51
问题 I am trying to get my head around memory allocations and freeing them in ANSI C. The problem is I don't know when to free them. 1) Does program exit free the allocated memory itself (even if I didn't do it by free() )? 2) Let's say my code is something like this: (please don't worry about the full code of those structs at the moment. I am after the logic only) snode = (stock_node *) realloc(snode, count * sizeof(stock_node)); struct stock_list slist = { snode, count }; stock_list_ptr slist

Safely reading in strings of unknown length

牧云@^-^@ 提交于 2020-01-06 14:58:09
问题 I am trying to safely get in strings of unknown length with using fgets This is the code I have been able to come up with so far, but now I am stuck on how to conitinue forward. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define DEFLEN 4 #define CHUNKSZ 2 int i = 0; int size =0; int size2 =0; char* ln1; char* ln2; FILE *fpin; char *getStrFromFile( FILE *fpins )/*file stream to read string from */ { DEFLEN = malloc( CHUNKSZ *sizeof(int)); while(1){ if( fgets

Reading part of a file in C using fread() and fseek()

大兔子大兔子 提交于 2020-01-06 08:12:46
问题 I'm trying to read a file into a buffer in blocks of size BLOCK_SIZE (currently equal to 1000 unsigned chars ). My code initially finds the number of blocks it will have to read in order to read the entire file (usually 2-4), then iterates through a for loop reading the file (ignore the " +17+filenamesize " stuff, that is all needed for later in the program. However, only on the first time, when j=1 , does it actually put data into the buf array. In other cases, when j != 1 , strlen(buf)

Reading part of a file in C using fread() and fseek()

不羁岁月 提交于 2020-01-06 08:12:25
问题 I'm trying to read a file into a buffer in blocks of size BLOCK_SIZE (currently equal to 1000 unsigned chars ). My code initially finds the number of blocks it will have to read in order to read the entire file (usually 2-4), then iterates through a for loop reading the file (ignore the " +17+filenamesize " stuff, that is all needed for later in the program. However, only on the first time, when j=1 , does it actually put data into the buf array. In other cases, when j != 1 , strlen(buf)

Can't free a Malloc'd string

怎甘沉沦 提交于 2020-01-06 04:11:07
问题 This one should be quick, I think. EDIT: This is for my CS113 class. I just needed to free all the memory. If Valgrind found any memory leaks, I'd lose points. :P Regardless, I figured out that it apparently just required me to free stuff in Main that related to the return value of zero_pad. Once I did so, it worked fine. I'd mark this post as "complete" if I knew how. char *zero_pad(struct cpu_t *cpu, char *string) { char *zero_string = malloc(cpu->word_size + 1); int num_zeros = ((cpu->word

How come this C program does not crash?

a 夏天 提交于 2020-01-06 03:42:05
问题 This is from the book: Understanding and Using C Pointers If memory is re‐ peatedly allocated and then lost, then the program may terminate when more memory is needed but malloc cannot allocate it because it ran out of memory. In extreme cases, the operating system may crash. This is illustrated in the following simple example: char *chunk; while (1) { chunk = (char*) malloc(1000000); printf("Allocating\n"); } The variable chunk is assigned memory from the heap. However, this memory is not

How come this C program does not crash?

本秂侑毒 提交于 2020-01-06 03:42:05
问题 This is from the book: Understanding and Using C Pointers If memory is re‐ peatedly allocated and then lost, then the program may terminate when more memory is needed but malloc cannot allocate it because it ran out of memory. In extreme cases, the operating system may crash. This is illustrated in the following simple example: char *chunk; while (1) { chunk = (char*) malloc(1000000); printf("Allocating\n"); } The variable chunk is assigned memory from the heap. However, this memory is not

I'm having trouble with allocating memory with strings

淺唱寂寞╮ 提交于 2020-01-05 21:04:12
问题 I am having trouble with the allocating memory part of my program. I am supposed to read in a file that contains a list of names then allocate memory for them and store them in the allocate memory. This is what I have so far, but I keep getting a segmentation fault when I run it. #include <string.h> #include <stdio.h> #include <stdlib.h> #define MAX_STRING_LEN 25 void allocate(char ***strings, int size); int main(int argc, char* argv[]){ char **pointer; int size = atoi(argv[1]); allocate(

Getting no segmentation fault when exceeding the size that was allocated for a char * [duplicate]

被刻印的时光 ゝ 提交于 2020-01-05 04:36:15
问题 This question already has answers here : Array index out of bound in C (10 answers) Closed 4 years ago . I use malloc to allocate n (string length of y) bytes for x. However after copying y to x , I added 3 more characters including '\0' in x and i got no error. Shouldn't I get an error for trying to assign values to unallocated memory since I've allocated space enough for only 10 characters? Is this undefined behavior? #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int

Why does calling malloc() not make a difference?

早过忘川 提交于 2020-01-05 04:02:21
问题 Here's a basic example: #include <all the basic stuff> int main(void) { char *name = (char *) malloc(2 * sizeof(char)); if(name == NULL) { fprintf(stderr, "Error: Unable to allocate enough memory!\n"); return EXIT_FAILURE; } strcpy(name, "Bob Smith"); printf("Name: %s\n", name); free(name); return EXIT_SUCCESS; } Because I'm only allocating 2 bytes of information (2 chars), there should be some sort of error when I perform strcpy, right? This doesn't happen, instead it just copies the string