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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:21:40+00:00 2026-05-29T16:21:40+00:00

I have a vector containing integer values, which I wish to sum. However, it’s

  • 0

I have a vector containing integer values, which I wish to sum. However, it’s summing the total wrong – if the vector holds 10 ints between -10 and 10, my total should not be 18446744073709551602.

I declare the vector, and populate it with values:

std::vector<int> X;
for ( int i = 0; i < readings.size() ; ++i ) { 
    X.push_back(readings[i].x);

I then sum and display:

unsigned long temptotal = 0;
for ( int i = 0; i < readings.size() ; ++i ) {
    std::cout << "Value in X: " << X[i] << std::endl;
    temptotal += X[i];
}
std::cout << "Temp total: " << temptotal << std::endl;

which produces the following output: Temp total: 18446744073709551603.

When I step through the summation loop in GDB, (at temptotal += X[i];), I can print X[i]:

(gdb) p X[i]
$1 = (reference) @0x100100c80: -1

Printing temptotal before and after the addition is performed:

(gdb) p temptotal
$2 = 0
(gdb) p temptotal
$3 = 18446744073709551615

I’m fairly sure that I’m adding the memory locations rather than the values in the memory locations – I could be wrong on that – but I’m not sure why I’m not adding the values at X[i]. When I populate X with the values from readings[i].x, they’re doubles, which I then type-cast to ints. Is this where my error is? Checking in GDB, my type-cast of the readings[i].x value should be an int, or, at least, a number of some kind:

(gdb) p (int)readings[i].x
$1 = -1

I’m stumped; if somebody could shed some light on why I’m not adding what I think I’m adding, I’d be grateful.

EDIT
It’s the casting from int to unsigned long. I went over it and over it, pressed “submit question” and spotted my error. Although, I still don’t understand why it’s doing what it’s doing – any light shed on that would be wonderful – why, if I’m adding an int to an unsigned long, does it not take the value from the int?

  • 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-29T16:21:40+00:00Added an answer on May 29, 2026 at 4:21 pm

    This is because your temptotal is unsigned. All numbers added to it are interpreted as unsigned, hence small negative numbers become large positive numbers in two’s complement representation.

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

Sidebar

Related Questions

I have a vector containing the values 0, 1, 2 and 3. What I
I have a std::vector containing a handful of numbers, which are not in any
I have a map class, which contains a vector containing MapEntitys. MapEntity is a
I have a vector containing a time series with different values and some missing
I have some data structures: all_unordered_m is a big vector containing all the strings
I have a vector of beans that holds information I want to display in
I have created a vector containing zeros and 1's using the following command in
I have two variables, the first is 1D flow vector containing 230 data and
Given a vector of datetime values, I needed to create a data.frame containing datetimes
I have a data.frame called mydata and a vector ids containing indices of 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.