简体   繁体   English

C中二维数组的大小

[英]Size of 2d array in c

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??? 现在我在arr中有100行3列。但是这个数组用在其他地方,它填充的行远远少于100.所以我如何获得大小(填充的行数)以打印此数组?

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. 如果您不能使用哨兵方法(如C字符串,使用终止符指示有效数据的结尾),则必须使用显式长度值或其他表达此值的方法。

Also, please don't cast the return value of malloc() in C . 另外, 请不要在C中malloc()的返回值

Further, don't scale allocations by sizeof (char) since that's always 1 you're only adding noise. 此外,请勿按sizeof (char)缩放分配,因为它始终为1,只会增加噪音。 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. 最后,您正在执行100个堆分配,每个堆分配3个字节,这是非常低效的。 I would suggest just doing an array of 100 3-byte arrays (possibly expressed as an array of struct s). 我建议仅做一个100个3字节数组的数组(可能表示为struct的数组)。

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? 另请参阅如何在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??? 但是这个数组在其他地方使用,它填充的行远远少于100.所以我如何获得大小(填充的行数)以打印此数组?

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. sizeof等不起作用,即使您使用适当的数组而不是查找表也不行。 Because sizeof knows nothing about the contents of the array. 因为sizeof对数组的内容一无所知。 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. 用一些包装器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. 然后假设例如您分配101个项目而不是100个项目,并使用第一个或最后一个项目来包含大小。 You'll have to clear all memory cells if you use such a method, so preferably use calloc instead of malloc in that case. 如果使用这种方法,则必须清除所有内存单元,因此在这种情况下,最好使用calloc而不是malloc。

Or possibly make a linked list implementation if you need to add/remove items in the middle. 或者,如果您需要在中间添加/删除项目,则可以进行链接列表实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM