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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:36:51+00:00 2026-05-28T14:36:51+00:00

I have a loop that looks something like this while(condition){ read_some_data(source, buf, BUFSIZE); printf(buf);

  • 0

I have a loop that looks something like this

while(condition){
  read_some_data(source, buf, BUFSIZE);
  printf(buf);
  memset(buf, 0, BUFSIZE+1);
  //do stuff to affect condition that does not touch buf
}

buf is an char array of size BUFSIZE+1. The strange thing is that if I comment out the printf, the program executes perfectly with no segmentation fault. It is only when I try to print out buf that I get the problem. Also, the seg fault does not necessarily happen on the first iteration of the loop. It usually takes 6 or 7 iterations.

Also, there is no dynamic memory allocation in this program.

  • 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-28T14:36:52+00:00Added an answer on May 28, 2026 at 2:36 pm
    • Make sure that buf has a null (zero-valued) byte somewhere within the allocated range. That null byte is how printf can tell that it’s reached the end of a string; without it, it will keep reading past where it can safely do so.
    • Make sure that buf doesn’t contain anything like %d that printf might take to indicate additional arguments. Better yet — just use printf("%s", buf), which completely eliminates any such risk.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple server that looks something like this: void *run_thread(void *arg) {
I have something that looks like this: my $report = new ReportGenerator; #custom object
I frequently have code that looks something like this: if (itm != null) {
I'm working on a graphical application which looks something like this: while (Simulator.simulating) {
I have an xml formatted document that looks like this: <?xml version=1.0 encoding=windows-1250?> <
If I have some code that looks something like: typedef struct { bool some_flag;
I have a file that looks like this: A B C D E 0
I have a for loop that loops over one array... for i=1:length(myArray) In this
I have a loop that runs through a variety of websites and I'd like
Lets say I have a table containing several hundred million rows that looks something

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.