0

I have a 2d array represented as double pointer- `

   char ** arr;
  arr = (char **) malloc(100 * sizeof(char *));
    for (i=0; i<100; i++)
         arr[i] = (char *) malloc(3 * sizeof(char));

Now I have 100 rows and 3 columns in arr.But this array is used somewhere else which fills far less rows than 100.So how can I get the size(number of rows filled) in order to print this array???

  • 2
    How is it "used somewhere else"? How is it passed in, and what is done in said-code. – WhozCraig Feb 20 '15 at 09:26
  • 5
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family. – Sourav Ghosh Feb 20 '15 at 09:28
  • Just with this information..You would need a count of number of rows filled manually you can pass a address of the variable to function which fills the array and gets the value of number of rows filled – Gopi Feb 20 '15 at 09:31

2 Answers2

2

You can't, you're going to have to use a more expressive representation that can hold such meta information.

Memory is memory, there's no way to determine if it has been "used" or not, since it's there all the time once you've allocated it.

If you can't use the sentinel approach (like C strings, have a terminator indicate end-of-valid-data) you're going to have to use explicit length values or some other approach that expresses this.

Also, please don't cast the return value of malloc() in C.

Further, don't scale allocations by sizeof (char) since that's always 1 you're only adding noise. This is, quite obviosuly, my opinion. Your code will never be technically wrong if it includes that multiplication, and some clearly feel that it adds value and makes the code clearer.

Finally, you are doing 100 heap-allocations of 3 bytes each, that is very inefficient. I would suggest just doing an array of 100 3-byte arrays (possibly expressed as an array of structs).

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • So how can I print the array??If I print it all the 100 rows it prints garbage values as only 5 are filled with valid data –  Feb 20 '15 at 09:32
  • @Coder Please read what I wrote. You must add information about how many elements in the array are valid. – unwind Feb 20 '15 at 09:33
  • @Coder Don't print all 100 rows keep track of number of rows filled and just use that value to print out the rows – Gopi Feb 20 '15 at 09:33
  • Ok that is one way.But is there no way I can use `strlen` or some other inbuilt method? –  Feb 20 '15 at 09:34
  • @Coder No, since the data in question isn't a string. – unwind Feb 20 '15 at 09:41
  • These are all good advices. Although "sizeof (char) is always 1 you're only adding noise" - I'd say this is your own personal opinion. I think sizeof(char) can make the code more readable in some cases, it is a way of writing self-documenting code. I think it is far better to write `malloc(N*sizeof(char));` than `malloc(N); // allocate N characters`. – Lundin Feb 20 '15 at 10:21
  • @Lundin Fine. I don't understand what the latter `malloc()` could possibly do except attempt to allocate space for `N` character, that is after all what it does by definition. :) – unwind Feb 20 '15 at 10:33
  • @unwind Suppose you have some odd code like `int* ptr = malloc(N);`, then it isn't really obvious if your intention was indeed to allocate N bytes or if this is a bug and you actually meant to allocate N integers. – Lundin Feb 20 '15 at 10:35
  • @Lundin That's why you (=me, that is) always write such code as `int *ptr = malloc(N * sizeof *ptr);`. I tend to not do so *exactly* when I'm allocating character/byte buffers though, which is perhaps a bit inconsistent. – unwind Feb 20 '15 at 10:37
  • @unwind Well there you go then, you should be using `char *ptr = malloc(N * sizeof(*ptr));` to keep your coding style consistent :) – Lundin Feb 20 '15 at 10:39
0

I have a 2d array represented as double pointer

No you don't. You have a pointer-to-pointer based lookup table. To dynamically allocate multi-dimensional arrays, do something like this. Also see How do I correctly set up, access, and free a multidimensional array in C?.

But this array is used somewhere else which fills far less rows than 100.So how can I get the size(number of rows filled) in order to print this array???

Have your application keep track of it. There is no other way. sizeof etc won't work, not even if you use a proper array instead of a lookup table. Because sizeof knows nothing about the contents of the array. The best and simplest solution is to keep a separate size counter variable.

If you for some reason can't use such a size counter, you could make something more complex. Surround the code filling in values with some wrapper API. Then suppose for example that you allocate 101 items instead of 100 and use either the first or last item to contain the size. You'll have to clear all memory cells if you use such a method, so preferably use calloc instead of malloc in that case.

Or possibly make a linked list implementation if you need to add/remove items in the middle.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396