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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:09:22+00:00 2026-05-13T16:09:22+00:00

I have come across a very odd problem in C that I have never

  • 0

I have come across a very odd problem in C that I have never encountered before. I have narrowed it down to the following very simple snippet.

The variables are global and of type:

    int cpd;
    int nPart;

And here is the relevant code snippet which I gradually stripped down to the very minimum needed to produce the issue:

    printf("\ncpd1: %d\n",cpd);

    int p;
    for(p=1;p<=nPart;p++)
    {
        printf("\ncpd2: %d\n",cpd); exit(0);
    }

…The output I get is this:

cpd1: 17

cpd2: 0

How on earth is this possible?!
cpd has NOT been reassigned, NO functions have been called… yet it changed?
HOW?!?!

This has been driving me slowly insane for quite some time now…
… so any ideas?

thanks for your time,
Ben.

EDIT: and when I remove -02 from the makefile arguments to gcc, BOTH the print statements tell me that cpd = 0!

EDIT: Okay, I just found that a variable that is declared globally once, initialised as 4.0, and then never modified is now apparently 1.51086e-311 … Something is very wrong somewhere…

EDIT: SOLVED!: I had an array of size 1000 that needed to be over 4000, and trying to write to this was corrupting the memory around it. Thing is, this array is NOT accessed anywhere near those print statements, it is accessed in the same function however, much earlier on (large function!). The weird discrepancy between print statements must be some weird artifact of using -O2, as without -O2, both prints of cpd print the corrupted version. Thank you everyone, I wouldnt have worked this out without your help!

  • 1 1 Answer
  • 1 View
  • 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-13T16:09:23+00:00Added an answer on May 13, 2026 at 4:09 pm

    Stack frame corruption due to buffer overflow is the usual explanation for this. Here’s an example:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      int cpd;
      char msg[4];
      cpd = 17;
      printf("%d\n", cpd);
      strcpy(msg, "Oops");
      printf("%d\n", cpd);
      return 0;
    }
    

    Output:

    17
    0
    

    The “msg” string buffer is too short by one character, the string terminator overwrites the value of “cpd”.

    The best way to find the cause is to use the data breakpoint feature of the debugger. Set a regular breakpoint on the function entry point. Then find the address of the “cpd” variable and a set a byte-size data breakpoint on it. The debugger will stop as soon as the cpd value changes.

    Beware that this won’t necessarily work in optimized code, the “cpd” value might be stored in a register. Which is another possible explanation why its value is different in separate statements.

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

Sidebar

Related Questions

I have recently come across a very simple Typed DataTable (without using a .XSD)(I've
Sometimes I come across this problem where you have a set of functions that
I've been writing PHP web applications for some time, and have come across very
I have come across a lot of optimization tips which say that you should
I have come across the following type of code many a times, and I
I have come across what must be a common problem. When I have an
I am a newbie to Python and have come across the following example in
I have come across a very weird error. I'm on Solaris 10, using Ruby
I have come across a very strange issue in python. (Using python 2.4.x) In
I have come across what appears to be very peculiar behaviour using entity framework

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.