Questions tagged [malloc]

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

Filter by
Sorted by
Tagged with
3
votes
1answer
13k 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
1answer
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 ...
2
votes
1answer
81 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
1answer
190 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 ...
1
vote
1answer
560 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 ...
1
vote
2answers
597 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 ...
1
vote
1answer
407 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 ...
1
vote
1answer
36 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
1answer
6k 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? ...
0
votes
1answer
133 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
1answer
207 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
2answers
295 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
3answers
127 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
2answers
239 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
1answer
132 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
1answer
451 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 ...
0
votes
2answers
114 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 ...
0
votes
1answer
39 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
1answer
124 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
1answer
31 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 ...
0
votes
1answer
105 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
1answer
91 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 ...
0
votes
1answer
95 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
1answer
193 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 ...
0
votes
1answer
34 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 ...
0
votes
1answer
83 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
1answer
10 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) #...
0
votes
1answer
31 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
1answer
615 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
1answer
605 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
1answer
225 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
1answer
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
1answer
354 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
1answer
49 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
1answer
571 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
1answer
140 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 ...
0
votes
1answer
198 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 ...
0
votes
1answer
464 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
1answer
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(...
0
votes
1answer
74 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
1answer
408 views

Bad pointer null assignment followed by malloc fail — WHY?

I'm stumped! In dictionary.c I have coded the load function (with more code that follows the bug, but not needed for this question.) One small block of code is producing a problem! Assigning NULL to ...
0
votes
1answer
93 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-...
0
votes
1answer
74 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
1answer
208 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
1answer
75 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
1answer
41 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. ...
0
votes
1answer
71 views

PSET5. Free a variable outside a loop. leaking memory

I've been stuck for days in the implementation of LOAD with a TRIE data structure. I think my code works (well it doesn't show seg faults) but I am getting a lot of memory leaks. I implemented the ...
0
votes
1answer
64 views

pset5 can't figure out what's wrong! segmentation fault

I have no idea what I am doing wrong in this pset! I feel very lost. Anybody have any pointers on where to even begin with what I'm doing wrong? Some specifics -- I am getting a segmentation fault ...
0
votes
0answers
45 views

I am trying to use this code segment to know the execution time of the C program by initializing the matrices a,b and c.Can someone help?

This is the question /* code segment */ for (k=0; k<n; k++) { for (i=0; i<n; i++) { r = a[i][k]; for (j=0; j<n; j++) { c[i][j] += r + b[k][j]; }/* end for…...
0
votes
1answer
1k views

malloc and fgets

#include<stdio.h> #include<cs50.h> int main(void) { char* s= malloc(sizeof(char)*5)); //allocating 5 bytes fgets(s,6,stdin); //allocating 5 bytes for hello and 1 byte for '\0'...