1

Array by definition is a chunk of contiguous memory locations. What i understand from Dynamic is that we can allocate memory at run time. So for Dynamic Array lets say we allocate 100 memory spaces at run time. Now we perform some other operation and allocate few memory spaces for other variables. Since the array we created was meant to be Dynamic so i suppose we can add more elements t it occupying more memory spaces. But what i fail to understand is how this is going to be contiguous because the next memory addresses are per-occupied and Arrays by definition are supposed to be Contiguous???

6
  • I know it might seem a bit naive. But little help here this thing is stuck in my mind i cant get rid of it. Commented Sep 14, 2013 at 10:28
  • 1
    You can't really expand an array. What is usually done is to allocate a larger one, copy the elements of the original one into it, and delete the original. Commented Sep 14, 2013 at 10:29
  • So we cant add elements in array even though we make it dynamic?? Commented Sep 14, 2013 at 10:30
  • Do you mean contiguous ? Commented Sep 14, 2013 at 10:31
  • @315 right. If you make an array of size N, you cannot expand it to contain more than N elements. Well, not in a way that is guaranteed to always work. Commented Sep 14, 2013 at 10:32

1 Answer 1

1

You are mixing terminologies. In C and C++ arrays are technically a sequence of contiguous elements of the same type. When using the term "dynamic array" you are not using a standard-mandated term, but a general computer science term, which refers to array-like containers whose size can change at runtime.

In C++ these array-like containers are implemented, for example, through the vector class, available including the <vector> standard library header. In other languages there may be other facilities.

C has no dynamic arrays, but you can allocate arrays dynamically. Here the term "dynamic" has a different meaning: it is related to the management of dynamic memory, which is what you do when you use malloc and free functions.

In C++, dynamic arrays are implemented managing dynamic memory under the hood, but this is an implementation detail that has little to do with the terminology.

Also in C you could implement dynamic arrays (for example writing a custom library), but their usage wouldn't be supported by the language syntax, as in C++.

Whether or not the storage for a dynamic array (either in C++ or in C) is contiguous is, again, an implementation detail. If you need to have a dynamic array (general meaning) implemented as a C/C++ array (technical term), you will end up reallocating memory and copying elements around explicitly. The standard C library provides the realloc function to help coping with such a task.

Sign up to request clarification or add additional context in comments.

6 Comments

They could also mean a dynamically allocated array, i.e. one allocated via e.g. new or malloc.
So is there a difference between "Dynamic Array" and "Dynamically allocated Array"
@315 Yes, as I said, it is a terminology issue: "dynamic array" is a generic term which has nothing to do conceptually (at least in C/C++ context) with "dynamically allocated array".
@315 If it matters at all, a dynamically allocated array in C is generally managed with realloc(). Read the docs of that function to understand why.
@WhozCraig Thanks for the clarification! I forgot to mention that. If you don't mind I'll add it to my answer for completeness.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.