Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello Every one,

I have created an array but when i am getting an out of bound array access error please can you help me to avoid it.

Much awaited for reply.

Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 1-Feb-16 15:49pm    
Don't access element with the indices which are beyond the range 0 to length - 1. :-)
—SA

Since you didn't show any code the answer can only be a general one: Stay inside the bounds of the array. Apart from accessing at some wildly out of bounds position, one particular case is more common: If you have an array with a size of 10 then 10 isn't a valid index. Because arrays in C++ are 0-based. So in this case, valid indices are 0 to 9.
 
Share this answer
 
Comments
Mustafa_ub2016 1-Feb-16 15:56pm    
Thank you for your reply. Appreciate your time.
Sascha Lefèvre 1-Feb-16 16:04pm    
You're welcome!
You must not go out of bounds, the C/C++ developer has to be precise. That said, you could use std::vector instead of a plain array: it provides the std::vector::at - cppreference.com[^] method that throws an exception if you try to make an out-of-bounds access.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-16 15:50pm    
5ed.
—SA
CPallini 1-Feb-16 16:08pm    
Thank you.
Mustafa_ub2016 1-Feb-16 15:56pm    
Thank you for your reply. Appreciate your time.
CPallini 1-Feb-16 16:09pm    
You are welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900