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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:28:04+00:00 2026-05-11T20:28:04+00:00

As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements

  • 0

As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements of “here1,” “here2,” and so on at different points in code to figure out when my programs are going awry. This brute force debugging technique has saved me many, many times throughout my CS studies. However, when I started programming in C, I stumbled onto an interesting problem. If I were to try and run

void* test;

printf("hello world");
test[5] = 234;

Of course I get a segfault for not malloc’ing memory for testChar. However, you would think logically that “hello world” would be printed before the seg fault happens, since that is the flow of the code, but in my experience, it is always the case that the seg fault happens first, and “hello world” is never printed to the console at all. (I wasn’t able to test this exact example, but I have run into this sort of situation many times using gcc on a linux box.) I’m guessing this has to do with either the compiler rearranging some things and/or printf using some sort of buffer that is flushed asynchronously and therefore not being immediate. This is entirely speculation on my part because I honestly don’t know why it happens. In any other language that I have used, no matter what problem the “testChar =…” line caused, the “hello world” would still be printed, and thus I could determine where the problem is.

My question is why does this happen when I’m programming C? Why isn’t the hello world printed first? And secondly, is there a better C programming debugging technique than this that accomplishes the same basic thing? As in, an easy/intuitive way to find the line of code that is a problem?

Edit: I gave a working example by accident haha. What I have now should cause a segfault. It’s funny how usually when I don’t want a segfault I get one, and now when I actually wanted one I wrote legal code!

  • 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-11T20:28:04+00:00Added an answer on May 11, 2026 at 8:28 pm

    The code you posted is perfectly legal and should not cause a segfault – there is no need to malloc anything. Your problem must lie somewhere else – please post the smallest example of code that causes the problem.

    Edit: You have now edited the code to have a totally different meaning. Still, the reason that “hello world” is not displayed is that the the output buffer has not been flushed. Try addinig

    fflush( stdout );
    

    after the printf.

    Regarding locating the source of the problem you have a couple of choices:

    • liberally sprinkle printfs through your code using the __FILE__ and __LINE__ C macros
    • learn to use your debugger – if your platform supports core dumps, you can use the core image to find where the error is.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many beginning programmers write code like this: sub copy_file ($$) { my $from =
In my admittedly young career I've found myself writing code to support quirky business
Many times I have seen Visual Studio solutions which have multiple projects that share
Many times we find ourselves working on a problem, only to figure out the
Many frameworks seek to abstract away from HTML (custom tags, JSFs component system) in
Many applications have grids that display data from a database table one page at
Many people use Mock Objects when they are writing unit tests. What is a
Many of us have used Perfmon tool to do performance analysis. Especially with .Net
Many websites, including this one, add what are apparently called slugs - descriptive but
Many of the parameters for interacting with the Office Object model in VSTO require

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.