Hot answers tagged

14

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 +++++++++++++++++++++++++++++++ | 'a' | 'b' | 'c' | 'd' | 'e' | +++++++++++++++++++++++++++++++ 0x100 0x101 0x102 0x103 0x104 now according to the man page of ...


8

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, with something like For counts of detected and suppressed errors, rerun with: -v If this answers your question, please click on the check mark to accept. ...


7

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 program is then run on a synthetic CPU provided by the Valgrind core. As new code is executed for the first time, the core hands ...


4

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? what happens is that the memory that this variable used to identify is again available to use after this variable goes out of scope and it may be reused when ...


4

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 memory? programs written in Java have memory automatically managed for them. the case is different in C. that is, you can simulate that manually by taking ...


4

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 will not automatically be changed to NULL or anything else. The only thing that will remove the pointer var from the stack would be if it went out of scope, just ...


3

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 on a read/write data segment. but yes, in case it's local, the memory will be allocated on the stack. In the second case, I think there will be problem ...


3

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 basically a variable that stores an address of a location in memory. Pretty much every type has its own pointer type. For example, char has char *, int has int *,...


3

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


3

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 macro during preprocessing. When it comes to the compiler they don't even exist, they've been replaced by the preprocessor before they get that far. That is, ...


3

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 in each section of the code where it is called, so you have multiple copies of the same code instead of just one called by reference.


3

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. We must distinguish between a pointer that stores a memory address only, and the declaration of a new node in the linked list "node * tmp = malloc (sizeof (node)...


2

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 variable goes out of scope. A good example is when you call a function in C. Vars are created inside the function and data is stored there, but later, you leave the ...


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

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 file was closed? If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


2

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 *)malloc(100); string = NULL; One of the most common failure when memory is managed explicitly is what is known as "memory leak". This situation occurs when a ...


2

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.


2

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...


2

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 example, if you were to use a trie, you can check (empirically) what's the maximum number of nodes you'll need, and initialize a global 2D array with that number of ...


2

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 garage. You have a tool box, shelves, and built in storage. Those are the same as static memory - the fixed vars that are part of the code. They are allocated ...


2

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 systems, the file position is simply an integer representing the number of bytes from the beginning of the file. The file position is normally set to the beginning of ...


1

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 starts at all zeroes, but there's no guarantee that's the case for all systems. You could use calloc instead of malloc, that one initialises the allocated ...


1

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 42 ints is allocated on the heap. Don't confuse those two parts. While the stack part ends its life on exiting its scope (the block it was declared in), the ...


1

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 unnecessary in my recursive function that unloads the dictionary and adjusting the way '\''s are handled and that seems to have eliminated the small memory leak. ...


1

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 as files. The .raw file is essentially a copy of such a partition/device, containing the file system. On Linux, devices reside in the /dev/ directory, for ...


1

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.5 million in some of my runs before I ctrl-c'd out of it. I haven't analysed your code to see where the segfault is happening, because, quite honestly, the ...


1

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 when a program exists with memory leaks, and frees the memory itself. So even if you run your program a million times you would not notice any memory shortage (...


1

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 to zero initialize it). malloc doesn't. So, on line 112, when you do if (current->children[index] == NULL) Here there's no guarantee that it will (or ...


1

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 is given is arbitrary? To a large extent the return value is arbitrary. It (usually) only has meaning to the "caller". Recall the discussion of $? (testing ...


1

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 example, memcpy might always copy addresses from low to high. If the destination overlaps after the source, this means some addresses will be overwritten before ...


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