Questions tagged [malloc]

The tag has no usage guidance, but it has a tag wiki.

Filter by
Sorted by
Tagged with
3 votes
1 answer
18k views

How to free a nested struct?

In preparation for my final project, I am writing a C program that handles task lists. It utilizes a nested struct. For every line it reads from a file it creates a struct of type task. Each task then ...
2 votes
1 answer
2k views

Pointers and arrays, don't understand the relationship

I made two structs, they are very similar. The first one is a node and the second one, called hash, only contains a pointer to a node. In main I've made pointers to allocated memory for them. newNode ...
  • 59
2 votes
1 answer
85 views

pset2/crack/(Memory issue)

If I run this code for the first time it gives me the desired output. But on the second run, I don't get the desired output. It seems like memory is not given free. #include <stdio.h> #include &...
1 vote
1 answer
281 views

segfault while freeing memory in speller

I'm having an enormous problem implementing the unload function for the speller in pset6. Although it's not a well-polished algorithm I first wanted to make a quick design that allows me to take a ...
  • 23
1 vote
1 answer
731 views

Recursion Destroy Singly linked List, memory leak?

I'm working through some personal test code to make sure I understand how to work withs singly linked lists. I'm basically going through the Week 5 Section video on singly linked lists and putting ...
  • 45
1 vote
2 answers
966 views

declaring char array vs malloc

I was watching the week 5 lecture and David said that a chunk of memory can be allocated two ways: using malloc(); declaring an array of a certain size (char buffer[10]). Is there really any ...
  • 17
1 vote
1 answer
478 views

malloc an array - hash table - Pset 6

Initially i had no problems implementing my hash table during the load function. however when it came time to check for errors, i couldn't access my hash table in order to traverse my linked list. I ...
  • 11
1 vote
1 answer
43 views

spellchecker unload not freeing 1 block

What is not freeing!? [code snipped by mod] // end of load function (mod comment) return true; fclose(dict_file); } [code snipped by mod]
0 votes
1 answer
8k views

Invalid pointer error when using free()

I was trying to practice malloc'ing and freeing memory, however when I run the free() function in this program an error is returned for an invalid pointer. Can anyone tell me what I'm doing wrong? ...
  • 17
0 votes
1 answer
214 views

problem with recover.c using malloc and fread

i tried to write a function that sample 4 bytes from card.raw file but i doesn't work because fread doesn't put any data into block buffer so i wrote this simple code and i found out that fgetc works ...
0 votes
1 answer
265 views

pset4 recover with malloc having a segmentation fault

I can't seem to find the reason of why I'm getting the segmentation fault in my code. The moment my code starts to write the bytes of buffer, I encounter the fault Any help is really appreciated. #...
0 votes
2 answers
326 views

Pset5 - Problem with hash table load(), malloc, and segmentation-fault

I am having problem with the load function. When I run: valgrind --leak-check=full ./speller texts/austinpowers.txt it gives me the following report (I am using the small dictionary to test my code) ...
0 votes
3 answers
148 views

Week 4, Section : Dynamic Memory Allocation

After watching Dynamic Memory allocation section and reading this i am a bit confused, if declaring an array int array[10]; and allocating using malloc int* parray = malloc(10 * sizeof(int));...
0 votes
2 answers
246 views

Segmentation fault in recover.c (pset4)

On checking with valgrind, I get main (recover.c:89) ==14182== Address 0x55022c8 is 0 bytes after a block of size 8 alloc'd There are 2 more problems with malloc again, while doing sprintf and ...
0 votes
1 answer
147 views

Want to make sure I understand struct, malloc, and pointers and how they relate to each other

Sort of related to PSET4 resize, but actually want to make sure I understand this in general. So at first I just declared a struct BITMAPINFOHEADER called "new_bi" and dealt with it while avoiding ...
0 votes
1 answer
470 views

fread not putting what I expect in buffer

for Pset4, recover, I'm trying to read 4 bytes from card.raw into a buffer. I''m testing the first 4 bytes of card.raw with what my buffer contains using gdb, and I'm not getting the same thing. I ...
  • 22
0 votes
2 answers
122 views

Pointers and asterisks

I searched and found Luke's very detailed reply to a similar question, however, my question (and confusion) still remain. I don't understand why we can malloc a pointer to a struct, and this also ...
  • 1,317
0 votes
1 answer
18 views

Week 4 lecture: calling free() after malloc() removes destination contents

In the lecture of week 4 in CS50x, David shows how to allocate memory using malloc() to clone a string, and how free() needs to be called whenever malloc() was called to free up allocated memory ...
0 votes
1 answer
153 views

Why do you not need to allocate memory for int16_t in Lab4, volume?

Why, when setting up the buffer below, do you not need to use malloc to set aside memory for it? My code works fine, I just want to understand this a little better. Thanks, in advance, for the help. ...
  • 14
0 votes
1 answer
54 views

PSET5: memory leak in check function

bool check(const char *word) { unsigned int hash_index = hash(word); node *trav = table[hash_index]; while (trav != NULL) { int len = strlen(trav->word); char ...
0 votes
1 answer
432 views

Speller Valgrind leaks bytes + error: munmap_chunk(): invalid pointer error in C

Check50 gives me the munmap_chunk: invalid pointer - it seems I am not copying something right with strcpy. Valgrind gives me: LEAK SUMMARY: still reachable: 608 bytes in 2 blocks ==2864== 56 bytes ...
0 votes
1 answer
33 views

pset4 speller trie segmentation fault

I am doing the trie version of speller in pset4 and keep getting a segmentation fault. I have run my code on short dictionaries and short text files and it seems to work just fine but when I try ...
  • 3
0 votes
1 answer
136 views

PSET4 (Speller) malloc and calloc usage

After getting my code to work, i looked if there were any memory leaks using valgrind. It found no memory leaks, however it returned these two errors: ==7109== Conditional jump or move depends on ...
0 votes
1 answer
113 views

PSET4 free on malloc in hash function

I'm trying to improve my hash function in Speller and I'm having trouble freeing allocated memmory. I'm getting * Error in `./speller': free(): invalid pointer: 0x0000000001a02252 * Aborted I know ...
  • 1
0 votes
1 answer
128 views

Pset5 why calloc works and malloc doesn't ?

I wish i could understand better the application of malloc and calloc, i was using malloc to allocate memory for the root in my code and had the following error on valgrind: ==20066== Conditional ...
0 votes
1 answer
76 views

PSET 4: Resize. Does not work with dynamic buffer, only static

I managed to make resize work using a static array to store a row of pixels for the outfile (rewrite method). However, my first attempt with a dynamic array doesn't work, and I don't understand why. ...
0 votes
1 answer
703 views

PSET4: free(): invalid next size (fast)

So as the title says, I'm trying to do the pset4 where I get this error when I try to execute the program. I've searched the forum and also Reddit, and I understood that I've some issue with the ...
  • 13
0 votes
1 answer
35 views

Errors from context when running trie dictionary from pset5 speller through valgrind

I've managed to implement a trie data structure for speller, and it seems like there are no memory leaks, but when I run valgrind -v ./speller texts/lalaland.txt I get quite a lot of errors from ...
  • 119
0 votes
1 answer
94 views

PSet5 Hash Tables

I was learning how to create an array of linked lists for the pset5 problem. But i keep getting a segmentation fault when I go through a character linked lists a second time. Some kind of address keep ...
0 votes
1 answer
11 views

for cicle itrations

hi guys im wrote testing program to test features for my function and there is only 7 iterations in my for cicle but i malocc`ed more then that tell me why pls(testing string was andrew8mike) #...
  • 21
0 votes
1 answer
40 views

What will happen if you assign a static value to a pointer in C?

In this example, what will it happen if I assign for example: pk = 6; Thanks!
0 votes
1 answer
732 views

pset4 recover.c - working with fread() and malloc()

I'm attempting recover.c and I'm interested in clearing up a few things about fread() and malloc(). I tried to sample the first few bytes of the card.raw file but I keep getting the same two errors: ...
0 votes
1 answer
1k views

Assigning an empty array with malloc has a random number in it

Im trying to debug why my helpers.c in the find pset is not working perfectly. I kept getting random large numbers in my array and I kept going up the ladder in the function to the point of ...
0 votes
1 answer
236 views

pset 4 recover - segmentation fault with malloc

I seem to be stuck on pset4 recover and would appreciate some help! I think I understand the idea of the problem set by now but I've done approached the problem a bit unconventionally and keep getting ...
0 votes
1 answer
33 views

PSET 6 Problem with malloc() on parse function

I can't figure out this problem, I hope someone can help me. My server works perfectly only for the first HTTP request, but when I try to request something else, it throws me an error. This is a copy ...
0 votes
1 answer
540 views

pset6 parse function: Invalid read of size 1

I'm having trouble with allocating memory correctly. p is a pointer that I intended to point to method, target, then http switching to the next one after each space is found. Then I thought I'd be ...
0 votes
1 answer
57 views

check50 fails on caesar.c when using free()

I have a little problem. I can get get this to pass all the checks, but only if I remove the free() statement on L71 Link to code removed by andy5995 If I leave the free() statement in, this is ...
0 votes
1 answer
604 views

pset4 resize: how to copy a row

Just spent 2 days for figuring out how fread(), fwrite() and fseek() works. I tried to use "Rewrite" method from Zamyla's video for vertycal resizing: - Remember pixels in array (first written row of ...
0 votes
1 answer
168 views

Kinda confused with what needs memory allocated in pset5

I wanted to give hashtables a go with pset5. I've set the struct up as: typedef struct node { char *data; struct node *next; } node; Every time I link a new node all of the node->data ...
  • 23
0 votes
1 answer
206 views

pset 4 Recover creates 50 corrupted or mainly grey images

I wanted to try using malloc and a BUFFER structure of 512BYTES for this exercise with the intention of creating a file if the 4 signatures of a JPG are detected and fwriting the struct of 512B into ...
  • 3
0 votes
1 answer
490 views

pset4 malloc() approach. Help with size of buffer

I'm having some trouble with the malloc function. I want to make a buffer the size of the new row I'm going to write in the resized file, and this is my approach. I must say that bi.biWidth is ...
0 votes
1 answer
26 views

PSET6 - Why is my filename string getting weird characters after two refreshes?

This is my code to extract the filename requested from the server from the request message: char* buffer2 = strchr(buffer1, '/'); char* buffer3 = strchr(buffer2, ' '); char* buffer4 = malloc(strlen(...
  • 1
0 votes
1 answer
76 views

How can I avoid a Segmentation fault in my Recover.c

I have looked thoroughly through other solutions, but they tend to be specific to another person's code. I have also used Valgrind to try and isolate the problem, and considered that I may have to use ...
0 votes
1 answer
97 views

Garbage values when creating new node

So, for my load function in pset6, I'm creating a new node for the current char if it isn't found in the children. node* node_cur = &root_node; loop iterate over chars in word { if(node_cur-...
  • 157
0 votes
0 answers
5 views

malloc less amount of memory than needed but still works?

So I was just playing around with malloc() modifying the code given by David in Week 4's lecture. #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { ...
0 votes
0 answers
7 views

How can i allocate memory in the heap without loosing data?

I am writing a text game and my task is to create a function that allocates space in memory for a new game item and returns a reference to it. The task says that the name and description parameters ...
0 votes
1 answer
23 views

Why is the reference variable for dynamically allocated memory lower on the stack?

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int n = 5; char str1[n]; char *str2; char str3[] = "World!"; // ...
0 votes
1 answer
312 views

Why we use sizeof(char)

Can anyone please explain? char *s = get_string("s: "); if (!s) { return 1; } // allocate memory for another string char *t = malloc((strlen(s) +1) * sizeof(char)); if (!t) { return 1;...
0 votes
1 answer
78 views

pset5 speller unload - node allocation quirk

Most of this pset5 has gone smoothly, but unload() has caused me problems. When I'm navigating down my data structure (trie) to locate a node with all NULL children, the last index has a memory ...
0 votes
1 answer
53 views

Last word of dictionary file replaces all the nodes in node bucket

Currently I am working on the load function using a hash table with singly-linked list. I have tested the create(), insert(), and find() function in a separate file and they work as expected. ...
  • 1