x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

C++ For Loop crash [4.6.1]

I don't get any errors in my code when compiling but on (Play) it crashes

Here's my code -

 for(int32 CheckNum = 0; CheckNum <= 12; CheckNum++)
 {
         if(Inventory[CheckNum]->ItemNum == 4)    //<--- this is the part that crashes???
         {
         }
 }

This is the crash message i get but i don't know how to fix it because it should work?

this

Product Version: Not Selected
Tags:
crashimage.png (15.1 kB)
more ▼

asked Jan 19 '15 at 04:55 PM in Bug Reports

avatar image

AndrewM47
257 67 60 77

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Hi!

I'm assuming your "Inventory" array does not have 12 items in it. It has less. Say the size of the array is only three, that means you can only access those three members:

  1. Inventory[0] -> OK

  2. Inventory[1] -> OK

  3. Inventory[2] -> OK

  4. Inventory[3] -> BAD :(

Once you start accessing items past the end of the array, you're accessing memory that is not part of that array (memory that doesn't have an inventory item stored in it). The crash assert you provided is guarding again you doing it. It's making sure the index (CheckNum) is within the bounds of your array.

To keep from using an invalid index, you probably want your for loop to look like this:

 for(int32 CheckNum = 0; CheckNum < Inventory.Num(); CheckNum++)

Using Inventory.Num() instead of 12, ensures that all the values of CheckNum are within the range of your array.

Hopes this makes sense!

more ▼

answered Jan 19 '15 at 05:13 PM

avatar image

User-1420270633 STAFF
948 27 26 58

avatar image AndrewM47 Jan 19 '15 at 07:37 PM

I just used "12" as demonstration purposes on my original code i do have Inventory.Num() but the crash still occurs, it crashes when i use the "CheckNum" inside of the inventory param's? why?

 Inventory[CheckNum]

avatar image Jamie Dale STAFF Jan 19 '15 at 08:58 PM

Are you really using <= rather than <?

<= will cause it to try and access index 1, rather than stopping when it gets to index 1.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question