You are not logged in.

#1 2025-05-27 20:53:49

lelouchvibritannia
Member
Registered: 2025-05-27
Posts: 2

[SOLVED] gcc compiled program broken cause of "basic_string.h"

I was participating in a contest yesterday and my program wasnt working.when I run a code compiled by gcc it shows an error and I think it has something to do with string cuz it doesnt happen when im using ll or int, here is the error:

4 1
0000
/usr/include/c++/15.1.1/bits/basic_string.h:1369: constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; reference = char&; size_type = long unsigned int]: Assertion '__pos <= size()' failed.
fish: Job 1, './pali' terminated by signal SIGABRT (Abort)

and here is the code:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n,k;
    string b;
    int score = 0;
    cin >> n >> k;
    cin >> b;
    for(int i=0;i<n;i++){
        if(b[i]==b[i-n+1]){score++;}
    } 
    if(score==k){
        cout << "YES" << "\n";
        }
    else{
        cout << "NO" << "\n";
    }
}

here is my g++ command:

 g++ code.cpp -o code -std=c++20 && ./code 

Last edited by lelouchvibritannia (2025-05-27 21:41:38)

Offline

#2 2025-05-27 21:31:03

arh
Member
Registered: 2024-10-09
Posts: 30

Re: [SOLVED] gcc compiled program broken cause of "basic_string.h"

for(int i=0;i<n;i++){
    if(b[i]==b[i-n+1]){score++;}
}

you're reading out of bounds here (let's say that n=10, the first comparison would be between b[0] and b[-9])

Offline

#3 2025-05-27 21:37:59

lelouchvibritannia
Member
Registered: 2025-05-27
Posts: 2

Re: [SOLVED] gcc compiled program broken cause of "basic_string.h"

holy I didnt notice thank you :3

Offline

#4 2025-05-27 21:38:53

arh
Member
Registered: 2024-10-09
Posts: 30

Re: [SOLVED] gcc compiled program broken cause of "basic_string.h"

no problem! :3

Offline

#5 2025-05-27 21:42:43

seth
Member
Registered: 2012-09-03
Posts: 64,153

Re: [SOLVED] gcc compiled program broken cause of "basic_string.h"

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Offline

#6 2025-05-27 23:15:02

WorMzy
Administrator
From: Scotland
Registered: 2010-06-16
Posts: 12,804
Website

Re: [SOLVED] gcc compiled program broken cause of "basic_string.h"

Mod note: moving to Programming and Scripting


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB