Does someone know how I can use dynamically allocated multi-dimensional arrays using C? Is that possible?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- How to get the maximum of more than 2 numbers in V
- Faster loop: foreach vs some (performance of jsper
- Convert Array to custom object list c#
Why, colleagues, nobody revealed the fact that no exact solution exists in the realm of C? but only in C++ (by exact I mean that above and other site's solutions differ from true C-multidimentional arrays with: aditional entities and so additional memory, etc).
In C++ you must implement (just several lines of code):
That's all for those who use code like this:
a[i][j]
.But for exact similarity with C-multidimensional arrays that class (
Matrix2D
) lacks double-pointer arithmetics, to be used like this:(*(a+i))[j]
. That is not difficult: implement inner clsss "doublePointer" with arithmetics and dereference operators. But I'd rather implemented sort of iterator for such purposes (to step from row to row).More than 2 dimensions? You just need inner classes that implement corresponding
operator[]...[]
of (n-1)-dimension. Alas, routine..Since C99, 13 years now, C has 2D arrays with dynamical bounds. If you want to avoid that such beast are allocated on the stack (which you should), you can allocate them easily in one go as the following
and that's it. You can then easily use it as you are used for 2D arrays with something like
A[i][j]
. And don't forget that one at the endRandy Meyers wrote series of articles explaining variable length arrays (VLAs).
If you know the number of columns at compile time, it's pretty simple:
You can treat
ap
like any 2D array:When you're done you deallocate it as
If you don't know the number of columns at compile time, but you're working with a C99 compiler or a C2011 compiler that supports variable-length arrays, it's still pretty simple:
If you don't know the number of columns at compile time and you're working with a version of C that doesn't support variable-length arrays, then you'll need to do something different. If you need all of the elements to be allocated in a contiguous chunk (like a regular array), then you can allocate the memory as a 1D array, and compute a 1D offset:
If you don't need the memory to be contiguous, you can follow a two-step allocation method:
Since allocation was a two-step process, deallocation also needs to be a two-step process:
There's no way to allocate the whole thing in one go. Instead, create an array of pointers, then, for each pointer, create the memory for it. For example:
Of course, you can also declare the array as
int* array[50]
and skip the first malloc, but the second set is needed in order to dynamically allocate the required storage.It is possible to hack a way to allocate it in a single step, but it would require a custom lookup function, but writing that in such a way that it will always work can be annoying. An example could be
L(arr,x,y,max_x) arr[(y)*(max_x) + (x)]
, then malloc a block of 50*50 ints or whatever and access using thatL
macro, e.g.But that's much nastier unless you know the effects of what you're doing with the preprocessor macro.
malloc will do.
Refer the below article for help:-
http://courses.cs.vt.edu/~cs2704/spring00/mcquain/Notes/4up/Managing2DArrays.pdf