dynamically allocate memory and read data when # samples is specified.