多个数组和指针问题 [英] Multiple Array and pointer issue

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

问题描述

我正在通过新闻组常见问题解答我的问题的答案,

但它似乎没有。


我是使用内存非常有限的系统。我是

定义两个缓冲区来传入和传出数据:

unsigned char inputBuffer [100];

unsigned char outputBuffer [100];


但现在事实证明,在某些情况下我需要输出缓冲区大于

。我想做的是让输出缓冲区双倍

大小:


unsigned char outputBuffer [200];


然后使用输入缓冲区作为outputBuffer的一部分:

unsigned char * inputBuffer = outputBuffer + 100;


必要时,然后outputBuffer可以使用大数组,并且/ b $ b输入缓冲区也可以拥有它的空间,而不必为outputBuffer分配额外的100字节

。 />
所以我做了这个改变,但是,现在输入不起作用。我想

认为代码中存在的下标仍然有效。

我的代码全部使用inputBuffer [location]作为访问
$ b $的方式b inputBuffer。


所以我的问题是,我做错了什么?


RonB

I was looking through the newsgroup FAQ for an answer to my question,
but it doesn''t appear to have one.

I am working with a system that has VERY limited memory. I was
defining two buffers to pass in and out data:

unsigned char inputBuffer[100];
unsigned char outputBuffer[100];

But now it turns out I need the output buffer to be larger under
certain cases. What I wanted to do was make the output buffer double
size:

unsigned char outputBuffer[200];

And then use the input buffer as a portion of outputBuffer:

unsigned char *inputBuffer = outputBuffer + 100;

When necessary, the outputBuffer could then use the large array and
inputBuffer would have it''s space as well, without having to allocate
the extra 100 bytes for the outputBuffer.
So I made this change, however, now input is not working. I would
have thought subscripting that was existing in code would still work.
My code all uses inputBuffer[location] as means of accessing the
inputBuffer.

So my question is, what am I doing wrong?

RonB

推荐答案

Ron Blancarte在06/07/07 13:08写道:
Ron Blancarte wrote On 06/07/07 13:08,:

我正在浏览新闻组回答我的问题的常见问题解答,

但它似乎没有。


我正在使用内存非常有限的系统。我是

定义两个缓冲区来传入和传出数据:

unsigned char inputBuffer [100];

unsigned char outputBuffer [100];


但现在事实证明,在某些情况下我需要输出缓冲区大于

。我想做的是让输出缓冲区双倍

大小:


unsigned char outputBuffer [200];


然后使用输入缓冲区作为outputBuffer的一部分:

unsigned char * inputBuffer = outputBuffer + 100;


必要时,然后outputBuffer可以使用大数组,并且/ b $ b输入缓冲区也可以拥有它的空间,而不必为outputBuffer分配额外的100字节

。 />

所以我做了这个改变,但是,现在输入不起作用。我想

认为代码中存在的下标仍然有效。

我的代码全部使用inputBuffer [location]作为访问
$ b $的方式b inputBuffer。


所以我的问题是,我做错了什么?
I was looking through the newsgroup FAQ for an answer to my question,
but it doesn''t appear to have one.

I am working with a system that has VERY limited memory. I was
defining two buffers to pass in and out data:

unsigned char inputBuffer[100];
unsigned char outputBuffer[100];

But now it turns out I need the output buffer to be larger under
certain cases. What I wanted to do was make the output buffer double
size:

unsigned char outputBuffer[200];

And then use the input buffer as a portion of outputBuffer:

unsigned char *inputBuffer = outputBuffer + 100;

When necessary, the outputBuffer could then use the large array and
inputBuffer would have it''s space as well, without having to allocate
the extra 100 bytes for the outputBuffer.
So I made this change, however, now input is not working. I would
have thought subscripting that was existing in code would still work.
My code all uses inputBuffer[location] as means of accessing the
inputBuffer.

So my question is, what am I doing wrong?



你的指针没有任何固有的错误

算术,如果这就是让你担心的事情。但请注意,

这并不奇怪地创造了100个额外的字节

的空气。你实际上在做的是分享这个200-char缓冲区的后半部分,使用那些位置

作为输入和长输出的尾端。

你处理过它们之后,你的输入字符是否在你的输入中踩到了?比如说,你把数据读入

inputBuffer,然后用空格填充outputBuffer,然后

试图处理突然全空的inputBuffer?

如果没有看到任何代码,那就是我可以管理的所有

猜测。


-
< a href =mailto:Er ********* @ sun.com> Er ********* @ sun.com


2007年6月7日星期四13:25:19 -0400(当OU吮吸时),Eric Sosman

写道:
On Thu, 07 Jun 2007 13:25:19 -0400 (while OU was sucking), Eric Sosman
wrote:

> Ron Blancarte在06/07/07 13:08写道:
>Ron Blancarte wrote On 06/07/07 13:08,:

>我正在浏览新闻组常见问题解答我的问题的答案,
但它似乎没有。

我正在使用内存非常有限的系统。我定义了两个缓冲区来传入和传出数据:

unsigned char inputBuffer [100];
unsigned char outputBuffer [100];

但现在事实证明,在某些情况下我需要输出缓冲区更大。我想做的是让输出缓冲区大小为:

unsigned char outputBuffer [200];

然后使用输入缓冲区作为一部分outputBuffer:

unsigned char * inputBuffer = outputBuffer + 100;

必要时,outputBuffer可以使用大数组,
inputBuffer可以使用它空间也是如此,无需为outputBuffer分配额外的100个字节。

所以我做了这个改变,但是,现在输入不起作用。我会认为代码中存在的下标仍然有用。
我的代码都使用inputBuffer [location]作为访问
inputBuffer的手段。

所以我的问题是,我做错了什么?
>I was looking through the newsgroup FAQ for an answer to my question,
but it doesn''t appear to have one.

I am working with a system that has VERY limited memory. I was
defining two buffers to pass in and out data:

unsigned char inputBuffer[100];
unsigned char outputBuffer[100];

But now it turns out I need the output buffer to be larger under
certain cases. What I wanted to do was make the output buffer double
size:

unsigned char outputBuffer[200];

And then use the input buffer as a portion of outputBuffer:

unsigned char *inputBuffer = outputBuffer + 100;

When necessary, the outputBuffer could then use the large array and
inputBuffer would have it''s space as well, without having to allocate
the extra 100 bytes for the outputBuffer.
So I made this change, however, now input is not working. I would
have thought subscripting that was existing in code would still work.
My code all uses inputBuffer[location] as means of accessing the
inputBuffer.

So my question is, what am I doing wrong?



你的指针算术没有任何本质上的错误,如果这就是让你担心的事情。但是请注意,这并不是神奇地创造了100个额外的字节。你实际上在做的是分享后两半的200-char缓冲区,使用那些位置来输入和长输出的尾端。在您处理之前,您的
输出字符是否在您的输入中踩到了?你是说,把数据读入
inputBuffer,然后用空白填充outputBuffer,然后尝试处理突然全空的inputBuffer?


没见过任何代码,都是关于我能管理的所有猜测。


There''s nothing inherently wrong with your pointer
arithmetic, if that''s what''s worrying you. Note, though,
that this doesn''t magically create 100 extra bytes out
of thin air. What you''re actually doing is sharing the
latter half of the 200-char buffer, using those locations
for input and for the tail end of long outputs. Are your
output characters stomping all over your inputs before
you''ve processed them? Are you, say, reading data into
inputBuffer, then filling outputBuffer with blanks, then
trying to process the suddenly all-blank inputBuffer?

Without having seen any code, that''s about all the
guessing I can manage.



该死的,我确实遗漏了一段。


所以是的,我没有神奇地创造100字节,我只是在特殊情况下分享那个

回100(如果我确定输入缓冲区中的信息,我不会介意)。正常情况不应该是b $ b b是一个问题


我遇到的问题是,我的函数似乎与
一起工作:


unsigned char inputBuffer [100];

unsigned char outputBuffer [100];


don''似乎正在使用:


unsigned char inputBuffer [100];

unsigned char * inputBuffer = outputBuffer + 100;


这些函数不会产生足够的数据来实际敲击
100字节的限制。


例如:


无效SPI_receive()

{

systat_7 = 0;

SPICON = 0x31;

SPI_COMM_SS = 0;

SPIDAT = 0;

而(ISPI == 0);

inputBuffer [spi_in] = SPIDAT ;

++ spi_in;

if(!( - spiSize)){

systat_3 = 1;

systat_1 = 0;

}

SPI_COMM_SS = 1;

}


void spi_getSize( )

{

*((char *)& spiSize)= inputBuffer [1];

*((char *)& spiSize + 1)= inputBuffer [2];

}


无效SPI_send()

{

systat_7 = 0;

SPICON = 0x31;

if(!systat_5){

SPI_COMM_SS = 0;

SPIDAT = outputBuffer [OB_out];

while(ISPI == 0);

if(!( - spiSize))

systat_5 = 1;

+ + OB_out;

ISPI = 0;

SPI_COMM_SS = 1;

}

else {

systat_1 = systat_3 = systat_5 = systat_6 = 0;

systat_4 = 1;

}

}


这些似乎是我现在的问题函数。发生的事情是

,你最初通过SPI总线读取3个字节的数据。那么你

调用spi_getSize(),这样就知道发送了多少个字符。

然后你开始使用SPI_receive()从公共汽车上拉出数据1 />
一次一个字符并将其放入缓冲区。完成一些

处理后,通过SPI_send将响应发送回

SPI总线。


spi_in和OB_out只是索引,允许我通过缓冲区逐个字符遍历
。每个缓冲区都有一个out和

的指示(spi_in / spi_out和OB_in / OB_out)


RonB

Damn, I did leave out a paragraph on this.

So yea, I didn''t magically create 100 bytes, I am just sharing that
back 100 under the special circumstances (which I won''t mind if I
clobber the information in the input Buffer). Normal cases should not
be a problem

What problem that I am having is that it seems my functions which
worked with:

unsigned char inputBuffer[100];
unsigned char outputBuffer[100];

don''t appear to be working with:

unsigned char inputBuffer[100];
unsigned char *inputBuffer = outputBuffer + 100;

These are functions which don''t produce enough data to actually hit
the 100 byte limit.

For example:

void SPI_receive()
{
systat_7 = 0;
SPICON = 0x31;
SPI_COMM_SS = 0;
SPIDAT = 0;
while (ISPI == 0);
inputBuffer[spi_in] = SPIDAT;
++spi_in;
if(!(--spiSize)){
systat_3 = 1;
systat_1 = 0;
}
SPI_COMM_SS = 1;
}

void spi_getSize()
{
*((char*)&spiSize ) = inputBuffer[1];
*((char*)&spiSize+1) = inputBuffer[2];
}

void SPI_send()
{
systat_7 = 0;
SPICON = 0x31;
if(!systat_5){
SPI_COMM_SS = 0;
SPIDAT = outputBuffer[OB_out];
while (ISPI == 0);
if(!(--spiSize))
systat_5 = 1;
++OB_out;
ISPI = 0;
SPI_COMM_SS = 1;
}
else{
systat_1 = systat_3 = systat_5 = systat_6 = 0;
systat_4 = 1;
}
}

These appear to be my problem functions right now. What happens is
that you xfer 3 bytes of data over the SPI bus initially. Then you
call spi_getSize(), so you know how many characters are being sent.
Then you start to use SPI_receive() to pull data off the bus 1
character at a time and place it into the buffer. After some
processing is done, and then the response is sent back out across the
SPI bus via SPI_send.

spi_in and OB_out are just indices which allow me to traverse
character by character through the buffers. There is an out and in
indice for each buffer (spi_in/spi_out and OB_in/OB_out)

RonB


2007年6月7日星期四13:39:39 -0500(当OU吮吸时),Ron

Blancarte写道:
On Thu, 07 Jun 2007 13:39:39 -0500 (while OU was sucking), Ron
Blancarte wrote:

> 2007年6月7日星期四13:25:19 -0400(当OU吮吸时),Eric Sosman写道:
>On Thu, 07 Jun 2007 13:25:19 -0400 (while OU was sucking), Eric Sosman
wrote:

>> Ron Blancarte在06/07/07 13:08写道:
>>Ron Blancarte wrote On 06/07/07 13:08,:

>>我正在浏览新闻组常见问题解答回答我的问题,
但它似乎没有。

我正在使用一个内存非常有限的系统。我定义了两个缓冲区来传入和传出数据:

unsigned char inputBuffer [100];
unsigned char outputBuffer [100];

但现在事实证明,在某些情况下我需要输出缓冲区更大。我想做的是让输出缓冲区大小为:

unsigned char outputBuffer [200];

然后使用输入缓冲区作为一部分outputBuffer:

unsigned char * inputBuffer = outputBuffer + 100;

必要时,outputBuffer可以使用大数组,
inputBuffer可以使用它空间也是如此,无需为outputBuffer分配额外的100个字节。

所以我做了这个改变,但是,现在输入不起作用。我会认为代码中存在的下标仍然有用。
我的代码都使用inputBuffer [location]作为访问
inputBuffer的手段。

所以我的问题是,我做错了什么?
>>I was looking through the newsgroup FAQ for an answer to my question,
but it doesn''t appear to have one.

I am working with a system that has VERY limited memory. I was
defining two buffers to pass in and out data:

unsigned char inputBuffer[100];
unsigned char outputBuffer[100];

But now it turns out I need the output buffer to be larger under
certain cases. What I wanted to do was make the output buffer double
size:

unsigned char outputBuffer[200];

And then use the input buffer as a portion of outputBuffer:

unsigned char *inputBuffer = outputBuffer + 100;

When necessary, the outputBuffer could then use the large array and
inputBuffer would have it''s space as well, without having to allocate
the extra 100 bytes for the outputBuffer.
So I made this change, however, now input is not working. I would
have thought subscripting that was existing in code would still work.
My code all uses inputBuffer[location] as means of accessing the
inputBuffer.

So my question is, what am I doing wrong?


你的指针算术本身并没有什么问题,如果这就是令你担心的事情。但是请注意,这并不是神奇地创造了100个额外的字节。你实际上在做的是分享后两半的200-char缓冲区,使用那些位置来输入和长输出的尾端。在您处理之前,您的
输出字符是否在您的输入中踩到了?比方说,你是把数据读入
inputBuffer,然后用空白填充outputBuffer,然后尝试处理突然全空的inputBuffer?

没有看到任何代码,那个关于我可以管理的所有猜测。


There''s nothing inherently wrong with your pointer
arithmetic, if that''s what''s worrying you. Note, though,
that this doesn''t magically create 100 extra bytes out
of thin air. What you''re actually doing is sharing the
latter half of the 200-char buffer, using those locations
for input and for the tail end of long outputs. Are your
output characters stomping all over your inputs before
you''ve processed them? Are you, say, reading data into
inputBuffer, then filling outputBuffer with blanks, then
trying to process the suddenly all-blank inputBuffer?

Without having seen any code, that''s about all the
guessing I can manage.


该死的,我确实遗漏了一个段落。

所以是的,我没有神奇地创造100个字节,我只是分享那个<在特殊情况下返回100(如果我在输入缓冲区中删除信息,我不介意)。正常情况不应该是一个问题

我遇到的问题是,我的功能似乎与
一起工作:

unsigned char inputBuffer [100];
unsigned char outputBuffer [100];

似乎没有使用:


Damn, I did leave out a paragraph on this.

So yea, I didn''t magically create 100 bytes, I am just sharing that
back 100 under the special circumstances (which I won''t mind if I
clobber the information in the input Buffer). Normal cases should not
be a problem

What problem that I am having is that it seems my functions which
worked with:

unsigned char inputBuffer[100];
unsigned char outputBuffer[100];

don''t appear to be working with:



ONE THING !!

我不知道这是否有所作为,但是......

我忘了提及,inputBuffer和outputBuffer都是全局的。

它们是通过SPI例程中的extern访问的。这个

会有什么不同吗?

ONE THING!!
I don''t know if this makes a difference or not, BUT...
I forgot to mention, inputBuffer and outputBuffer are both global.
They are being accessed via extern in the SPI routines. Will this
make any sort of differnce?


> unsigned char inputBuffer [100];
unsigned char * inputBuffer = outputBuffer + 100;

这些函数不能产生足够的数据来实际达到100字节的限制。

例如:

void SPI_receive()
{
systat_7 = 0;
SPICON = 0x31;
SPI_COMM_SS = 0;
SPIDAT = 0;
while (ISPI == 0);
inputBuffer [spi_in] = SPIDAT;
++ spi_in;
if(!( - spiSize)){
systat_3 = 1;
systat_1 = 0;
}
SPI_COMM_SS = 1;
}

void spi_getSize()
{
*((char * )& spiSize)= inputBuffer [1];
*((char *)& spiSize + 1)= inputBuffer [2];
}
void SPI_send()
{
systat_7 = 0;
SPICON = 0x31;
if(!systat_5){
SPI_COMM_SS = 0;
SPIDAT = outputBuffer [OB_out];
while(ISPI == 0);
if(!( - spiSize))
systat_5 = 1;
++ OB_out;
ISPI = 0;
SPI_COMM_SS = 1;
}
else {
systat_1 = systat_3 = systat_5 = systat_6 = 0;
systat_4 = 1;
}
}

这些似乎是我现在的问题。发生的事情是,您最初通过SPI总线读取3个字节的数据。然后你打电话给spi_getSize(),这样就知道发送了多少个字符。
然后你开始使用SPI_receive()从一个时间和地点拉出总线1
字符的数据它进入缓冲区。完成一些处理后,然后通过SPI_send将响应发送回
SPI总线。

spi_in和OB_out只是允许我遍历的索引。 />通过缓冲区逐个字符。每个缓冲区都有一个out和in
indice(spi_in / spi_out和OB_in / OB_out)

RonB
>unsigned char inputBuffer[100];
unsigned char *inputBuffer = outputBuffer + 100;

These are functions which don''t produce enough data to actually hit
the 100 byte limit.

For example:

void SPI_receive()
{
systat_7 = 0;
SPICON = 0x31;
SPI_COMM_SS = 0;
SPIDAT = 0;
while (ISPI == 0);
inputBuffer[spi_in] = SPIDAT;
++spi_in;
if(!(--spiSize)){
systat_3 = 1;
systat_1 = 0;
}
SPI_COMM_SS = 1;
}

void spi_getSize()
{
*((char*)&spiSize ) = inputBuffer[1];
*((char*)&spiSize+1) = inputBuffer[2];
}

void SPI_send()
{
systat_7 = 0;
SPICON = 0x31;
if(!systat_5){
SPI_COMM_SS = 0;
SPIDAT = outputBuffer[OB_out];
while (ISPI == 0);
if(!(--spiSize))
systat_5 = 1;
++OB_out;
ISPI = 0;
SPI_COMM_SS = 1;
}
else{
systat_1 = systat_3 = systat_5 = systat_6 = 0;
systat_4 = 1;
}
}

These appear to be my problem functions right now. What happens is
that you xfer 3 bytes of data over the SPI bus initially. Then you
call spi_getSize(), so you know how many characters are being sent.
Then you start to use SPI_receive() to pull data off the bus 1
character at a time and place it into the buffer. After some
processing is done, and then the response is sent back out across the
SPI bus via SPI_send.

spi_in and OB_out are just indices which allow me to traverse
character by character through the buffers. There is an out and in
indice for each buffer (spi_in/spi_out and OB_in/OB_out)

RonB


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

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