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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:33:37+00:00 2026-05-31T04:33:37+00:00

I ran into unexpected behavior with a function I wrote to compute the average

  • 0

I ran into unexpected behavior with a function I wrote to compute the average point for a set of 3D points.

static inline Point3d
average_vertex_position( Vertex vertices[], int nPoints )
{
  Point3d average = { 0.0, 0.0, 0.0 }; // Must be there to avoid accumulation
  Point3d point;
  int i;

  for ( i = 0; i < nPoints; i++ )
  {
    point = vertices[i].position;
    average.x += point.x;
    average.y += point.y;
    average.z += point.z;
  }

  average.x /= (double) nPoints;
  average.y /= (double) nPoints;
  average.z /= (double) nPoints;

  return average;
}



// ...
Point3d average;
// ...
for ( j = i; j < nVertices; j++ )
{
  // ...
  average = average_vertex_position( vertices, nVertices );
  // ...
}

For each iteration the return value of average_vertex_position would accumulate unless I added explicitly the initialization Point3d average = { 0.0, 0.0, 0.0 };.

If the previous return value was Point3d( 10, 0, 20 ) and the next run should have returned Point3d( 20, 10, 0 ) it would instead return an accumulated result Point3d( 30, 10, 20 ).

Originally I had just Point3d average; assuming all the member values (double values) would be initialized to 0.0. I also assumed average would be in this initial state between each call. I don’t understand why I need to explicitly initialize it?

I trimmed out the code I didn’t think was relevant – but I could be wrong, in which case I’ll update if it doesn’t hold enough information.

  • 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-31T04:33:38+00:00Added an answer on May 31, 2026 at 4:33 am

    That is normal with auto variables – they are not initialized to 0 if not explicitly stated.

    The fact that the old values are reused is pure coincidence, as you don’t have any other function calls in-between. If you would have them, the memory at the given stack position would be overwritten and your values were different.

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

Sidebar

Related Questions

While creating a map of String to partial functions I ran into unexpected behavior.
I just ran into some unexpected behavior with DateTime.UtcNow while doing some unit tests.
I ran into an interesting behavior recently. It seems that if I override .equals()
I recently endeavoured to learn about multiple threading, and ran into the following unexpected
I ran into what was to me an unexpected result when testing a simple
Ran into the ReferenceTable limit in my Android jni function. Cleaned it up with
I've been playing around with Backbone.js and ran into something a bit unexpected. It
I've just ran into an unexpected behaviour for Entity framework entities in use with
I ran into an unexpected result in round-tripping Int32.MaxValue into a System.Single : Int32
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX

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.