problem with position of my codes

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

I do not know where is the best location in this forums to post my problem so I posted here :D 

I write a function but it does not work fine, after couple of hours! I change to position of my function from line 500 to line 1000! and now every thing is fine!

 

could you please tell me why?!
 

Last Edited: Mon. Jan 5, 2015 - 07:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your function failed after 2 hours,   you have probably got a memory corruption problem.

 

I would guess that moving your function in the C file will not solve the problem.    It might take 4 hours or 24 hours.    It will still fail.

 

Typical causes are allocating char buffer[6] and writing to buffer[6].    A 6-element array goes from 0..5.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

If your function failed after 2 hours,   you have probably got a memory corruption problem.

 

I would guess that moving your function in the C file will not solve the problem.    It might take 4 hours or 24 hours.    It will still fail.

 

Typical causes are allocating char buffer[6] and writing to buffer[6].    A 6-element array goes from 0..5.

 

David.

 

Hi. thank you for your consideration.

but I had a writing bug :D in my previous post, I missed a comma in that sentence so you could not get my Purpose.

I mean after 2 hours that I think on that problem, I changed the position of my code.

in addition my function did not work when I changed position of function to previous line(500) again.!!  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seriously,   you have a memory corruption problem.

 

Look for arrays that are too small or local variables that are too big.

Look at the Memory report at the end of the Build.

 

David.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

Seriously,   you have a memory corruption problem.

 

Look for arrays that are too small or local variables that are too big.

Look at the Memory report at the end of the Build.

 

David.

 

 

what kind of problem happen, if I have a local variables that are too big?!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It sounds as if you have a fairly large file.   e.g. 1000 lines

So it is too big to copy-paste into a message.

But you can always attach the C file to a message or post a link to Github, Pastebin, ... or whichever website it is on.

 

If you have too many local variables,   the stack gets corrupted.   GCC does not tell you.

If you have too many global / static variables,   GCC tells you that you have used too much SRAM

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Attach your code, do not post it, so that we don't have to guess why your code does not work.

 

EDIT: David beat me to the punch.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Mon. Jan 5, 2015 - 08:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you think it's wise to be writing 1000+ line files in the first place. Aim for modularity and you should find that no source file need be more than a few hundred lines. Thinking in such a "modular" way will likely improve the design and stability of your code too. Aim to test the 5 or 10 functions in a few hundred line file in isolation. Problems like arrays out of bounds will hopefully show themselves at this stage (assuming you write good test harnesses) so the only other work is then to bolt together the highly tried and tested modules at the end of the integration phase.

 

I can't believe I'm about to say this but: C++ helps this process.

(if only Johan were around to hear that!).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

I can't believe I'm about to say this but: C++ helps this process.

(if only Johan were around to hear that!).

I heard it.  devil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

 

If you have too many local variables,   the stack gets corrupted.   GCC does not tell you.

If you have too many global / static variables,   GCC tells you that you have used too much SRAM

 

David.

 

so do you think that this problem happen because I used many local variable in my function ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nta_xyz wrote:
so do you think that this problem happen because I used many local variable in my function ?

How can we possibly know that?

 

You haven't attached your code for us to see, and even if you did I doubt anyone is going to look at a file with 1000 lines of code to see what the problem might be.

 

I think you need to re-structure your program as clawson suggested.  In the process of doing that you may find the problem yourself. 

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Do you think it's wise to be writing 1000+ line files in the first place. Aim for modularity and you should find that no source file need be more than a few hundred lines. Thinking in such a "modular" way will likely improve the design and stability of your code too. Aim to test the 5 or 10 functions in a few hundred line file in isolation. Problems like arrays out of bounds will hopefully show themselves at this stage (assuming you write good test harnesses) so the only other work is then to bolt together the highly tried and tested modules at the end of the integration phase.

 

I can't believe I'm about to say this but: C++ helps this process.

(if only Johan were around to hear that!).

 

thank you for guiding me. so I will separate my functions into several files and test that again, then I will post the result here.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1000 lines is a lot but not terrible.

Make sure you find the problem before you break more things.

 

If your program does strange things you have a wild pointer or a buffer over run , pretty much guaranteed.

 

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OP: do you use malloc() ? Or New ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or a stack overrun.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]