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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:56:40+00:00 2026-06-07T16:56:40+00:00

I have a pointer to an array of structs like so: class Terrian {

  • 0

I have a pointer to an array of structs like so:

class Terrian  {
     ...
    private:
        Vector *terrian_vertices;
     ...
}

And the data for the pointer is generated in the “construct_vertices” function

Terrian::Terrian(int width, int height)  {
    this->width = width;
    this->height = height;

    std::cout << "Width: " << width << "  Height: " << height << "\n";

    std::cout << "Vertices\n";
    construct_vertices();
    std::cout << "Element\n";
    construct_elements();
    std::cout << "Buffers\n";
    construct_buffers();
}

void Terrian::construct_vertices()  {
    terrian_vertices = new Vector[width * height];

    std::cout << "Generating data\n";

    for (int x = 0; x < width; x++)  {
        for (int y = 0; y < height; y++)  {
            int index = x + y * width;

            Vector *pos = new Vector((GLfloat)x, 0.0f, (GLfloat)-y);
            memcpy(pos, terrian_vertices, sizeof(Vector) * index);

            std::cout << terrian_vertices[index].x;

            Color *color = new Color(0, 255, 0);
            memcpy(color, terrian_colors, sizeof(Color) * index);
        }
    }
}

Here is the program’s output (all I do in the main function is instantiate the object)

Width: 32  Height: 32
Vertices
Generating data
5.2349e-039
Process returned -1073741819 (0xC0000005)   execution time : 10.073 s
Press any key to continue.

The program crashes when the first pointer is copied to the array, and the output for ‘x’ should be 0. Which is puzzling. Does anyone know what is causing this to happen? If so, is there a better way to allocate structs dynamically – without using memcpy?

  • 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-07T16:56:44+00:00Added an answer on June 7, 2026 at 4:56 pm

    Does anyone know what is causing this to happen?

    The use of memcpy is incorrect. Any reference documentation will tell you that.

    The first parameter is a pointer to the destination, which would be index elements into the terrian_vertices array: terrian_vertices + index.

    The second parameter is a pointer to the source, which is pos.

    (If you’re curious, the reason the destination comes before the source is because it parallels the assignment operator: destination = source)

    The third parameter is the amount of data to copy, which in your case would just be sizeof(Vector): it’s just one Vector it needs to copy, not index.

    Misusing memcpy like the code does easily leads to undefined behaviour, which is luckily manifesting as an error.

    If so, is there a better way to allocate structs dynamically – without using memcpy?

    Yes. Don’t manage memory yourself: use std::vector and normal copy semantics.

    class Terrian  {
    // ...
    private:
        std::vector<Vector> terrain_vertices;
        // Hmm, this may need some touch up on naming,
        // or it may get confusing with two "vector" thingies around
    };
    
    // ...
    
    void Terrian::construct_vertices()  {
        terrain_vertices.reserve(width * height);
         // reserve is actually optional,
         // but I put it here to parallel the original code
         // and because it may avoid unneeded allocations
    
        std::cout << "Generating data\n";
    
        for (int x = 0; x < width; x++)  {
            for (int y = 0; y < height; y++)  {
                terrain_vertices.emplace_back((GLfloat)x, 0.0f, (GLfloat)-y);
                // or this if your compiler doesn't support C++11:
                // terrain_vertices.push_back(Vector((GLfloat)x, 0.0f, (GLfloat)-y));
    
                std::cout << terrian_vertices[index].x;
    
                // same thing for colors
                terrain_colors.emplace_back(0, 255, 0);
            }
        }
    

    Notice how now there’s no new anywhere in sight. This solves another issue with the original code: it was leaking one instance of Vector and one of Color per loop iteration.

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

Sidebar

Related Questions

I have structs like this: struct Child { int foo; char bar[42]; }; struct
In a C++ program I have two classes (structs) like struct A { int
I currently have a class like this: struct Rgb { static const int NUM_CHANNELS
I have a struct like this: class Items { private: struct item { unsigned
I have a pointer to integer array of 10. What should dereferencing this pointer
If i have a pointer to an array of pointers, is the pointer to
Suppose I have some pointer, which I want to reinterpret as static dimension array
I have a 3x3 array that I'm trying to create a pointer to and
I have an example involving a pointer to a 2D array. Can someone help
Possible Duplicate: checking if pointer points within an array If I have an array

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.