-3

I see this quote in many books that one of major problems of Arrays in C/C++ is:

Arrays needs a contiguous block of memory

I can't really understand why this feature of Arrays considers a problem? I think compiler can manage and assign correct size to the Array, so what's the problem?

Afshin Mehrabani
  • 28,405
  • 26
  • 117
  • 186
  • 6
    A contiguous block of memory of the specified size may not be available. – Derek Aug 02 '13 at 20:17
  • 1
    possible duplicate of [What is memory fragmentation?](http://stackoverflow.com/questions/3770457/what-is-memory-fragmentation) where Steve Jessop beautifully illustrates what I tried to explain in my answer. – user7116 Aug 02 '13 at 20:18
  • 6
    It's not considered a problem. It's only a problem in cases where you don't want your data to require a contiguous block of memory. And it's a big advantage in cases where you do want your data to be contiguous. – Nikos C. Aug 02 '13 at 20:18
  • 1
    The statement you quoted doesn't imply that it's a problem. Who says it is? – Keith Thompson Aug 02 '13 at 20:20
  • @KeithThompson Here, http://ee.usc.edu/~redekopp/Streaming/CPPLib_review/CPPLib_review.html – Afshin Mehrabani Aug 02 '13 at 20:23
  • 1
    That's a 35-minute video. I lack the time or patience to watch it. But if you're asking why it's considered a problem, you should mention the source in your question. – Keith Thompson Aug 02 '13 at 20:24
  • @KeithThompson You're right. Just take a look at the minute 2:30. – Afshin Mehrabani Aug 02 '13 at 20:26
  • 1
    @AfshinMehrabani: As I said, that information should be in your question, not just in a comment. – Keith Thompson Aug 02 '13 at 20:30
  • 1
    @AfshinMehrabani: Why are you asking a question about some random video on the Internet from some random guy? – Nicol Bolas Aug 02 '13 at 20:33
  • @NicolBolas he has an important sounding voice – aaronman Aug 02 '13 at 20:35

4 Answers4

0

It's not a problem per say but it means that a contiguous block in memory has to be found to get space for the array. If your memory is very fragmented this may be tricky. That being said it is often very good that arrays have to be continuous because it means that you can have regular memory access and have fewer cache misses, creating cache friendly code.

Now on the other hand there are structures like linked lists which contain pointers to the other elements, this means they can be in different parts of memory and don't have to be continuous but it also means that when you try to access the pointer it may not be cached and thus you have a cache miss delaying the program.

Community
  • 1
  • 1
aaronman
  • 17,266
  • 6
  • 57
  • 78
0

When you consider the case of memory fragmentation, you may not be able to find a free block of memory which meets the size requirements of an entire array.

This is especially easy to see with certain allocation/deallocation schemes where thousands of variable sized objects are quickly allocated and deallocated, which can leave multiple holes in the free memory (depending on the strategy used). At this point, an ill timed allocation of a large object can fail to find any free space.

user7116
  • 60,025
  • 16
  • 134
  • 166
0

defragmentations happen within memory and then, when trying to allocate a memory, you may not have the correct size needed.

aah134
  • 790
  • 11
  • 21
0

The reason why a contigious block is an important feature is, when you call functions in the operating system, they usually expect the whole buffer being one byte after the other. For your own code it may not be a problem, because the array implementation can encapsulate the access to it, so that multiple smaller chunks could be presented to the outside as if it were ocntigious. Passing that memory to other functions, which don't know about your particular implementation, this no longer holds true.

Another, smaller issue, may be performance when accessing the array.

Devolus
  • 20,356
  • 11
  • 56
  • 104