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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:37:15+00:00 2026-06-15T01:37:15+00:00

I am utterly mystified by this error, and I hope you can help me!

  • 0

I am utterly mystified by this error, and I hope you can help me!
I have a class which has a vector of structures (the structure is called particle). I am simply writing a getter which returns a vector of a certain field in the structure; once for a field called x, and then again for a field called v:

vector<double> x = s.getx();
vector<double> v = s.getv();

For some reason, getx works, but getv does not: It gives me “malloc(): memory corruption:”.

Here are the classes getx and getv:

vector<double> getx() {

    vector<double> ret(number-2);

    for(int i = 1; i < number; i++) {

        ret[i-1] = masses[i].x;

    }

    return ret;

}

vector<double> getv() {

    vector<double> ret(number-2);

    for(int i = 1; i < number; i++) {

        ret[i-1] = masses[i].v;

    }

    return ret;

}

The error occurs on this line:

vector<double> ret(number-2);

in getv.

The reason I am so confused is that both these methods are essentially identical! I even get the same error when I replace (number-2) with a constant, e.g. 4. It simply can’t allocate the memory!

I hope you can see what I’ve done wrong…

Thank you very much for your help,
Best wishes,
noctilux

  • 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-06-15T01:37:16+00:00Added an answer on June 15, 2026 at 1:37 am

    Memory corruption errors rarely show up at the point where they occur; they show up later, when something hits the corrupted memory. In this case, both functions are writing to memory that doesn’t belong to them. Each one initializes a vector with number - 2 elements, then writes to vector[number - 1], which doesn’t exist.

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

Sidebar

Related Questions

This one has me utterly baffled... I have a very simple input model, with
Could use some help. I am utterly stuck, been trying to fix this one
My brain must be utterly broken, because I can't figure out why this isn't
I have encountered a strange linker problem that has me utterly perplexed : I
I have just written what has to be considered utterly hideous code to count
I'm utterly bamboozled by an app I'm trying to write which has suddenly stopped
location.hash seems to be utterly broken in Safari 4: It can be set initially,
I've literally been trying to debug this for a full day and it's utterly
I'm utterly new to APS.NET and in particular the MVC model...! I have a
There has to be something utterly simple I'm overlooking here, but for the life

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.