handling variable length arrays?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

handling variable length arrays?

Jump to solution
1,190 Views
irob
Contributor V

Is there a good way of handling variable length arrays that I'm just not seeing?  Here's what I have.

 

I'm writing a general purpose I2C library with all the usual suspects, like Start, Stop, Ack, Nack, Write, and Read functions, etc.  For the Read function, I'd like to accommodate a range of I2C slave peripherals.  For instance, I've used devices that have no registers, some that do have registers, the majority of which only return single octets of data at a time when read, but some that return multiple octets.  So I figured it would be cool to make the Read function such that I could send it how many bytes to read back.  Well, this requires some allocated memory block, say an array, to dump into.

 

But if this Read function is general purpose, then the array depth is not known at compile time.  I tried writing my function to return a pointer to a static memory location.  Trouble is, the compiler only allocates the first data byte pointed to by this pointer.  The rest of the data is in unallocated space and might get trampled on.

 

I finally gave up and just created a global array that I knew would be big enough for most devices.  Then I call my Read function and pass it a pointer to this array.

 

Not elegant!  Any suggestions?

Labels (1)
Tags (1)
0 Kudos
1 Solution
486 Views
bigmac
Specialist III

Hello irob,

 

I might suggest that the general purpose IIC function library should not contain any code that directly relates to a specific IIC slave device.  The buffer array variable, and its size, required by the read process should therefore be defined outside of the function library.

 

The read function within the library would then be passed a pointer to the start of the buffer, plus the number of bytes to be read.

 

Maybe the following prototype for the library function:

void IIC_read( byte slave_addr, byte *buf, byte n);

 

#define IIC_BUFSIZE  4

#define SLAVE_ADDR   0x3C

byte IIC_rdbuf[IIC_BUFSIZE]  // Define buffer array

.

...

.

IIC_read( SLAVE_ADDR, IIC_rdbuf, IIC_BUFSIZE);

 

 

 

Multiple buffers could be defined if more than one IIC device, without affecting the library functions.  The buffer would not necessarily need to be a global variable.

 

Regards,

Mac

 

View solution in original post

0 Kudos
3 Replies
487 Views
bigmac
Specialist III

Hello irob,

 

I might suggest that the general purpose IIC function library should not contain any code that directly relates to a specific IIC slave device.  The buffer array variable, and its size, required by the read process should therefore be defined outside of the function library.

 

The read function within the library would then be passed a pointer to the start of the buffer, plus the number of bytes to be read.

 

Maybe the following prototype for the library function:

void IIC_read( byte slave_addr, byte *buf, byte n);

 

#define IIC_BUFSIZE  4

#define SLAVE_ADDR   0x3C

byte IIC_rdbuf[IIC_BUFSIZE]  // Define buffer array

.

...

.

IIC_read( SLAVE_ADDR, IIC_rdbuf, IIC_BUFSIZE);

 

 

 

Multiple buffers could be defined if more than one IIC device, without affecting the library functions.  The buffer would not necessarily need to be a global variable.

 

Regards,

Mac

 

0 Kudos
486 Views
irob
Contributor V

You and I were thinking along the same lines, bigmac.  But you went one further by suggesting essentially separate read arrays for each IIC slave device.  I like this idea and have adopted it.  Until your example, I hadn't realized you could pass an array to a function.  Thought only pointers were legal.  Thanks!

0 Kudos
486 Views
bigmac
Specialist III

Hello irob,

 

Strictly speaking, when you pass an array name to a function, this represents the address of the first element of the array, i.e. a pointer to the array.  In this reapect, array variables differ from other variables.

 

The string manipulation library functions would also use a similar method.

 

Regards,

Mac

 

0 Kudos