HI Stephan!
Thx for your replay. I believe it is definitive a bug because it does not
behave as real C.
In this example I really want to allocate 10 bytes and NOT 10 int elements.
There is nothing wrong with my memory allocation. I allocate 10 bytes and
use memset() to initialize these 10 bytes with 0 (and not more).
The only task of the int pointer is to hold a pointer. Normally I use void
pointers for this purpose, but I
needed an int pointer to demonstrate the problem.
I often use void pointers to handle generic data structures. I took well tested
C source code
from other projects and used them in CVI. It took me more then 2 days to
isolate the
problem.
> I think this is not a bug, but CVI tells you that there is something wrong
> with your memory allocation. Other C compilers do not notice you about the
> error.
Yes. That's why I like real C compilers. Especially if it isn't an error.
Best regards,
Franz Hollerer
Stephan Gerhards wrote:
> Hello Franz
>
> I think this is not a bug, but CVI tells you that there is something wrong
> with your memory allocation. Other C compilers do not notice you about the
> error.
>
> I think, the problem with your code is, that you define test_t as an
> integer. An Integer type needs 4 byte for every element. In line 11 you
> reserve 10 bytes of memory, which give you 2.5 elements of the type test_t.
> Do you want to allocate memory for 10 elements of type_t? Then you must
> change your code to
>
> p = malloc (10*sizeof(test_t));
> memset (p, 0, 10*sizeof(test_t));
>
> and, by the way, don't forget to free the allocated memory...
>
> --
> Stephan Gerhards
>
> DATALOG Systeme zur Me�werterfassung GmbH & Co. KG
>
> Trompeterallee 110,
> D-41189 M�nchengladbach
> e-mail: stephan.gerhards@ni.com
>
> Franz Hollerer schrieb in im Newsbeitrag:
> 39D844EC.78003F83@hephy.oeaw.ac.at...
> > Hi,
> >
> > I have some troubles with dynamic allocated memory. Maybe this
> > is a CVI C compiler bug, because with gcc and Visual C it works
> > fine. As you can see below, I have included a sample source code.
> > The task of this program is only to isolate the error. So don�t wonder
> > about the function.
> >
> > If I start this program it crashes with the message:
> > FATAL RUN-TIME ERROR
> > "malloc_test2.c", line 12, col 10, thread id 0x0000009b
> > Array argument too small (8 bytes). Argument must contain
> > at least 10 bytes (10 elements).
> >
> > As we found out, the boundary where the program crashes depends
> > on the data type used for test_t (line 5).
> >
> > I believe this is no typical C behavior. Does anyone know a workaround
> > for this?
> >
> > Best regards,
> > Franz Hollerer
> >
> > ========================================================
> > sample c code
> > ========================================================
> > 1 #include
> > 2 #include
> > 3 #include
> > 4
> > 5 typedef int test_t;
> > 6
> > 7 int main()
> > 8 {
> > 9 test_t *p;
> > 10
> > 11 p=malloc(10);
> > 12 memset(p, 0, 10);
> > 13
> > 14 printf("sizeof(test_t)=%d\n", sizeof(test_t));
> > 15 return(0); /* avoid gcc warning */
> > 16 }
> > 17
> >
--
Institut fuer Hochenegiephysik
Nikolsdorfer Gasse 18
1050 Wien
Austria
Tel: (+43-1)5447328/50