Jump to content
Sign in to follow this  
pittuck

Array Size Problem - Overflows....

Recommended Posts

okies found a bug:

 

Code:

 

void main()
{

// Main program routine
// Created 3/2/05 - Martyn Pittuck
// Modified -

// -- Call setup() function
setup();

char sensor_a[2];

sensor_a[0] = 0xFF;
sensor_a[1] = 0xF0;
sensor_a[2] = 0x0F;
sensor_a[3] = 0xEE;
sensor_a[4] = 0xE0;
sensor_a[50] = 0x0E;

while( 1 );

}

Build Output:

 

BoostC Optimizing C Compiler Version 1.9.2 Beta (for PIC18 architecture)

http://www.picant.com/c2c/c.html

Copyright© 2004-2005 Pavel Baranov

Copyright© 2004-2005 David Hobday

 

 

success

BoostLink Optimizing Linker Version 1.9 Beta

http://www.picant.com/c2c/c.html

Copyright© 2004-2005 Pavel Baranov

Copyright© 2004-2005 David Hobday

Warning: Unable to successfully create 'delay_us' for target with clock freq 4000000 Hz

Warning: argument of 'delay_10us' calls must have a value of 1 or more

Building CASM file

 

Memory Usage Report

===================

RAM available:1536 bytes, used:22 bytes (1.5%), free:1514 bytes (98.5%)

ROM available:32768bytes, used:343 bytes (1.1%), free:32425 bytes (98.9%)

 

Successful

File 'I:\Micromouse\main.asm' has been reloaded

Done

Clearly sensor_a[50] does not exist, but this still compiles ok. So as u can change to a byte 50 (or whatever) places down from the start byte it could potentially muck up other veriables etc. etc.

 

Reproducable.

 

On a seperate(ish) note, does char array[3] make a 3 byte array or a 4 byte array (ie 3 bytes = array[0:1:2] or 4 bytes = array[0:1:2:3]) lol, feel dumb in asking, but want to make sure i am not being dense!

 

Thanks

 

- Martyn

Share this post


Link to post
Share on other sites

pittuck,

 

Not a bug!!!! :)

 

You are now enjoying one of the great things about 'C', you don't have the overhead of bounds checking on arrays unless you add it yourself. Your code will compile under any 'C'-compiler without an error.

 

And now for the "dumb" question (remember that once we where all beginners)

char array1[ 3 ]; // uses 3 bytes, valid indexes 0 to 2

char array2[ 4 ]; // uses 4 bytes, valid indexes 0 to 3

 

Regards

Dave

Share this post


Link to post
Share on other sites
pittuck,

 

Not a bug!!!! :)

 

You are now enjoying one of the great things about 'C', you don't have the overhead of bounds checking on arrays unless you add it yourself. Your code will compile under any 'C'-compiler without an error.

 

And now for the "dumb" question (remember that once we where all beginners)

char array1[ 3 ]; // uses 3 bytes, valid indexes 0 to 2

char array2[ 4 ]; // uses 4 bytes, valid indexes 0 to 3

 

Regards

Dave

 

ok thanks,

 

lol, my being a vber c is not too alien, but has some strange traits. I know PHP quite well so base my work around what i do in that. But I suppose I should consult some C books now and then!

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...