473,765 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Array and pointer issue

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

Jun 7 '07
14 1785
On Jun 8, 9:39 am, Ron Blancarte wrote:
*((char*)&spiSi ze ) = inputBuffer[1];
*((char*)&spiSi ze+1) = inputBuffer[2];
This code is suspicious; you would be much
better off writing:
spiSize = inputBuffer[1] * 0x100 + inputBuffer[2];

or perhaps the other way around depending on
the desired result.

However, that probably isn't the source of your
problem. Based on the information provided,
I'd have to suspect you're overflowing the
output and thus overwriting your input, or you
are using 'sizeof' on the pointer. You could
debug the former by writing a sentinel value to
outputBuffer[99] and checking if it is still
there when you apparently have the garbage in
inputBuffer.

Jun 7 '07 #11
"CBFalconer " <cb********@yah oo.comwrote in message
news:46******** *******@yahoo.c om...
Ron Blancarte wrote:
>>
... snip ...
>>
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?

Please snip immaterial quoted material.

The answer is quite likely yes. They will affect anything such as
"sizeof buffer".
AND....
the SPI routines are going to expect character arrays, not character
pointers.

karl m

Jun 7 '07 #12
"Karl Malbrain" <ma******@yahoo .comwrites:
"CBFalconer " <cb********@yah oo.comwrote in message
news:46******** *******@yahoo.c om...
>Ron Blancarte wrote:
>>>
... snip ...
>>>
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?

Please snip immaterial quoted material.

The answer is quite likely yes. They will affect anything such as
"sizeof buffer".

AND....
the SPI routines are going to expect character arrays, not character
pointers.
I don't know what "SPI routines" are, but if they're declared in C,
that hardly seems likely.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 7 '07 #13
>On Thu, 07 Jun 2007 13:39:39 -0500 (while OU was sucking), Ron
>Blancarte wrote:
>>unsigned char inputBuffer[100];
unsigned char outputBuffer[100];
[then outputBuffer[] size increased and the other replaced, viz:
unsigned char outputBuffer[200];
unsigned char *inputBuffer = outputBuffer + 100;
]

In article <0v************ *************** *****@4ax.com>
Ron Blancarte <ron@---TAKETHISOUT---.blancarte.comw rote:
>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?
I am going to make a wild guess at the problem: you changed
the actual definitions of the two variables, but somewhere else
-- where they are used, as these "global" variables -- you left
in *declarations* of the form:

extern char inputBuffer[];
extern char outputBuffer[];

Since arrays are not pointers (nor vice versa), this causes
references of the form:

inputBuffer[i]

to generate "array access" instructions instead of the "pointer
accesss" instructions now need. (See the FAQ, section 6.)

Note that you may be able to get "better" (for some definition
of "better") code by writing:

unsigned char outputBuffer[200];
#define inputBuffer (&outputBuffe r[100])

and/or using (off-topic, but probably-available) assembler or
linker tricks to make the two buffers overlap. That is, on some
microprocessors , if some variable A is an array while some other
variable p is a pointer pointing into that array -- e.g.:

char A[100];
char *p = &A[0];

-- then, on that machine, the code generated for:

op(A[i])

is shorter and/or faster than that for:

op(p[i])

for most operations "op". (On other machines, it is a wash, and
in some cases, the pointer may be "better". The definition of
"better" is rather fuzzy in the first place, so this is something
that has to be defined clearly, then measured.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 8 '07 #14
On 8 Jun 2007 00:17:10 GMT, Chris Torek wrote:
>I am going to make a wild guess at the problem: you changed
the actual definitions of the two variables, but somewhere else
-- where they are used, as these "global" variables -- you left
in *declarations* of the form:

extern char inputBuffer[];
extern char outputBuffer[];
I was thinking the same thing, but no, not the problem. However, you
did mention...
>Note that you may be able to get "better" (for some definition
of "better") code by writing:

unsigned char outputBuffer[200];
#define inputBuffer (&outputBuffe r[100])
>and/or using (off-topic, but probably-available) assembler or
linker tricks to make the two buffers overlap. That is, on some
microprocessor s, if some variable A is an array while some other
variable p is a pointer pointing into that array -- e.g.:
Which got me digging into assembly, etc. It turns out that since I
was declaring the inputBuffer globally:

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

the value was being set at startup. The assembly code which was
running was re-mapping the XDATA space of the chip after this line.
It erased the pointer.

RonB

Jun 8 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
43955
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation -...
18
5410
by: Joshua Neuheisel | last post by:
The following code compiles with gcc 3.2.2 and Visual C++ 6: #include <stdio.h> int main() { int a = {3, 4}; printf ("%d\n", 0); return 0; }
13
2478
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing multiple allocation as follows: 1. I allocated an array of pointer. 2. I Read line by line from a text file. 3. I allocated memory for the read line.
9
6089
by: toton | last post by:
Hi, I am looking for a circular buffer solution ( a fixed buffer) , where the elements can be pushed back & removed front. It looks stl deque can be a solution. my question is, 1) circular buffer is random access? (i.e implemented over array and not linked list) 2) if I reserve the required space & do push_back & remove_front so that total memory doesn't exceed the reserved amount, will the internal pointers will wrap, or the memory...
14
4086
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
10
5468
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask this simple case.) /---- int array = { 1, 2, 3, 4 } \-----
1
2413
by: Memphis Steve | last post by:
Is it possible to combine multiple javascipts into one file and then call that file from a linked URL in the head section of an XHTML file? Here are the two scripts I want to use with the instructions on where to normally place them in an XHTML document: SCRIPT #1: Step 1: Insert the below anywhere into the <body> section of your page where you wish the clock to be displayed:<script> /* Live Date Script-
5
3311
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening the forms all at once, I'm opening them as needed. I have a main form with multiple records; and then I have a pop-up form that the user opens with button. The pop-up form contains one record relating to the current record in the main form (but...
5
2681
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.