编译时数据段太大

时间:2016-02-17 07:38:42

标签: c++ c embedded microcontroller 8051

这里,我将一个位数组传递给其他函数。 由于数组大小太大,它会抛出一个错误,说'&34;数据段太大"在编译时。

我新编了代码。但是,错误:数据段太大仍然存在

这是代码:

char TxBits[]={0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,     
               0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,1,0,1,1,0,1,1,1,0,
               0,0,0,1,1,0,0,0,1,0,0,1,0,0,1,1,1,1,1,1,0,1,0,1,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0};

 int nTxBits = sizeof(TxBits)/sizeof(char);

void data(char *TxBits,int nTxBits, int loopcount)
{

  int i;

  for (i = 0;i < nTxBits;i++)
  {

    gpio=TxBits[i];    
    wait(loopcount);
  }

}

所以,我正在考虑将数组中的位转换为字节并传递给函数。我可以知道怎么办吗?愿意接受建议。 请回复

5 个答案:

答案 0 :(得分:2)

从你的代码中我认为你正在使用一些微控制器,所以我不确定你是否认真对待C ++标签。如果是的话,这是一个C ++风格的解决方案,它使用std::bitset(专用容器来处理需要更少空间的位):

std::bitset<134> foo (std::string("01010101010101010101010100101010101010101010101010010101010101010101010101001010101010101010101010100101010101010101010101010100000000"));

void data(const std::bitset& bitset, int loopcount) {
  // if C++11 
  for (auto& bit : foo) {
    gpio = bit;
    wait(loopcount);
  }

  // if C++98
  // for (int i = 0; i<bitset.size(); i++) {
  //   gpio = foo[i];
  //   wait(loopcount);
  // }
}

答案 1 :(得分:1)

你可能需要这个:

void data(char *TxBits, int size)  // size of the number of elements of your table
{
  int i;

  for (i = 0;i < size; i++)
  {
    gpio=TxBits[i];    
    wait(loopcount);
  }
}

调用函数

data(TxBits, sizeof(TxBits) / sizeof(TxBits[0]);

要获取数组元素的数量,我们使用sizeof(TxBits) / sizeof(TxBits[0],其中sizeof(TxBits)是数组在内存中占用的字节数,sizeof(TxBits[0]是数组中一个元素的大小

答案 2 :(得分:1)

  

我将一个位数组传递给其他函数

不,您传递的是一个字节数组,每个字节的二进制值为00000000或00000001。

为了节省内存,您应该将位值存储为实际位而不是字节:

uint8_t TxBits[]=
{  0x55, // 0,1,0,1,0,1,0,1,
   0x55, // 0,1,0,1,0,1,0,1,
   0x55, // 0,1,0,1,0,1,0,1,
   0x00, // 0,0,0,0,0,0,0,0,
   0x20, // 0,0,1,0,0,0,0,0,
   ...
};

size_t nTxBits = sizeof(TxBits) / 8;

每当进行算术运算时,你也应该避免使用char类型,因为它具有实现定义的符号。

此外,如果这是一个小型微控制器系统,您应该尽可能在ROM而不是RAM中分配数据。那就是:const uint8_t TxBits[]

答案 3 :(得分:0)

您的参数未正确声明。替换这个:

this

由此

void data(char TxBits)

答案 4 :(得分:0)

你的功能

4 5 3 2

应该是

void data(char TxBits)

您可以通过以下方式致电:

void data(char *TxBits, size_t nTxBits)
{
    int i;

    for (i = 0;i < nTxBits;i++)
    {
        gpio=TxBits[i];    
        wait(loopcount);
    }
}

在这种特定情况下,您有一个data ( TxBits, sizeof(TxBits)/sizeof(TxBits[0]) ); 数组,您也可以写:

char