the stack overflow issue was related to a not correctly initialized external variable (size). Use a ternery to prevent to large size values.