1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Segmentation Fault?

Discussion in 'Software Development' started by Davidov541, Feb 27, 2010.

Thread Status:
Not open for further replies.
  1. Davidov541

    Davidov541 Thread Starter

    Joined:
    Apr 20, 2004
    Messages:
    76
    I am trying to build an Assembler in C++ atm, but when I return from a function (itoa), I get a seg. fault before I can print anything out. The output makes it obvious that the issue is between the end of the function and the next line of code in the calling function. I have tried returning std::string and std::string* instead of doing parameters by reference, but nothing has worked. I have no clue where it is coming from, so any help on where to look would be great. Below is the relevant code and output:

    void Assembler::assembleCode(std::string assembly) {
    ...
    std::string imm;
    std::cout << "14\n";
    itoa(labelPositions[tokens[3]], 16, imm);
    std::cout << "13\n";
    ...
    }

    void Assembler::itoa(int value, int len, std::string& imm) {
    ...
    imm = std::string(binresult.substr(30 - len, len));
    std::cout << "Translated to " << imm << std::endl;
    }

    Output:
    ....
    14
    ....
    Translated to 0000000000011111
    Segmentation Fault
     
  2. JimmySeal

    JimmySeal

    Joined:
    Sep 25, 2007
    Messages:
    290
    It's been a long time since I used C++, and I'm not sure, but I find it doubtful that the program would segfault from just exiting that function. More likely, the error is occurring a few lines later, while "13" is still in the output buffer and just hasn't been displayed yet.

    What are the next few lines of your code?
     
  3. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi Davidov541,

    Check the parameters you are passing into the call in terms that you specify your parameter. You can choose one of two ways to approach the task:
    1) use a symbolic debugger that your program has been compiled to produce the symbolic debug information with via compilation options - much easier to view values in memory and know immediately whether or not they have been properly initialized.
    2) introduce debugging code into your program via a conditional like if DEBUG and try to print out the values of the parameters before you make the call to the subprogram.

    The notion is to verify your parameters prior to the call, and maybe its a good idea to wrap an assertions statement around them prior to the call which could possibly tell you which parameter value is at fault.

    -- Tom
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/906397

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice