Results 1 to 4 of 4

Thread: C++ problem: Segmentation fault (core dumped)

  1. #1
    Join Date
    Jul 2013
    Beans
    4

    Post C++ problem: Segmentation fault (core dumped)

    ok i've been working on this program that takes from a string the part that was bracketed. and i've got this output when i compiled it(without any warnings or errors)
    here is the code:
    Code:
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    void formula_devider(char input[128]){
            char bracket_formula[128];
            int rmb_brk[2];
            int brk,n;
            bool mark = false;
            for(int i = 128; i < 0; i--){
                    if(!mark){
                            if(input[i] == ')' ){    //scan for brackets
                            rmb_brk[1] == i;
                            mark = true;
                            }
                    }
                    if(input[i] == '('){
                            rmb_brk[0] == i;
                            }
                    }
            n = rmb_brk[1] - rmb_brk[0];
            for(int j = 0; j > n; j++){
                    bracket_formula[j] = input[rmb_brk[0] + j];
                    }
            puts(bracket_formula);
            }
    
    int main(){
            puts("TEST! TEST! TEST! TEST!");
            char input[128];
            formula_devider(input);
            }
    Last edited by Pghg022; July 15th, 2013 at 03:15 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Magic City of the Plains
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: C++ problem: Segmentation fault (core dumped)

    Moved to Programming Talk.

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: C++ problem: Segmentation fault (core dumped)

    Looks like mostly C to me.

    What do you expect to get in 'input" in main?

  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: C++ problem: Segmentation fault (core dumped)

    The conditional of a 'for' loop is a "while" condition, not an "until" condition. Neither for loop is executed because both your conditionals are always false, so rmb_brk's contents are uninitialized when you try to use them, which leads to undefined behavior.

    You may have other bugs, but fix those for loops first.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •