#Code keeps crashing exe (again)

122 messages · Page 1 of 1 (latest)

slender ivy
#
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int Prev_Room, quest_end;
char choice1;
int inputtest(char test1[], char test2[]);
void quest();
void room2();

int inputtest(char test1[], char test2[]) {
    size_t test1lenght = strlen(test1);
    int check1 = 0, check2 = 0;
    for (int i = 0; i < test1lenght; i++)
    {
        if ((test2[0] == test1[i]) && (test2[1] == test1[i+1])) {
            check1 = 1;
        }
    }
    return (check1 == 1)? 1:0;
    
}

void room1() {
    int leave_room1;
    leave_room1 = 0;
    while (leave_room1 != 1) {
        printf("Your are in room 1\n[1]: Quest\n[2]: Room 2\n");
        gets(choice1);
        if (inputtest(choice1, "qu") == 1) {
            system("cls");
            quest();
        }
        else if (inputtest(choice1, "ro") == 1) {
            system("cls");
            room2();
        }
    }
}

void quest() {
    printf("Type 1 to end quest\n");
    scanf("%d", &quest_end);
    if (quest_end == 1)
    {
        system("cls");
        room1();
    }
    
}

void room2() {
    int leave_room2;
    leave_room2 = 0;
    choice1 = 0;
    while (leave_room2 != 1) {
        printf("Your are in room 2\n[1]: Room 1\n");
        scanf("%d", &choice1);
        switch (choice1)
        {
        case 1:
            system("cls");
            room1();
        }
    }
}



int main() {
    system("cls");
    room1();
    return 0;
}
thorny pythonBOT
#

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 tips on how to ask a good question use !howto ask.

glad inlet
#

choice1 is a char, which can only store a single letter. A string is an array of chars

#

Also, gets is unsafe and deprecated. use fgets instead

slender ivy
#

What’s the difference between gets and fgets

glad inlet
#

fgets takes the length of the buffer it's reading into, so it can stop and avoid a buffer overflow

thorny pythonBOT
#
char* fgets(char* str, int count, FILE* stream);
// ... and 1 more
Defined in

<stdio.h>

slender ivy
#

What’s a buffer

signal flicker
# slender ivy What’s the difference between gets and fgets

Excerpt from the fgets manpage:

Bugs
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

signal flicker
glad inlet
# slender ivy What’s a buffer overflow
char str[5]; // only has 5 spaces
for (int i = 0; i < 10; i++) {
  str[i] = 'a'; // this will keep going past the last space and write into whatever memory is after the array
}
slender ivy
#

100 bytes???? Not bits???

glad inlet
#

C only counts sizes in bytes, we don't have direct interaction with bits (outside of bitwise integer operations)

slender ivy
signal flicker
slender ivy
signal flicker
#

no, haha

glad inlet
#

why would they

signal flicker
#

Smth like

char buf[1'000'000];
```would be 1 megabyte (1 MB)
slender ivy
signal flicker
#

char buf[1'000'000'000]; would be 1 GB (although that'd be too large for your stack)

signal flicker
#

Arrays aren't created at runtime

glad inlet
slender ivy
glad inlet
#
char arr[2];
while (1) {
  arr[0] = 'a'; // infinite loop but no issues
}
slender ivy
#

Yeah I see why I sounded dumb

signal flicker
#

Just a part of your program's memory space

slender ivy
#

I see, how big is the stack? And do I ever have to worry about the issue of it being too full?

signal flicker
glad inlet
signal flicker
#

If you want more memory then you'll need to use the heap where you can get memory from using malloc

#

!man malloc

thorny pythonBOT
#

malloc, free, calloc, realloc, reallocarray - allocate and free dynamic memory

Synopsis
#include <stdlib.h>

void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

reallocarray():
    Since glibc 2.29:
        _DEFAULT_SOURCE

... (truncated)

signal flicker
#

Big advantage of malloc is not only that you have practically unlimited memory but also that you can allocate that memory at runtime e.g. from a user input

slender ivy
#

Unlimited sounds like it wouldn’t end well for me ngl

signal flicker
#

Disadvantage is that you need to manually free it and that it's a bit slower than the stack

glad inlet
#

heap isn't slower than stack

#

heap allocations are slower than stack allocations

#

access is same

signal flicker
signal flicker
#

But yeah, the whole OS part around it is what makes it slow

slender ivy
#

Man, okay time to get a degree in CS ig, screw engineering

glad inlet
#

engineering is cooler

swift lagoon
#

CS is mainly algorithms, graph and set theory, linear algebra and stuff

#

probability

slender ivy
#

Okay then, time to switch majors, screw mechatronics

swift lagoon
#

"Computer science" is kind of a bad wording, its more "Computing science"

glad inlet
slender ivy
glad inlet
#

well yeah, undergrad

#

join an engineering club and write code for them

swift lagoon
signal flicker
slender ivy
swift lagoon
#

like i made a small 2d game in C for last semesters homework assignment(2k lines of code, not too much), it uses like 30 MBs of memory with all textures and text loaded into memory after they are used once

slender ivy
#

My uni don’t got engineering clubs, mostly community

glad inlet
#

not built-in

swift lagoon
#

I used a library :)

slender ivy
#

Which one?

swift lagoon
#

always my go-to for simple stuff

slender ivy
#

Is it hard to use?

#

I’m currently attempting to make a game for my final project in C

glad inlet
slender ivy
glad inlet
#

there's only one way to learn

#

those students didn't know squat about the things they found

#

found a professor who did to advise them, learned the rest as they went

#

knowledge gets passed down each year, and while I was there we ended up setting a couple world records

slender ivy
#

I’m planning to go to a course this summer that dives deep into all engineering aspects, they told me that at the end of the course I’ll have to submit a project that’s on the level of an engineering graduation project

swift lagoon
#

i think you can just

git clone https://github.com/nevemlaci/Prog1_NHF1.git

and open it up with visual studio code and run it with the play button at the top right(i have a visual studio code task attached for running/debugging)

#

the code is horrible tho

#

but basically sdl allows you to create a window, renderer and load+render textures and stuff

#

very basic media library

slender ivy
#

I see, ill do that when i get the internet to downlaod things 👍

signal flicker
#

You only got the upload version of the internet rn?

slender ivy
#

(Internet is going at 2b/s on my laptop rn)

signal flicker
#

Huh? How can you even send and receive Discord texts

glad inlet
slender ivy
#

It takes a sec, but i send most of the messages on phone

#

Which is running on data

signal flicker
glad inlet
#

yeah

#

it's one packet per 5.6 seconds

signal flicker
#

x)

#

ouch

glad inlet
#

128 bits of data per packet

#

low-power satellite link

signal flicker
#

why?

glad inlet
#

more speed = more power

signal flicker
#

I mean you could almost carry the data by hand to Discord and it'd be faster

glad inlet
# signal flicker I mean you could almost carry the data by hand to Discord and it'd be faster

In computer networking, IP over Avian Carriers (IPoAC) is a joke proposal to carry Internet Protocol (IP) traffic by birds such as homing pigeons. IP over Avian Carriers was initially described in RFC 1149 issued by the Internet Engineering Task Force, written by David Waitzman, and released on April 1, 1990. It is one of several April Fools' ...

signal flicker
#

Is there also such a joke article for "IP over Aryan Carriers"?

glad inlet
#

not that I know of

#

but there's HTCPCP

signal flicker
#

Hyper Text Coffee Pot Control Protocol

#

nice

glad inlet
#

IETF has good april fools

signal flicker
#

@glad inlet Such a shame that you don't know German, because there's an entirely different Wikipedia dedicated to joke articles, like e.g. the "W-Lan-cable" or the "W-Lan-pipe".

#

E.g. for the "W-Lan-cable" they even give the materials it's build from:

  • 78.1% Nitrogen
  • 20.9% oxygen
  • 0.93% Argon
  • 0.03% carbon dioxide
  • 0.01% hydrogen
  • 10.98% plutonium
  • 2.718% Dilithium
  • 1.93% oxdrahdium
  • 0.002% anionic and non-ionic surfactants
  • 0.001% pro-retinol-α nanosomes (prevent wrinkling)
slender ivy
glad inlet
#

I really don't think that the earth's atmosphere is 10.98% plutonium

signal flicker
#

If this is a serious question then think about it again.
What does the W in W-Lan stnad for?

#

Yeah, up to the hydrogen it's the earth's atmosphere, starting from the plutonium we're actually surpassing the 100% barrier

glad inlet
#

the dilithium is important so we can maintain a warp field tho

signal flicker
#

true

#

wouldn't want to imagine what it'd be like without warping.

Imagine travelling by moving like a peasant

glad inlet
#

I prefer gate travel personally

fallen nebula