Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:22:52+00:00 2026-05-29T17:22:52+00:00

I have this really simple c++ function I wrote myself. It should just strip

  • 0

I have this really simple c++ function I wrote myself.
It should just strip the ‘-‘ characters out of my string.
Here’s the code

char* FastaManager::stripAlignment(char *seq, int seqLength){
    char newSeq[seqLength];
    int j=0;
    for (int i=0; i<seqLength; i++) {
        if (seq[i] != '-') {
            newSeq[j++]=seq[i];
        }
    }

    char *retSeq = (char*)malloc((--j)*sizeof(char));
    for (int i=0; i<j; i++) {
        retSeq[i]=newSeq[i];
    }
    retSeq[j+1]='\0'; //WTF it keeps reading from memory without this
    return retSeq;
}

I think that comment speaks for itself.
I don’t know why, but when I launch the program and print out the result, I get something like

'stripped_sequence''original_sequence'

However, if I try to debug the code to see if there’s anything wrong, the flows goes just right, and ends up returning the correct stripped sequence.

I tried to print out the memory of the two variables, and here are the memory readings

memory for seq: https://i.stack.imgur.com/dHI8k.png

memory for *seq: https://i.stack.imgur.com/UqVkX.png

memory for retSeq: https://i.stack.imgur.com/o9uvI.png

memory for *retSeq: https://i.stack.imgur.com/ioFsu.png

(couldn’t include links / pics because of spam filter, sorry)

This is the code I’m using to print out the strings

for (int i=0; i<atoi(argv[2]); i++) {
    char *seq;
    if (usingStructure) {
        seq = fm.generateSequenceWithStructure(structure);            
    }else{
        seq = fm.generateSequenceFromProfile();
    }
    cout<<">Sequence "<<i+1<<": "<<seq<<endl;
}

Now, I have really no idea about what’s going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:22:59+00:00Added an answer on May 29, 2026 at 5:22 pm

    This happens because you put the terminating zero of a C string outside the allocated space. You should be allocating one extra character at the end of your string copy, and adding '\0' there. Or better yet, you should use std::string.

    char *retSeq = (char*)malloc((j+1)*sizeof(char));
    for (int i=0; i<j; i++) {
        retSeq[i]=newSeq[i];
    }
    retSeq[j]='\0';
    

    it keeps reading from memory without this

    This is by design: C strings are zero-terminated. '\0' signals to string routines in C that the end of the string has been reached. The same convention holds in C++ when you work with C strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be really simple, but I have tried many solutions posted on the
This is a (hopefully) really simple question - I have been told recently that
A really simple shell script question. I have a file with something like this:
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
I have a very simple little piece of Lua code, which I wrote while
This seems like a really simple thing but I haven't figured out how to
I wrote this very simple function for my current project called insidelabel() that let's
I have a really simple PowerShell function that I use to make notes to
I have a problem with this very simple block of code. please give me
So I have this program that I really like, and it doesn't support Applescript.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.