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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:06:21+00:00 2026-06-18T20:06:21+00:00

I haven’t used C++ in quite a while and I seem to be making

  • 0

I haven’t used C++ in quite a while and I seem to be making what I’m sure is a very stupid mistake. Could someone tell me why

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
        double* atoms;

        atoms = (double*)malloc(10 * 3*sizeof(double));
        for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 3; j++) {
                        atoms[i*10 + j] = 2.0;
                }
        }

        for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 3; j++) {
                        cout << atoms[i*10 + j] << endl;
                }
                cout << endl;
        }


        free(atoms);

        return 0;
}

is printing

2
2
2

2
2
2

2
2
2

6.94528e-310
6.94528e-310
0

0
4.24399e-314
4.24399e-314

2
2
2

2
2
2

2
2
2

2
2
2

2
2
2

instead of all 2’s? Thanks

  • 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-18T20:06:22+00:00Added an answer on June 18, 2026 at 8:06 pm

    malloc(10 * 3*sizeof(double)) allocates enough memory for 30 doubles.

    The loop:

        for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 3; j++) {
                        atoms[i*10 + j] = 2.0;
                }
        }
    

    accesses well past the last allocated element (which would be atoms[29]). For example, when i == 3 and j == 0 you’re accessing atoms[30]. Any accesses when i >= 3 are out of bounds.

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

Sidebar

Related Questions

haven't used regex replaces much and am not sure if how I have done
Haven't used C++ in a while. I've been depending on my Java compiler to
I haven't used the STL much before, but I started to on this huffman
I haven't used ARC yet other than to deal with it when it forces
I haven't used multithreading in Clojure at all so am unsure where to start.
I haven't designed a website for about 3 years now, so I am quite
Haven't seen this before so hopefully someone has a easy solve, I have a
I haven't done C++ in a while and can't figure out why following doesn't
I haven't used WPF that much so the solution to this is probably pretty
Haven't seen this feature anywhere else. I know that the 32nd bit is used

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.