Questions tagged [malloc]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
30 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
75 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
29 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
84 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
106 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
83 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
84 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
73 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
173 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
204 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
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
79 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
74 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
40 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
1answer
162 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. #...
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 &...
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) #...
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 ...
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
571 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
492 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
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
206 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
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'...
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
298 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
67 views

Memory address in 64 bit OS

How long is the memory address of a single byte of 8 bytes allocated to pointer variable in a 64bit OS?
0
votes
2answers
280 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
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
555 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
3answers
119 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
1answer
44 views

PSET6: Question with Mallocing char* for PARSE

I'm currently working on PSET6 and had a question for the parsing section. I decided to use strtok() and as a result, will need to malloc memory when creating new char*. How do i know how much memory ...
0
votes
2answers
236 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
0answers
141 views

pset4/resize | reading and writing a file simultaneously?

I'm trying to use "malloc" approach to the task. My algorithm is: draw a multiplied scanline into a new file fread this line, save itto a string, write it to the file factor-1 times do the same ...
0
votes
1answer
132 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
195 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
5k 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
451 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
42 views

2D array of given size

I want to create a 2D array of ints, where the array's dimension is given at run time (not compile time). So far, I have been using: int **ptr = malloc(m * sizeof(int*)); for (size_t i = 0; i < m;...
1
vote
1answer
516 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 ...
0
votes
1answer
20 views

Dynamic Allocation Section Video

Just watched the section video in Week 4 on dynamic memory allocation. Near the end, after the "free(b);" case. What if the next case was " a= &b;" ? From the video, it seems like the pointer ...
1
vote
2answers
549 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 ...
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
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
64 views

Pset4 Recover fails check, looks right and file sizes are right

struggling with the last piece of the puzzle for Recover. I'm pretty sure It's related to a memory leak of sorts, but I fail to understand how to resolve it. Here is my code: http://pastebin.com/...
0
votes
2answers
382 views

pset5 valgrind not happy with load (also, using malloc vs calloc on trie)

First, valgrind flags the file pointer at the fopen call, and also the malloc call to the root pointer. It designates both still reachable in loss record. Second, since for pset5 I check for an end ...
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]