As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I would like to allot some huge dynamic memory and then write my own memory manager for it. i.e. As and when my code needs memory, I'd allot from this pool of memory. I want the algorithm to take care of internal and external fragmentation. Which is the most efficient algorithm for this?
For these criteria I'd go with Doug Lea's http://g.oswego.edu/dl/html/malloc.html, which maintains collections of blocks of store for each of a number of different sizes - it's quick to find the size you need, and reusing blocks of the same size reduces fragmentation. Note (http://entland.homelinux.com/blog/2008/08/19/practical-efficient-memory-management/) that this is NOT tuned for multi-threading.
If I was writing one myself I'd go for the http://en.wikipedia.org/wiki/Buddy_memory_allocation because it's fast and not commonly used in user space (not commonly used because it restricts the possible block sizes, leading to internal fragmentation). In fact, I did, some time ago - http://www.mcdowella.demon.co.uk/buddy.html
This question is amiguous because the term "most efficient" is not clear. You don't say in what terms it should be most efficient.
As an example: There is a strategy called first fit which might be faster than best fit but could lead to more out fragmenetation of the heap (a really bad thing). Best fit on the other hand reduces somewhat the outer fragmentation but still suffers from it while finding a chunk of free memory takes more time. There is also a strategy called buddy heap where you don't suffer from outer fragmentation but from inner fragmentation. But at least finding a free block is usually fast there.
You see choosing an algorithm really depends on your requirements. Should the allocation be fast or the fragmentation low? What's the allocation behavior? Are there small uneven chunks allocated and freed frequently or only big chunks? And there are even more factors playing a role here.
Maybe you wanted an answer like this. If not I recommend you clearify your requirements.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I came across this article: Should you ever use Linked List. It cites that given the technological advances in available memory and RAM structures, using arrays would be better than Linked List.
There's also an old question When to use a linked list over an array/array list?
Do the arguments in the article really hold and are/have Linked List become obsolete or what would be the scenarios where using a LinkedList would still be better than Arrays if the arguments are true ?
(Explainations for any point with example would be helpful)
Nonsense. O(n) will never beat constant-time. Any use of lists that's required to perform well for insertions with saved iterators will use linked lists. They're a fundamental structure and won't go away.
I'd spin the argument the other way: linked lists are more acceptable these days. On a 386, you have to be careful with performance, but now, we write programs in Python even and put up with their speed. From the amount of code written in languages that use a VM (or are interpreted) I think it's fair to say a lot of people aren't at the level of worrying about cache misses in their choice of data structure.
We have fast CPUs now, so often don't need to worry about the few extra instructions that might be needed in implementing our data structures. We can look at the uses we have, work out what requirements we have and pick our structures based on their asymptotic performance. This also makes the code more maintainable: you won't have to change in your code if you find out in six months' time that for n=100 list is quicker after all. Profiling is hard work, so we should be very comfortable in our CPU-guzzling days to pick the structure with the algorithmic properties we want rather than guessing at vector.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I tried googling it, but no solid answer. What are available, still maintained dynamic array implementations for C? What are pros and cons for every one of them, and what is the best one(speed/footprint ratio)? Just asking, so that I don't have to reinvent the wheel.
GArray from GLib does what you want.
If you are looking for something like NSMutableArray, from Objective-C, or something like ArrayList from Java, you won't find anything (std C, at least).
You can create your own dynamic array implementation in C, though. It will take you a few code lines and is not that hard to implement.
All you need to have in mind is Time vs Memory. You can do an implementation that allocates a new array with a bigger size, every time you push/add an element, and then pops it for you in the return or by reference, or you can reallocate memory every time. I don't see big advantages in neither one, except that realloc is a C library function that I think is low level implemented, meaning it is probably faster, and in matters of implementation I would go with the realloc one since it is faster to implement.
You can even build an api that gives you sorting types and clean all methods.
Now is up to you.
Hope this helps.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In C if i have only one line in the if condition then can i combine it into one line to reduce the size of code. The size of code is long so I intend to take this step to reduce size of code. Should I should I not? Is it a good programming practice?
It makes no sense to be concerned about the size of the sources at the point of sacrificing readability, especially since sources are extremely small compared to almost any kind of other data that our computers usually process/store (e.g. the bzipped sourced of the Whole Firefox are 85 MB - smaller than any medium-length video). Also, omitting a newline won't change the compilation times of a millisecond.
So, if you prefer one-line ifs for your own stylistic reasons it's fine (although it's often frowned upon), but for saving a few bytes it makes no sense at all.
Actually if the code is long then it's not good programming practice as that code will not be as readable.
so it's better to use if else syntax to make your code readable
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
So, I'm wondering if there's a good marker to choose in C, apart from the common 0xDEADBEEFor the less appealing (for proper code) 0x0BADA550.
What's your favorite?
Is there any reason to choose one particular value or another?
Wikipedia has a whole page on this subject; it provides a lot of examples and famous software in which they are used.
Anyway, if you are working on x86 you should consider following #torak's suggestion, memory filled with int 3 saved me several times. If you feel creative, you may make it more recognizable and use CC90CC90, which translates to alternated int 3 and nop.
The only marker that I can think of that might have more than asthetic value is 0xCCCCCCCC. If, through some kind of error, it was executed 0xCC translates to an INT 3 instruction.
This will work in IA-32 and x86-64. Im not sure if there are equivalents for other architectures.
Well I always picked 0x80000000, 0x80000001 incrementing for each new region type to tickle unexpected values with signed integers. Use of these values will be strangely large for unsigned types, largely negative for signed types, and abruptly become positive for any subtractions made (thereby testing for other bugs at the same time). This has another neat side effect, in that various ALU bits will be set such as overflow, which can be detected through use of -ftrapv and other compiler flags.
0xBABECAFE, 0xBADADD00, 0xBADBAD00, 0xFADEFADE
0xCAFEBABE is, I understand, the magic number for Java. The only thing that makes one better than another is how unlikely it is to appear in data or uninitialized memory. So don't use 0x00000000 or all Fs.
It depends what you're trying to mark. For example, do you need to distinguish between allocated but uninitialized memory and deallocated memory? If so, you need different markers for the two.
Everyone has their preference. If everyone on the team uses a different marker, it can help to determine the source of an error. As in:
"DEADBEEF? Oh, this must be from John's code."
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I have an interview coming up in a week's time for an entry level position that involves programming in CUDA (hopefully with C).
I was wondering if anybody can suggest some interview questions that I can expect during the interview.
I have gone through the official programming guide but I'm not all that convenient right now.
Thanks.
Some questions I think you should prepare are:
How many different kind of memories are in a GPU ?
What means coalesced / uncoalesced?
Can you implement a matrix transpose kernel?
What is a warp ?
How many warps can run simultaneously inside a multiprocessor?
What is the difference between a block and a thread ?
Can thread communicate between them? and blocks ?
Can you describe how works a cache?
What is the difference between shared memory and registers?
Which algorithms perform better on the gpu? data bound or cpu bound?
Which steps will you perform to port of an application to cuda ?
What is a barrier ?
What is a Stream ?
Can you describe what means occupancy of a kernel?
What means structure of array vs array of structures?
"You have N vectors of length M (N>>M). Tell me how you would go about designing a kernel to evaluate the distance matrix. Pay special attention to the way the problem is sub-divided and to the way the thread co-operation can be used to improve occupancy.
How would your answer to this question change if M>>N?"
The idea here is not to get you writing code, but to get you thinking out loud. This shows that you really know how to use GPGPU technology and are not merely regurgitating the user guide.
If it's a scientific role then expect questions on floating point and numerical accuracy, in particular you should look at the reduction sample in the NVIDIA SDK since that illustrates a whole load of the points in Fabrizio's post too.