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

  • Home
  • SEARCH
  • 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 7785567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:16:36+00:00 2026-06-01T20:16:36+00:00

I copied some code that simply reads a file to a string and prints

  • 0

I copied some code that simply reads a file to a string and prints the string from an older program. It was working fine, so I decided to modify it a bit. The new program is

#include <stdio.h>
#include <string.h>

int main() {
    FILE *itemlist = fopen("itemlist", "r");
    char *currentstring, charbuffer[2];
    // char itemstart = 0;
    while (fgets(charbuffer, 2, itemlist)) {
        strcat(currentstring, charbuffer);
    }
    printf("%s", currentstring);
    return 0; 
}

And it works as expected. But when I uncomment the itemstart line, it gives a segmentation fault. I’m not even using it and as far as I’m concerned, initializing an char to 0 is not illegal. I thought it was an issue with types, then I changed it to a short and then to int and it was still giving a segfault.

But then I removed the = 0 part and it worked again. Then I decided to put it back, debug the binary with gdb, and the segfault was at strcat.

How is this possible?

  • 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-01T20:16:37+00:00Added an answer on June 1, 2026 at 8:16 pm

    currentstring is a dangling pointer, so strcat(currentstring, charbuffer); results in undefined behavior.

    Probably uncommenting char itemstart = 0 initializes some memory to 0 and the access violation is made visible, however this is just a guess. Undefined behavior means anything can happen.

    You should allocate memory for currentstring:

    currentstring = malloc(10); //or whatever length you need
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some doc file, that when copied and pasted into a text
I have copied some files across from one project to another. The code simply
Background We've been using some code copied verbatim from Joe Duffy's Concurrent Programming on
some code snippets. The java coding doing the jaxb unmarshaling. pretty straightforward, copied out
I have copied some files from project A to project B, both of which
I am processing some CSV file which i have copied in Bin folder of
In some old Java code, I found a class that contains a lot of
I'm updating some old code to grab some binary data from a URL instead
I coded some calculation stuff (I copied below a really simplifed example of what
I have some 3rd party libraries and includes (I have copied them to the

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.