Skip to main content

Questions tagged [malloc]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
23 views

cs50 pset5 not enough memory

I have been stuck here for several days. After loading 52209 words, I keep encountering Segmentation fault (core dumped). I think that I am consuming memory unnecessarily somewhere in the code, but I ...
Ahmed Hesham's user avatar
0 votes
1 answer
36 views

check50 indicates that my code has memory errors but executing valgrind in terminal says my code has zero memory errors

what is the problem with my code #include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef uint8_t BYTE; int main(int argc, char *argv[]) { //check for usage if (...
Michael's user avatar
0 votes
1 answer
77 views

Getting segmentation fault when running speller.c

I am trying to solve speller and I always get segmentation fault every time I run it. I checked the error on valgrind and it told me that the culprit is in line 108, where I allocate a new node. How ...
IgorArnaut's user avatar
0 votes
2 answers
18 views

Getting segmenatation fault even though i freed memory

#include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef uint8_t BYTE; int main(int argc, char *argv[]) { // Only allow 2 command line arguments if (argc != 2) ...
lukadeving's user avatar
0 votes
1 answer
29 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 ...
therubberduck's user avatar
0 votes
2 answers
293 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. ...
Lperino's user avatar
  • 14
0 votes
1 answer
28 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!"; // ...
Matt Shirvan's user avatar
0 votes
1 answer
58 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 ...
Uzayer Masud's user avatar
0 votes
1 answer
572 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 ...
Dragos Toader's user avatar
0 votes
1 answer
35 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 ...
Egill's user avatar
  • 3
0 votes
1 answer
154 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 ...
tomas-silveira's user avatar
0 votes
1 answer
288 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 ...
Antwan Samir's user avatar
0 votes
1 answer
115 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 ...
Victor's user avatar
  • 1
0 votes
1 answer
158 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 ...
Murillo Guindani's user avatar
0 votes
1 answer
77 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. ...
user3230142's user avatar
0 votes
1 answer
2k 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 ...
Dement0's user avatar
  • 13
0 votes
1 answer
703 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;...
Lara Corika's user avatar
0 votes
1 answer
39 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 ...
doctopus's user avatar
  • 119
0 votes
1 answer
96 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 ...
Arnold Heinminthant's user avatar
0 votes
1 answer
81 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 ...
Scott J.'s user avatar
0 votes
1 answer
60 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. ...
TQL's user avatar
  • 1
0 votes
1 answer
85 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 ...
Diego Miquilena's user avatar
0 votes
1 answer
83 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 ...
Audrey Breitwieser's user avatar
0 votes
1 answer
288 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. #...
Candaş Ünal's user avatar
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 &...
user10784's user avatar
0 votes
1 answer
14 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) #...
Andrew's user avatar
  • 21
2 votes
1 answer
3k 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 ...
Karin's user avatar
  • 59
0 votes
1 answer
49 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!
gonzaloorsi's user avatar
0 votes
1 answer
804 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: ...
Kyle's user avatar
  • 15
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 ...
Tod Lazarov's user avatar
0 votes
0 answers
48 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…...
qwerty's user avatar
  • 64
0 votes
1 answer
240 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 ...
Jeremy Chow's user avatar
0 votes
1 answer
2k 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'...
qwerty's user avatar
  • 64
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 ...
Carlos C's user avatar
0 votes
1 answer
659 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 ...
OrderAndChaos's user avatar
0 votes
1 answer
98 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?
qwerty's user avatar
  • 64
0 votes
2 answers
349 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) ...
Levblanc 's user avatar
0 votes
1 answer
60 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 ...
Andy Alt's user avatar
  • 302
0 votes
1 answer
605 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 ...
Semyon Pilyukov's user avatar
0 votes
3 answers
159 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));...
spirosbax's user avatar
0 votes
1 answer
48 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 ...
Bobby's user avatar
  • 41
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 ...
Vaibhav Kulshrestha's user avatar
0 votes
0 answers
143 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 ...
Fed's user avatar
  • 3
0 votes
1 answer
171 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 ...
Michael's user avatar
  • 23
0 votes
1 answer
212 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 ...
alo129's user avatar
  • 3
0 votes
1 answer
9k 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? ...
Laura's user avatar
  • 17
0 votes
1 answer
494 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 ...
Alejandro Aristizábal's user avatar
0 votes
1 answer
28 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(...
edd91's user avatar
  • 1
0 votes
2 answers
66 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;...
dpalma's user avatar
  • 123
1 vote
1 answer
853 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 ...
Xia's user avatar
  • 45