数组需要帮助 [英] Arrays help required

查看:90
本文介绍了数组需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用C作为我的任务之一我想将一些布尔数据读入一个数组可能是一维或两个。为了节省内存,我打算制作一个结构,我将用于将布尔数据存储到二维数组中。问题是我不知道究竟哪种数据类型适合,以便我可以节省内存。我使用uint8_t作为数据类型。我需要检查我是否在正确的轨道上。


i希望在二维数组中存储位,即1'和0'。这实际上只保存了所述数组中的位,或者它是否保存了整个位索引?

即假设值[0] [j]存储{1,1,1,1,1}或

值[1] [j]存储{ 0,0,0,0,0}


代码片段如下

hi

i am using C for one of my task i want to read an some boolean data into into an array might be one dimensional or two. for saving memory i am intending to make a bit structure that i am going to use for storing boolean data into a two dimensional array. the problem is i dont know exactly which data type is suitable so that i could save memory. i have used uint8_t as a data type. all i need to check whether i am on right track or not.

i want to store bits i.e 1''s and 0''s in a two dimensional array.is that really save only the bits in said array or is it saving the whole bit in the index?
i.e suppose Value[0][j] stores {1,1,1,1,1}or
Value[1][j] stores {0,0,0,0,0}

the code snippet is as follows

展开 | 选择 | 换行 | 行号

推荐答案

这不会为你节省太多,因为结构Save_Bits仍然占用1个字节 - 所以每个元素的值[i] [j] ha独特的地址。

我会选择vector< bool>并使用手动计算的index = col * column_count + row访问每个元素。
This will not save you much, because structure Save_Bits still occupies 1 byte - so that each element of Value[i][j] has distinct address.
I would go with vector<bool> and access each element at with manually calculated index = col*column_count+row.


数组显示应该可以帮助您了解数组在内存中的布局。


您希望有多少数组元素?在我个人看来,记忆很少是如此短缺,以至于值得让程序变得更加复杂。程序越复杂,就越有可能出错。
Arrays Revealed should help you understand how arrays are laid out in memory.

How many array elements do you expect to have? In my personal opinion, memory is rarely in such short supply that it is worth making the program more complicated. The more complicated the program, the more likely you are to have a mistake.


感谢您的回复。

i am这也是一个问题因为数量元素没有固定它们可能达到1000.为什么我要节省内存是因为我使用控制器的项目,他们没有这么大的记忆。

并感谢分享文章。


@ newb16

你可以通过一些例子向我展示我将如何使用矢量< bool>并手动计算索引,实际上我没有得到你的观点。

谢谢
thanks for the reply .
i am that is also a problem because the number of elements are not fixed they might reached upto 1000. and why i am going to save memory is because i am using a controller for a project and they dont have such big memory.
and thanks for shareing the article.

@newb16
can you plz show me with some example how i gonna use vector<bool> and to calculate index manually, actually i didnt get ur point.
thanks


这篇关于数组需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆