Hot answers tagged

14 votes

Memory overlap in C

I believe you mean memmove which takes care of memory overlapping as oppose to memset. but what is memory overlapping anyway? suppose we have an array of 5 chars, where each char is a byte long ++++...
user avatar
  • 17.4k
8 votes
Accepted

(pset5) valgrind - 1 block not freed

Hmmm..... 568 bytes in 1 block.... sounds like a file pointer. Did we forget to close an open file somewhere???? ;-) Side note: valgrind usually tells you how you can get more information, ...
user avatar
  • 63.1k
7 votes
Accepted

How does valgrind work?

Valgrind basically runs your application in a "sandbox." While running in this sandbox, it is able to insert its own instructions to do advanced debugging and profiling. From the manual: Your ...
user avatar
  • 483
4 votes
Accepted

what happens to memory when a variable dies?

accessing a variable after it went out of scope is not defined in C. I assume by "erased" you mean "zero-filled" or something and, in this case, no, the memory is not zero-filled because why bother? ...
user avatar
  • 17.4k
4 votes
Accepted

how is C memory management different from Java?

In Java, when JVM process starts, then some memory (defined by Xms and Xmx) is allocated. How do we know that how much memory is allocated in case of C program, and can we control this program ...
user avatar
  • 17.4k
4 votes
Accepted

When we free a dynamically allocated memory, what happens to the pointer pointing at it?

Yes, when you use a free(px); call, it frees the memory that was malloc'd earlier and pointed to by px. The pointer itself, however, will continue to exist and will still have the same address. It ...
user avatar
  • 63.1k
3 votes

Does only malloc allocated memory on the heap?

In the first case, I am clear that memory will be allocated on stack. not necessarily. it depends on where the variable is defined. if it's a global variable for example, the memory will be allocated ...
user avatar
  • 17.4k
3 votes
Accepted

Memory allocation: recommended practices

I'm not sure you needed to learn about pointers and memory management in order to solve hacker 2. I think you've gone a little bit far. However, I'm gonna try answering your questions. A pointer is ...
user avatar
  • 17.4k
3 votes
Accepted

In breakout, why use 12 char to store an int?

The question is hinting at you to think about why the array is initialized with a size of 12.
user avatar
3 votes

Why do macros take more space than an equivalently defined function?

The plain and simple truth is: Macros are NOT stored directly in memory.(during program execution and/or compiling) What happens is, that, the values are copied from the macro to where you used the ...
user avatar
  • 1,170
3 votes
Accepted

Why do macros take more space than an equivalently defined function?

A function is identified by an entry point (address) inside the executable code. You use a function from different parts of your code. If you use a macro, the object code of that macro is substituted ...
user avatar
  • 1,156
3 votes
Accepted

Pset5 Memory leak in unload()

Complicated code in terms of memory management. This set of problems deals with the traditionally more difficult concepts of C programming, so we need a good theoretical basis for dealing with them. ...
user avatar
  • 5,172
2 votes

what happens to memory when a variable dies?

What happens to memory varies from one programming language to another, and sometimes from one platform to another. With some languages, a variable is discarded from the program, such as when a ...
user avatar
  • 63.1k
2 votes
Accepted

Week 4, Section : Dynamic Memory Allocation

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 ...
user avatar
  • 3,035
2 votes
Accepted

GetString() & Dynamic Memory Allocation

Yes it's true that for simplicity's sake, in the first psets, we din't free() the strings allocated by GetString() and that caused memory leaks. Nevertheless, most modern operating systems can see ...
user avatar
  • 7,326
2 votes
Accepted

What will realloc() do if the contiguous memory location is already occupied?

The answer of our friend @NullityNull is correct, as long as realloc succeeds and works well. Your question is interesting. Consider the following program fragment char *string; string = (char *)...
user avatar
  • 5,172
2 votes

What will realloc() do if the contiguous memory location is already occupied?

If realloc can't resize the memory block you pass in, it makes a new one, copies the data, and deallocates the old one. If I were you I'd read up a bit on specification for Realloc and malloc.
user avatar
2 votes
Accepted

Pset5 Speller Valgrind error

Without seeing more code, it's hard to know. A leak of 568 bytes in 1 block usually indicates a file that wasn't closed. Is it possible that there's a return command executed before the dictionary ...
user avatar
  • 63.1k
2 votes
Accepted

pset5 speller how to only use 7.4 kb memory?

It depends on the metrics used, I think those are heap and stack memory. Global variables don't show up in those statistics. Now if you can estimate an upper limit for the size of the dictionary...
user avatar
  • 20.7k
2 votes

pset5 speller how to only use 7.4 kb memory?

You don't have to use the heap (dynamic allocation) nor the stack (local variables) to store the dictionary. You can declare a static data structure as global and house the dictionary in it. For ...
user avatar
2 votes
Accepted

Intuition for what is statically allocated memory vs dynamically allocated memory

It sounds like you've already got it, but you're just not all that confident in your grasp of the concepts. Since you have the mechanics down, maybe an analogy will help. Think of memory as your ...
user avatar
  • 63.1k
2 votes

Why Doesn't fwrite overwrite previously copied data?

From this article: One of the attributes of an open file is its file position that keeps track of where in the file the next character is to be read or written. On GNU systems, and all POSIX.1 ...
user avatar
1 vote

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

Memory allocated using malloc can initially contain anything, you would have to give the individual fields of your newly allocated struct values yourself. It still works because fresh memory usually ...
user avatar
  • 20.7k
1 vote
Accepted

Pset4 - Array declaration- Heap

Arrays you declare as int array[42]; are stored on stack, like any other variable. For arrays like int *array = (int*)malloc(42*sizeof(int));, a pointer array is stored on stack, the memory block of ...
user avatar
  • 20.7k
1 vote

Memory leakage PSET 5

Following up on my previous comment, it seems that I was indeed running out of stack memory. I spent some time streamlining my code by eliminating a couple of temporary pointers that were ultimately ...
user avatar
1 vote
Accepted

pset 4 recover related question

You would have to read and interpret the partition, or device if it does not use partitions, not a file of the filesystem, as the file system driver might not provide any way to address "free" space ...
user avatar
  • 20.7k
1 vote
Accepted

Writing a word puzzle solver and having a problem with freeing heap memory. Would really appreciate some help!

When I tried to run your program, it ran for a long time before finally segfaulting. I put in a print statement to count the number of calls to your checkPermutations function, and it was more than 1....
user avatar
  • 18.3k
1 vote
Accepted

PSET5 - Help with Valgrind? error : "uninitialised value(s)"

Try using calloc instead of malloc (the syntax is slightly different, but you can google it, it's not difficult). The difference is that calloc initializes all the memory (in this case you might want ...
user avatar
  • 1,677
1 vote

How to end with an unsuccesful memory allocation?

Here are my thoughts on your interesting question. It touches on the broader category of error handling. Any return value != 0, in the main function signals an error. Whatever return (error) value ...
user avatar
1 vote

Memory overlap in C

With memcpy, the destination cannot overlap the source at all. With memmove it can. This means that memmove might be very slightly slower than memcpy, as it cannot make the same assumptions. For ...
user avatar

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