Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Book 2, Chapters 5.1 - 5.5] Don't use dynamic allocation #1483

Closed
dimitry-ishenko opened this issue Mar 29, 2024 · 2 comments
Closed

[Book 2, Chapters 5.1 - 5.5] Don't use dynamic allocation #1483

dimitry-ishenko opened this issue Mar 29, 2024 · 2 comments
Assignees
Milestone

Comments

@dimitry-ishenko
Copy link
Contributor

There is no need to use dynamic allocation in the perlin class. Simply declare ranvec, perm_x, perm_y and perm_z as arrays and that's it:

 class perlin {
   public:
     perlin() {
-        ranvec = new vec3[point_count];
         for (int i = 0; i < point_count; i++) {
             ranvec[i] = unit_vector(vec3::random(-1,1));
         }
 
-        perm_x = perlin_generate_perm();
-        perm_y = perlin_generate_perm();
-        perm_z = perlin_generate_perm();
+        perlin_generate_perm(perm_x);
+        perlin_generate_perm(perm_y);
+        perlin_generate_perm(perm_z);
     }
-
-    ~perlin() {
-        delete[] ranvec;
-        delete[] perm_x;
-        delete[] perm_y;
-        delete[] perm_z;
-    }
 
     double noise(const point3& p) const {
@@ -71,20 +63,16 @@ class perlin {
 
   private:
     static const int point_count = 256;
-    vec3* ranvec;
-    int* perm_x;
-    int* perm_y;
-    int* perm_z;
+    vec3 ranvec[point_count];
+    int  perm_x[point_count];
+    int  perm_y[point_count];
+    int  perm_z[point_count];
 
-    static int* perlin_generate_perm() {
-        auto p = new int[point_count];
-
+    static void perlin_generate_perm(int* p) {
         for (int i = 0; i < point_count; i++)
             p[i] = i;
 
         permute(p, point_count);
-
-        return p;
     }
 
     static void permute(int* p, int n) {
@hollasch
Copy link
Collaborator

hollasch commented Apr 2, 2024

Oif. Strong agreement.

@hollasch hollasch assigned hollasch and rupsis and unassigned hollasch Apr 2, 2024
@hollasch hollasch added this to the v4.0.0 milestone Apr 2, 2024
rupsis added a commit that referenced this issue Apr 16, 2024
hollasch pushed a commit that referenced this issue Apr 16, 2024
@rupsis
Copy link
Contributor

rupsis commented Apr 16, 2024

Resolved in #1533

@rupsis rupsis closed this as completed Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants