Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with arrays (turning them zero)

Status
Not open for further replies.

kappa_am

Full Member level 6
Joined
Jul 16, 2012
Messages
326
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,807
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
Joined
Jun 21, 2009
Messages
1,068
Helped
273
Reputation
554
Reaction score
248
Trophy points
1,343
Location
Japan
Activity points
10,597
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
Joined
Jul 16, 2012
Messages
326
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,807
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
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
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
Joined
Jul 16, 2012
Messages
326
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,807
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
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
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
Joined
Jul 16, 2012
Messages
326
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,807
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top