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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:47:08+00:00 2026-06-13T11:47:08+00:00

Ok, so I’m developing a small program that randomly generates a single sheet of

  • 0

Ok, so I’m developing a small program that randomly generates a single sheet of terrain using a recursive function to subdivide between four given points, and the terrain is made of of quads. The centre point found on each recursion is given a random height value, and then all of the vertices are saved to an array, which is then looped through when drawing.

Originally, I was defining the 2D array used to store the vertices on the stack, however depending on the number of subdivisions this can become quite large. A depth of 3 for example yields 256 vertices. So, I decided defining this array on the heap where possible will be much more efficient for larger sheets of terrain. In the Terrain class I define the following array of float pointers.

float* vertices[]; 

Then, in the constructor for this class, I do the following to initialize it’s size on the heap. 4 to the power of the number of given subdivisions plus one yields 256, and number of vertices that are created.

int vertexCount = pow(4.0, subdivisions+1);
*vertices = new float[vertexCount];

Then, each of these pointers points to an array of float values in the heap, defined as follows.

for(int i = 0; i<vertexCount + 1; i++)
{
    float* temp = new float[3];
    vertices[i] = temp;
    delete temp;
    temp = 0;
}

My recursive function used to create the terrain looks as follows:

void Terrain::Subdivide(float* a, float* b, float* c, float* d, int subdivisions, float      maxHeight)
{
if(subdivisions>0)
{
    float e[3];
    float f[3];
    float g[3];
    float h[3];
    float centre[3];
    int i = 0;

    srand(time(NULL));

    for(i = 0; i<3; i++) if(i!= 1) e[i] = (a[i]+b[i])/2;
    for(i = 0; i<3; i++) if(i!= 1) f[i] = (b[i]+c[i])/2;
    for(i = 0; i<3; i++) if(i!= 1) g[i] = (c[i]+d[i])/2;
    for(i = 0; i<3; i++) if(i!= 1) h[i] = (d[i]+a[i])/2;

    centre[0] = (h[0]+f[0])/2;
    centre[2] = (e[2]+g[2])/2;

    e[1] = 0;
    f[1] = 0;
    g[1] = 0;
    h[1] = 0;
    centre[1] = (float)rand()/((float)RAND_MAX/maxHeight);

    Subdivide(a, e, centre, h, subdivisions-1 , maxHeight);
    Subdivide(e, b, f, centre, subdivisions-1, maxHeight);
    Subdivide(centre, f, c, g, subdivisions-1, maxHeight);
    Subdivide(h, centre, g, d, subdivisions-1, maxHeight);
}
else
{
    int i = 0;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = a[i];} vertexCounter++;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = b[i];} vertexCounter++;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = c[i];} vertexCounter++;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = d[i];} vertexCounter++;
}

}

The code seems to work when I simply define vertices as a 2D array on the stack (with some changes elsewhere), however this code produces an unhandled exception in Glut.h on the following line, when calling glutInit in my main function: (Line 486)

static void APIENTRY glutInit_ATEXIT_HACK(int *argcp, char **argv) {     __glutInitWithExit(argcp, argv, exit); }

I’ve looked through the code so many times now and I can’t see where the problem lies, it’s even stranger that the exception should be thrown from Glut.h and not the Terrain class. Can anyone see anything wrong here?

  • 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-13T11:47:09+00:00Added an answer on June 13, 2026 at 11:47 am

    Here’s an error

    for(int i = 0; i<vertexCount + 1; i++)
    

    should be

    for(int i = 0; i<vertexCount; i++)
    

    The reason we count arrays from zero is so you don’t have to add one.

    Here’s another mistake

    for(int i = 0; i<vertexCount; i++)
    {
        float* temp = new float[3];
        vertices[i] = temp;
        delete temp;
        temp = 0;
    }
    

    should be

    for(int i = 0; i<vertexCount; i++)
    {
        vertices[i] = new float[3];
    }
    

    For some reason you allocate the memory, free it again immediately but continue to use the freed memory in your vertex array. That’s going to crash very quickly.

    Third (hopefully final) mistake is

    *vertices = new float[vertexCount];
    

    should be

    vertices = new float*[vertexCount];
    

    To simulate a 2D array, the first dimension is an array of of float pointers. That’s what the fixed code allocates.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a small JavaScript validation script that validates inputs based on Regex. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one 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.