# Problem with arrays (turning them zero)

Status
Not open for further replies.

#### kappa_am

##### Full Member level 6
Hi all;
I am working on a program using mikroc and dspic 30f4011.
I have problem with arrays. they keep values from past cycles. I like to turn them zero but I don't like to use for loop and turn each element to zero individually. is there any better way?
How about defining them in the loop?

Thanks

#### doraemon

##### Super Moderator
Staff member
Hello!

It depends on the memset implementation, but it may use a byte to byte zero filling.
If you want to zero an array, you may consider a dma. It is ususally possible to dma
without incrementing source or destination.

- Increment source and destination yields an array copy
- Increment destination only: copy a constant to the whole array (e.g. 0).
- Increment source only: send an array to serial port / ethernet / etc...
- You could also consider no increment at all: send to serial B all bytes coming from serial A.

Dora.

#### kappa_am

##### Full Member level 6
thank you all.
I tried memset, but it's very slow.
I do not understand using dma. it beats me. I would be grateful if you explain more and with a example.
another question is why the variables in defined arrays have value, while I have not assign any value to them before (dsPIC MCU)? the program is running well on PC but on MCU variable has strange values.:bang:

#### horace1

##### Advanced Member level 5
no idea why memset should be slow - library functions are generally implemented using efficent code

it would be used so to initialise an array to 0
Code:
int x[500];
memset (x, 0, sizeof(x));

when static variables are created (i.e. defined outside function code) the values are initialised to 0

when variables are defined inside functions by default no initialisation is performed (you get whatever values happen to be in the memory allocated)

to initialise an array inside a function to 0 you can define it so (remember the array is allocated on every function call and deallocated on exit)
Code:
int x[500]={0};

#### kappa_am

##### Full Member level 6
Thank you for your response.
I have defined the variables outside the function but I it has value!!
the memset is faster than a for loop in my case about 2us but it takes 3.6us that is rather high for a vector.

Code:
int x[500]={0};
this is suitable for C++; when I tried to use it in mikroc I faced with error. I think the mikroc is based on C.
no idea why memset should be slow - library functions are generally implemented using efficent code

it would be used so to initialise an array to 0
Code:
int x[500];
memset (x, 0, sizeof(x));

when static variables are created (i.e. defined outside function code) the values are initialised to 0

when variables are defined inside functions by default no initialisation is performed (you get whatever values happen to be in the memory allocated)

to initialise an array inside a function to 0 you can define it so (remember the array is allocated on every function call and deallocated on exit)
Code:
int x[500]={0};

#### horace1

##### Advanced Member level 5
Thank you for your response.
I have defined the variables outside the function but I it has value!!
the memset is faster than a for loop in my case about 2us but it takes 3.6us that is rather high for a vector.

Code:
int x[500]={0};
this is suitable for C++; when I tried to use it in mikroc I faced with error. I think the mikroc is based on C.
initialisation of static arrays should work with C, however
1. an array size of 500 may be too large for microc
2. microc may not have implemented ths feature of C

I don't use microc - perhaps other contributers can help

hat was the error message displayed?

#### kappa_am

##### Full Member level 6
The error is "invalid expression"
it's ok I increased the clock frequency and have used memset

Thank you

Status
Not open for further replies.