I need to convert a small code from Java to C. In Java I was able to dynamically create a List of Lists of Integers without knowing the size of the outer list in advance. I might have the size of inner lists and each one is of different size. How can I do the same in C?
Asked
Active
Viewed 86 times
0
-
Thanks but this method require prior knowledge of the size (`dimension1_max`) – Kaur Feb 20 '15 at 03:23
-
Yours can grow/shrink? It's unfortunate this isn't C++ :C – Blob Feb 20 '15 at 03:25
-
2You can always resize using `realloc` – Jon Egeland Feb 20 '15 at 03:26
-
Or, you could implement a [`LinkedList`](http://www.cprogramming.com/tutorial/c/lesson15.html) and have it work almost exactly like the Java version. – Jon Egeland Feb 20 '15 at 03:27
-
Thanks @Jon. Will the linked list be any slower than multidimensional arrays? Also, memory efficiency? – Kaur Feb 20 '15 at 03:30
-
There is no pre-defined List in C. You'd have to implement your own somehow, or struggle through just using C arrays. – Hot Licks Feb 20 '15 at 03:30
-
@Kaur. Well, it's not the *most* efficient. But realistically, it won't make a difference unless your data set is somewhere in the millions. – Jon Egeland Feb 20 '15 at 03:31
-
@Jon The dataset is actually in millions :( – Kaur Feb 20 '15 at 03:36
-
1A linked Hash table (or more precisely Hash table of separate chaining with linked lists) would be great :) And It could act like your requirement – Kavindu Dodanduwa Feb 20 '15 at 03:38
-
@Kaur even then, the difference is minimal, as long as your list implementation is efficient (there are thousands of sources online that are pretty well optimized). And I'd still wager that using linked lists is a better choice. It's a pretty standard way of dealing with dynamically-sized arrays. – Jon Egeland Feb 20 '15 at 03:39
-
Thanks @Jon. I will start with linked list. – Kaur Feb 20 '15 at 03:43
-
@KcDoD: Thanks, I will look up linked hashtable in C. – Kaur Feb 20 '15 at 03:43