Przeglądaj źródła

use calloc to initialize arrays of scalar values

Julius Pfrommer 10 lat temu
rodzic
commit
77398a57de
2 zmienionych plików z 5 dodań i 1 usunięć
  1. 4 1
      src/ua_types.c
  2. 1 0
      src/ua_util.h

+ 4 - 1
src/ua_types.c

@@ -1005,8 +1005,11 @@ void* UA_Array_new(const UA_DataType *dataType, UA_Int32 noElements) {
     if((UA_Int32)dataType->memSize * noElements < 0 || dataType->memSize * noElements > MAX_ARRAY_SIZE )
         return UA_NULL;
 
+    if(dataType->fixedSize)
+        return calloc(noElements, dataType->memSize);
+
     void *p = malloc(dataType->memSize * (size_t)noElements);
-    if(!p || dataType->fixedSize) // datatypes of fixed size are not initialized.
+    if(!p)
         return p;
 
     uintptr_t ptr = (uintptr_t)p;

+ 1 - 0
src/ua_util.h

@@ -27,6 +27,7 @@
 /* Replace the macros with functions for custom allocators if necessary */
 #define UA_free(ptr) free(ptr)
 #define UA_malloc(size) malloc(size)
+#define UA_calloc(num, size) calloc(num, size)
 #define UA_realloc(ptr, size) realloc(ptr, size)
 #define UA_memcpy(dst, src, size) memcpy(dst, src, size)
 #define UA_memset(ptr, value, size) memset(ptr, value, size)