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 8045173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:27:41+00:00 2026-06-05T05:27:41+00:00

I am having trouble trying to figure how why my g++ compiled program seg

  • 0

I am having trouble trying to figure how why my g++ compiled program seg faults at a strncat() call.

I’ve been jumping around this site and general Googling and found a number of similar problems but haven’t found any solutions that work for me. This is part of a much larger code and there’s only so much I can do to redefine variables as the code isn’t mine.

All this section of the code is meant to do is read in the last line of a file, remove the relevant data and concatenate to a char*

When I run it, I get a segmentation fault at the line with strncat(RAM,nextchar,1)

char line[256]={"0"};
char nextchar[10]={"0"};
int length=0;
double rac;
double decc;
bool SPACE=false;
char * RAM="RA: ";
char * DECM="DEC: ";
if(AutoColData.good()){
    while(!AutoColData.eof()) AutoColData.getline(line,256);
    for(int i=0;i<strlen(line);i++){
        nextchar[0]=line[i];
        cout<<line[i];
        if(isspace(nextchar[0])&& !SPACE)   SPACE=!SPACE;
        else if(SPACE && !isspace(nextchar[0])){
            SPACE=!SPACE;
            length++;
        }
        if(length==6) {
          cout<<"\n"<<RAM<<nextchar<<"\n";
          strncat(RAM,nextchar,1);
        }
        else if(length==7) strcat(DECM,nextchar);
    }
}

There are some sloppy choices here, I know (the whole “SPACE” thing is messy). But I don’t see any reason for this to Seg Fault. It runs fine up until the line with strncat(). The cout works fine, both strings print and have the right data in them but then strncat fails. I’ve tried using malloc(), strings and nothing seems to work. If anyone could point out what stupid thing I’m doing, it would be very helpful. Thank you.

  • 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-06-05T05:27:43+00:00Added an answer on June 5, 2026 at 5:27 am

    RAM is wrongly declared as a char *, when it really should be a const char *: String literals are read-only, and you are not allowed to write to them. Your strncat call is simply undefined behaviour.

    If you want a writable string, you could make a large enough char array: char RAM[100] = "RA: "; (this will be zero-padded at the back), or better even, just use std::string:

    std::string RAM = "RA: ";
    RAM += nextchar;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I been having trouble trying to figure this out. When I think I have
I'm having a bit of a trouble trying to figure this out today, i
Hey im new to database design and having trouble trying to figure this one
I'm having trouble trying to figure out how to achieve this programming challenge in
having some trouble trying to figure this out, complete novice when it come to
I am having some trouble trying to figure this out. What i have is
This query works fine but I'm having trouble trying to figure out why it
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
I'm having trouble trying to figure out why my formatDate is not working correctly.

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.