LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Q: malloc and type info - fatul run time error

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 rega
rds,
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
0 Kudos
Message 1 of 4
(3,433 Views)
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
>
0 Kudos
Message 2 of 4
(3,433 Views)
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
0 Kudos
Message 3 of 4
(3,433 Views)
Nothing like throwing out an answer almost three years after the fact.

The CVI has some runtime protection added in, which watches your arrays for you and tries to catch things like overflows. The run-time protection can't see the half int, so it thinks there's going to be an overflow. You can disable run-time protection as a build option, then your code should work fine.

Hopefully, you've already fixed or worked around this problem, but this is probably the cause.

jon
0 Kudos
Message 4 of 4
(3,433 Views)