LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

FATAL RUN-TIME ERROR: Array argument too small

Hello, I have the following code:
 
 size = sizeof(TWaveform) + nSamples * sizeof(double);
 wf = (TWaveform*) malloc(size);
 if(!wf)
  return NULL;
 memset(wf, 0, size);
and this code blows on "memset" line with the following error:
 
 FATAL RUN-TIME ERROR:   "Waveform.c", line 51, col 12, thread id 0x000003A4:   Array argument too small (816 bytes).  Argument must contain at least 848 bytes (848 elements).
The weird thing is that the memset clears exact amount of bytes that have been allocated (variable size). Visual C++ has no problem compiling and running this code. So does CVI when disabling runtime checks, but we do not want to run completely without them.
 
Thank you
Roman
0 Kudos
Message 1 of 4
(8,829 Views)
I couldn't immediately reproduce this; to look into it further it would help if you could provide:

1. The version of CVI you are using.
2. The definition of the TWaveform struct.

Regards,

-alex
0 Kudos
Message 2 of 4
(8,824 Views)

Here is additional information:

LabWindows/CVI 8.1.0 (271)
 
The waveform.h and waveform.c are in the attachment. I already tried to turn off protection just for that piece of code using SetBreakOnProtectionErrors but it did not help either. Only global turning runtime checks off would make it work.
 
Thank you
Roman

Message Edited by romanZ on 02-02-2007 12:21 PM

0 Kudos
Message 3 of 4
(8,817 Views)
Roman,

What seems to be happening is that CVI is treating wf as a pointer to an array of TWaveform structures and rounding down the bytes to the next multiple of (sizeof(TWaveform)). For example:

let nElements = 200
size = 1648 (but CVI thinks it only has 1632)
sizeof(TWaveform) = 48

1648 / 48 = 34.333...
34 * 48 = 1632

At any rate, there is a simple workaround. If you hold the pointer in a (void *) variable, CVI will not (incorrectly) infer the type information into the memory block size as you are seeing. A revised WaveformCreate would look like this:

TWaveform* WaveformCreate (int nSamples)
{
    void *ptr;
    TWaveform *wf;
    int size;

    size = sizeof(TWaveform) + nSamples * sizeof(double);
    ptr = malloc(size);
    if(!ptr)
        return NULL;

    memset(ptr, 0, size);

    wf = (TWaveform*) ptr;
   
    wf->nSamples = nSamples;
    wf->Samples = (double*)(wf+1);
    return wf;
}


I'll submit this issue to be fixed in a future version of CVI. Sorry for the inconvenience.

-alex

Message Edited by Alex D on 02-02-2007 01:09 PM

0 Kudos
Message 4 of 4
(8,811 Views)