#for loop crashes program

249 messages · Page 1 of 1 (latest)

peak quiver
#

I added this piece of code to my CPP program and for some reason, it crashes

           const double step = 1/150;
            vector<int> layerSizes{2,3,2};
            Network network{layerSizes, 0.0000001,0.1};
            for(int i = step/2; i<1; i++){
                for(int j = step/2; j<1; j++){
                    vector<int> output = network.process({i,j}); 
                    if(output[0]> output[1]){
                        DrawRectangle(i*screenWidth, j*screenHeight,step*screenWidth, step*screenHeight, Color{84, 165, 224, 128});
                    }else{
                        DrawRectangle(i*screenWidth, j*screenHeight,step*screenWidth, step*screenHeight, Color{254, 78, 90, 128});
                    }
                }
            }

my program and gives out this error:

if I remove the for loop it works perfectly fine, does anyone know why?

carmine pantherBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

limpid crystal
#

1073741819 is access violation

#

so you're probably accessing some memory that isn't yours

#

like an out of bounds array index

pure charm
#

your i and j look like they will always be 0 initialized, and run once

limpid crystal
#

hard to tell, there's a lot going on we can't see

pure charm
#

are you trying to go 150 times, one for each 1/150 step?

pure charm
#

and yeah, posting a minimally reproducible example would help debug

#

then you can't use i++

#

that adds 1

#

and your int i needs to be double i

peak quiver
#

oh

pure charm
#

for(double i = step/2.0; i<1.0; i+=step){

#

if I had to guess

#

your access error is output[0]> output[1])

#

make sure whatever network.process is, it's well defined

#

(and if it's always gonna be 2 numbers, might as well make it a pair instead, or better yet a named data struct)

#

also your step is always 0, you need to initialize as const double step = 1.0 / 150.0;

#

always use .0 or .0f for doubles and float literals to avoid stuff like this, and be more expressive

peak quiver
#

oh okay

peak quiver
#

do you know why the error is there?

pure charm
#

No I don’t, you didn’t provide your whole program

#

I said it was a guess

peak quiver
#

oh

#

wait let me show u the whole program(its kinda complicated)

#

the for loop is in the main function

#

@pure charm do u see the problem?

#

also

#

when I remove the for loop

#

everything works out perfectly

pure charm
#

do you know how to use a debugger

#

it'll take you to the exact line where there's a fault

peak quiver
pure charm
#

and you can inspect all your variables

#

then it's a good time to learn

peak quiver
pure charm
#

what compiler

peak quiver
peak quiver
#

I use mingw32-make file

pure charm
#

does the debug button work

peak quiver
#

nope

#

I get this

#

which one do you think i should choose?

pure charm
#

last one

peak quiver
#

oh okay

#

😦

pure charm
#

yeah open it

#

need to configure it

peak quiver
#

I clicked windows

pure charm
#
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "foo",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/foo.exe",
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    },
  ]
}
#

try this

#

replace your debugger path

#

and replace with your program name

peak quiver
#

do i need to type in main.cpp file?

#

or executable file?

pure charm
#

should be whatever .exe you compiled

peak quiver
#

wha tif

#

U cant compile

#

since

#

the error

pure charm
#

oh

#

misread your original question

#

thought it was runtime error

peak quiver
#

it isnt

#

so me having problems compiling

pure charm
#

can you paste the make in text

peak quiver
#

okay

#

its kinda long tho

#

I got it from github

pure charm
#

just what is ran then

peak quiver
peak quiver
pure charm
peak quiver
#

I will brb

#

in like 15 min

#

something came up

pure charm
#

but to answer previous question, your output is app but should be app.exe

#

btw this is what debugger shows

#

you need to reserve bias with some size, or use push_back to add the elements

pure charm
#

where?

peak quiver
#

in layers constructor

pure charm
#

you need .resize not .reserve

#

reserve make sure there's enough memory but elements outside of currently accessible elements can't be accessed yet

peak quiver
#

oh okay

#

I made that change

#

it still doesnt fix that error

pure charm
#

(also it is runtime error, your makefile is calling the executable)

#

so use the debugger now

#

and try to figure it out

#

correct app to app.exe, compile, and run it

peak quiver
pure charm
#

also you need the -g flag

peak quiver
#

where?

pure charm
#

did you change app to app.exe my man

#

in your makefile

peak quiver
#

ye

pure charm
#

add the -g flag to the compiling step

peak quiver
#

compiling step?

#

do you mean in the make file?

pure charm
#

yes

peak quiver
#

I think we are getting closer

#

but

#

another error

pure charm
#

is your program running

peak quiver
#

oh okay

#

wait

#

but now

#

😭

#

its this

#

with a loading cursor

pure charm
#

probably infinite loop

#

use the debugger

#

and just click pause

#

it'll show you what it's doing

#

so then step through the program line by line to see what's happening

peak quiver
pure charm
#

what do you see

peak quiver
pure charm
#

dude

#

it's the same thing 4 times now

#

show me your updated makefile where you changed it to .exe

peak quiver
pure charm
#

the makefile

peak quiver
#

app.exe

pure charm
#

this config is just looking for that exe

#

but it's not there

peak quiver
pure charm
#

because you're not creating it

peak quiver
#

in bin

limpid crystal
#

you'd probably have much less pain if you just use Visual Studio proper

#

meaning, not VSCode, they are not the same

#

Visual Studio Community edition is free

pure charm
#

can you actually run the exe itself from commandline?

#

wait what is your command to run it

peak quiver
pure charm
#

Run the program itself

#

./bin/app.exe

peak quiver
#

yeah

#

I did that

#

already

peak quiver
#

it does this

#

(I use raylib for graphics lib)

pure charm
#

Whelp not sure why debugger not working then

#

Since you can run it, should be able to run it with debugger

peak quiver
#

wait

#

I have an idea

#

I will send the project file to you

#

and try running it using your debugger

pure charm
#

I did

limpid crystal
#

I mean I have a cheeky yet simple explanation

pure charm
#

I sent screenshots

peak quiver
pure charm
#

“You’re not using VS”

limpid crystal
#

you don’t really know what you’re doing, which is fine

#

but yeah you should probably just use VS

#

that or invest the time to learn this stuff

peak quiver
#

oh

limpid crystal
pure charm
#

Actually I guess there’s a chance that it’s actually not a well formed program

#

And that windows opens a window but can’t run it

limpid crystal
#

though hax seems to have infinite patience

#

which is admirable

peak quiver
pure charm
#

Just use a simpler compilation , g++ app.cpp -g -o app.exe

peak quiver
peak quiver
#

ykw hax....

pure charm
#

The command

#

Stop using make

#

Run the command

peak quiver
#

i need to include

#

raylib-cpp

#

which is in the include folder

pure charm
#

Yeah, add the stuff from your main makefile little by little

#

And understand each action

#

You have a ton of stuff

peak quiver
#

I will have to go to bed to sleep now

#

its like 9pm for me

limpid crystal
#

you really need to know and understand quite a lot to use VSCode effectively

#

like understanding each compiler flag and what it does and how to use them

#

with Visual Studio you just click stuff and the GUI mostly guides you so you don’t need to know all that

#

VS is not zero effort but it feels to me that you don’t have the requisite knowledge and background to use VSCode effectively

peak quiver
#

no not really

#

I only ever used vscode, for...

limpid crystal
#

and while not zero effort, VS is pretty easy

peak quiver
#

well web dev

#

which is simple

#

for other langs I used diff editors

#

like intellij

#

Rider

#

and neovim

limpid crystal
#

yea C++ is very much less easy than using node or Python or ruby

#

or idk what you do web dev with

#

download the Community Edition

peak quiver
#

okay

limpid crystal
#

I must re emphasize that VS is not VSCode

peak quiver
#

vs is visual studio

#

which is

#

kinda not open source

#

vscode

limpid crystal
#

despite MS clearly wanting to believe they are the same

peak quiver
#

is visual studio code

peak quiver
#

just have a realy bad exp with it

limpid crystal
#

okay well I mean, VS is free

#

might not be open source but it is free

limpid crystal
peak quiver
#

c#

#

unity

limpid crystal
#

for C++ on windows there is near unanimous agreement that VS is one of the best IDEs

#

going the plain text editor route is fine but like I said, that requires quite a lot of “know what you’re doing already “

#

admittedly VS isn’t perfect

#

but you have two basic choices right now

#

1 learn all the things

#

2 use Visual Studio instead

peak quiver
#

im gonna choose 2

#

I am gonna go to bed now

#

since its pretty late for me (9pm)

#

and I have school tomo

limpid crystal
#

good night 🌙

peak quiver
#

night!

carmine pantherBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.