Hot answers tagged

6

OK, this is going to be long answer, because what you've asked spans many subjects, so bare with me. Arrays vs Pointers First, let's learn the difference between arrays and pointers. When you declare an array as: int nums[20]; you reserve 20 consecutive places in memory, which you can refer to by the variable nums. Specifically, you reserve these places ...


5

When you allocate memory by calling malloc(), and you want to free that memory, you have to call free() on every pointer that you initialized it with a call to malloc(). For example, if you have something like tasklist_container *cont = malloc(sizeof(tasklist_container)); // 1 allocation for (int i = 0 i < TASKLISTLENGTH; i++) { cont -> list[i] =...


4

The problem lies in fread(&storage, sizeof(char), 1, old_file); This line is storing the data read directly in the pointer, overwriting the address of the malloc'd memory. It isn't storing the data at the memory pointed to by the address stored in the pointer. When the free() call is executed, it's trying to free an invalid address. It is also creating a ...


3

You have to keep in mind that a node is only accessible through a previous node when dealing with a singly-linked-list. Also, since you've allocated memory separately for the string named word which is a member of struct words, you also have to free that separately "before" freeing the data associated with the pointer to the struct that contains this word ...


2

The problem is that you are not printing using the same format in gdb and with the xxd command. xxd outputs hexadecimal and gdb outputs in decimal. To print in the same format with both programs a solution can be this. In the command line type: xxd -g 1 -l 4 card.raw Notice that -g 1 separetes the output of xxd in hexadecimals of one byte width, and the ...


2

You have only implemented part of your goal. This code will walk down a linked list to the last element, but will only free memory when it finds that the next element is null, i.e., it is at the end of the list. To free memory as you go (beginning to end), you should create a temp pointer to hold the address of the next element, and then free the current ...


2

yes, indeed there is a difference! when you use malloc, the block of memory that you are trying to allocate gets allocated in a region of memory called the heap. freeing memory allocated on the heap has to be done manually (by calling free). when you declare an array of a certain size, depending on where you declare your array, the memory for it may get ...


2

I originally didn't alloc for the struct, just declared it. It worked, and from what I understand, it worked because it was created in the stack? Am I correct? the type BITMAPINFOHEADER is not a pointer type. it is similar to other native types (e.g., int, char, etc) except that it is a little more complex. when declaring a variable of that type, you ...


2

In addition to the points made by @MARS and @NullityNull, there are advantages and disadvantages to both methods. Allocating memory on the stack is more convenient because it automatically cleans up after itself when the variable is no longer in scope. Sometimes this is called "garbage collection". However, this convenience comes at a cost of fixed size: ...


2

char c[3]; is the guilty declaration of segfault, the array space is insufficient should be resolved with: char c[8]; keep in mind that we must store something like 001.jpg which are seven characters plus the end character of string \0


2

You just opened your file for writing (and reading). This erased your card.raw's content. The 0 you find in block[0] is the initial value of your allocated memory, which by coincidence is 0. The 255 you find in c is actually a -1 returned by fgetc (and means EOF - End Of File). When converting the int-type -1 to an unsigned char, only the least significant ...


1

It has nothing to do with the malloc call itself. The problem lies in what you think you are checking. Look at the following: RGBTRIPLE* row = ... int sizeRow = sizeof(row); row is declared as a pointer. sizeRow contains the size of the pointer row, not the actual size of the space allocated by the malloc that row points at. A good analogy is this: My ...


1

While this may not be the only problem, the math is "backwards". Remember, buffer3 is greater than buffer2 (found later in the string). So, buffer2 - buffer3 would not be the result you are after. Also, make sure to allocate enough memory for the null terminator. The reason it works sometimes: memory is fickle. :)


1

You have a misunderstaning of the return value from the fread() call. It will return the number of elements read - the third parameter. If it works correctly, the third parameter is returned. If it reads less, it will return the number of elements actually read. At EOF, it actually returns 0. In practice, given the size of the data, it will never return 1. ...


1

I'm working on the same pset but it seems like you have an infinite loop. Are you sure your first if statement is working correctly and ending when it reaches the EOF? Otherwise you'll just keep creating jpgs until you run out of memory.


1

well, no where to start, first see the hash tablenode* hashtable[ALPHA], is an array of pointers to node, are pointers we will use to store our words according to their index hash, to equal hash index equal pointer, for these pointers do not need malloc, your loop to check does not work, possibly segmentation fault. hashtable[k] == NULL, it is unnecessary ...


1

First, the problem with the code lies in this line: fwrite(&buf, sizeof(BUFFER), 1, outptr); Adding the & is causing the problem. If you remove it, it will work. If you look at the man page for fwrite, you will see that the first parameter is supposed to be a pointer, not the contents of the memory pointed at, which is what the & triggers. (It'...


1

You will get the following message in CS50 IDE: error: incompatible integer to pointer conversion assigning to 'int *' from 'int'. In the code above, pk is a pointer to an integer, not an integer itself. Therefore, assigning an integer to it will give you a compile-time error.


1

fread() reads the contents of a file (in your case 'card' is the pointer to the file to be read from) and places the contents into a buffer (in your case 'pattern'). The number of bytes placed into the buffer from one read is determined by the second and third parameters. Every time you call read it will read sizeof(uint32_t) * 1 bytes from the file ...


1

In the description for malloc, note the following sentence: DESCRIPTION Allocate size bytes of memory. Unlike calloc, malloc will not pre-set all allocated memory to zero. This tells you what's happening (memory allocated by malloc can contain any values), and it also presents one way to fix this. Now to the specific dimensions. You probably would want to ...


1

First, unsigned char *buffer = malloc(sizeof(buffer)); will give you a buffer of size 8 (because sizeof(buffer)) when buffer is a pointer is the size of a pointer (8 bytes). You need a buffer that will hold 512 bytes, so perhaps malloc(512); would be better. The only time sizeof(buffer) would work in this program is if your buffer were declared as an ...


1

This is what line looks like in memory, together with the space you're allocating for linecopy: This could cause the erratic behavior you're seeing. Other than that, you're extracting query and abs_pathwrong whenever a ? is present in line, but that would be material for another question. If this is enough to answer your question, please click the check ...


1

This is a tough one. It has to do with reusing p and realloc. I cannot find a specific technical explanation, but regardless, there are things that are problematic with this approach. First, you cannot depend on realloc to keep the same address. It'll start out with p = method;, ie p pointing to the same address as method. But if realloc "moves" p, then ...


1

What's causing your seg fault on this line is the sizeof operator: newNode->word = malloc(sizeof(strlen(word) + 1)); What you really meant here was: malloc((strlen(word) + 1) * sizeof(char));


1

In check, you don't copy the terminator, you could add a tmpWord[wordLen] = 0; between the for- and the while loop. It makes little to compare word and tmpWord (the lowercase version of word), but strcmp(targetList->word, tmpWord) might be work. Also, you never store anything in your hash table. After copying the word to the structure, I'd expect ...


1

It appears that check50 is using a slightly older version of the CS50 Library than the CS50 IDE is currently using. In that version, all strings created using GetString are freed for you before the program finishes. So when your program hits the free(text), the program segfaults with a 'double free' error. You don't see this in the IDE because the CS50 ...


1

"Everyone's looking for a zebra. I see the injured horse." All of the file reads and writes are using the outptr output file pointer. The only reference to the input file pointer inptr is the fclose statement at the end. Wouldn't it work better if the code read data from the input file instead of the output file? If this answers your question, please click ...


1

There's a bunch of issues here: 1) You're using the wrong type for your buffer array. It should store bytes, not ints. 2) Your first for loop does nothing at all. It only calls fread once without ever checking if the signature is there. What would happen if there's a jpeg signature there? 3) sprintf doesn't allocate memory for the string it produces. You'...


1

The problem is because you're allocating the wrong size for root root = malloc(sizeof(root)); the size of root is typically 4 bytes on 32-bit systems. You actually need to allocate sizeof node which is typically 1 (for is_word) + 27 * 4 (for children) + 3 (padding) = 112 bytes.


1

That's because you declared your array inside your load function which makes it a local to it — it goes out of scope as the function returns. Even if you allocated memory for your array on the heap (which I think of as not the best solution), you'll lose reference to it if it's declared inside your function — the memory will still be allocated, but you have ...


Only top voted, non community-wiki answers of a minimum length are eligible