[postgis-tickets] r15692 - Try and ensure clean memory into kmeans algorithm

Paul Ramsey pramsey at cleverelephant.ca
Mon Sep 11 11:45:31 PDT 2017


Author: pramsey
Date: 2017-09-11 11:45:31 -0700 (Mon, 11 Sep 2017)
New Revision: 15692

Modified:
   trunk/liblwgeom/lwkmeans.c
Log:
Try and ensure clean memory into kmeans algorithm


Modified: trunk/liblwgeom/lwkmeans.c
===================================================================
--- trunk/liblwgeom/lwkmeans.c	2017-09-11 17:55:38 UTC (rev 15691)
+++ trunk/liblwgeom/lwkmeans.c	2017-09-11 18:45:31 UTC (rev 15692)
@@ -93,6 +93,10 @@
 	assert(ngeoms>0);
 	assert(geoms);
 	
+    /* Initialize our static structs */
+    memset(&config, 0, sizeof(kmeans_config));
+    memset(&result, 0, sizeof(kmeans_result));
+    
 	if (ngeoms<k)
 	{
 		lwerror("%s: number of geometries is less than the number of clusters requested", __func__);
@@ -100,11 +104,13 @@
 
 	/* We'll hold the temporary centroid objects here */
 	centroids = lwalloc(sizeof(LWGEOM*) * ngeoms);
+	memset(centroids, 0, sizeof(LWGEOM*) * ngeoms);
 
 	/* The vector of cluster means. We have to allocate a */
 	/* chunk of memory for these because we'll be mutating them */
 	/* in the kmeans algorithm */
 	centers_raw = lwalloc(sizeof(POINT2D) * k);
+	memset(centers_raw, 0, sizeof(POINT2D) * k);
 
 	/* K-means configuration setup */
 	config.objs = lwalloc(sizeof(Pointer) * ngeoms);
@@ -170,6 +176,7 @@
 	dx = (max.x - min.x)/k;
 	dy = (max.y - min.y)/k;
 	seen = lwalloc(sizeof(int)*config.k);
+	memset(seen, 0, sizeof(int)*config.k);
 	for (i = 0; i < k; i++)
 	{
 		int closest;



More information about the postgis-tickets mailing list