LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Q: problems allocating pointers with malloc

Hi,

I have some troubles allocating pointers with malloc. 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.

In line 34 I declare a new data type 'test_t' which consists of some
int and pointer to int.
In line 45 I call memalloc() to allocate a memory for such a variable.
memalloc() is a wrapper function to malloc() which terminates the
program
if malloc fails otherwise it initializes the memory with 0.
The real problem can be found in line 46 where the void pointer returned

from memalloc() is assigned to a 'test_t' pointer. All fields of the
allocated
'test_t' variable which are pointers are set to 0xfd1e1f10. Other
fields like the 'int status' are left zero.

If I use calloc() instead of malloc() at line 20 it works.
What's going on? I can't explain this behavior.

Any ideas?

Best regards,
Franz Hollerer

========================================================
sample c code
========================================================
1 #include
2 #include
3 #include
4
5
6 /* =============================================== */
7
8 void *memalloc(size_t size);
9
10 /* =============================================== */
11
12 /*
13 * allocate and clear memory
14 */
15 void *memalloc(size)
16 size_t size;
17 {
18 void *p;
19
20 p=malloc(size);
21
22 if (!p)
23 {
24 fprintf(stderr, "malloc() faild\n");
25 exit(1);
26 }
27
28 memset(p, 0, size);
29 return(p);
30 } /* eof memalloc */
31
32 /* =============================================== */
33
34 typedef struct condgen_t{
35 int *next, *prev;
36 int status;
37 int *condnr;
38 } test_t;
39
40 int main()
41 {
42 void *vp;
43 test_t *p;
44
45 vp=memalloc(sizeof(test_t));
46 p=vp;
47 return(0); /* avoid gcc warning */
48 }
49

--
Institut fuer Hochenegiephysik
Nikolsdorfer Gasse 18
1050 Wien
Austria

Tel: (+43-1)5447328/50
0 Kudos
Message 1 of 3
(3,253 Views)
Hi,

I found a workaround for this problem. If I use an int pointer in memalloc()
(line 15-30)
instead of a void pointer, it works. Nevertheless this is not C.

Franz Hollerer

Franz Hollerer wrote:

> Hi,
>
> I have some troubles allocating pointers with malloc. 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.
>
> In line 34 I declare a new data type 'test_t' which consists of some
> int and pointer to int.
> In line 45 I call memalloc() to allocate a memory for such a variable.
> memalloc() is a wrapper function to malloc() which terminates the
> program
> if malloc fails otherwise it initializes the memory with 0.
> The real problem can be found in line 46 where the void pointer returned
>
> from memalloc() is assigned to a 'test_t' pointer. All fields of the
> allocated
> 'test_t' variable which are pointers are set to 0xfd1e1f10. Other
> fields like the 'int status' are left zero.
>
> If I use calloc() instead of malloc() at line 20 it works.
> What's going on? I can't explain this behavior.
>
> Any ideas?
>
> Best regards,
> Franz Hollerer
>
> ========================================================
> sample c code
> ========================================================
> 1 #include
> 2 #include
> 3 #include
> 4
> 5
> 6 /* =============================================== */
> 7
> 8 void *memalloc(size_t size);
> 9
> 10 /* =============================================== */
> 11
> 12 /*
> 13 * allocate and clear memory
> 14 */
> 15 void *memalloc(size)
> 16 size_t size;
> 17 {
> 18 void *p;
> 19
> 20 p=malloc(size);
> 21
> 22 if (!p)
> 23 {
> 24 fprintf(stderr, "malloc() faild\n");
> 25 exit(1);
> 26 }
> 27
> 28 memset(p, 0, size);
> 29 return(p);
> 30 } /* eof memalloc */
> 31
> 32 /* =============================================== */
> 33
> 34 typedef struct condgen_t{
> 35 int *next, *prev;
> 36 int status;
> 37 int *condnr;
> 38 } test_t;
> 39
> 40 int main()
> 41 {
> 42 void *vp;
> 43 test_t *p;
> 44
> 45 vp=memalloc(sizeof(test_t));
> 46 p=vp;
> 47 return(0); /* avoid gcc warning */
> 48 }
> 49
>
> --
> Institut fuer Hochenegiephysik
> Nikolsdorfer Gasse 18
> 1050 Wien
> Austria
>
> Tel: (+43-1)5447328/50

--
Institut fuer Hochenegiephysik
Nikolsdorfer Gasse 18
1050 Wien
Austria

Tel: (+43-1)5447328/50
0 Kudos
Message 2 of 3
(3,253 Views)
Hi,

I have started a discussion in comp.lang.c which deals with this
problem. Keith Thomson has rewritten the demo program (see below).

output of debuggable executable:

null_pointer = 0
sizeof(void *) = 4
sizeof(test_t *) = 4
sizeof(int *) = 4
p = a20060
p->next = 101f1efd
p->prev = 101f1efd
p->status = 0
p->condnr = 101f1efd

output of release executeable:

null_pointer = 0
sizeof(void *) = 4
sizeof(test_t *) = 4
sizeof(int *) = 4
p = a20060
p->next = 0
p->prev = 0
p->status = 0
p->condnr = 0

This means that the debuggable executable has some problem.

Franz Hollerer



1 #include
2 #include
3 #include
4
5 /*
6 * I've deleted the superfluous declaration for memalloc. -- kst
7 */
8
9 void *memalloc(size_t size)
10 /*
11 * Old-style declarations are still legal, but there's really
12 * no point in using them. -- kst
13 */
14 {
15 void *p;
16
17 p=malloc(size);
18
19 if (!p)
20 {
21 fprintf(stderr, "malloc() faild\n");
22 exit(1);
23 }
24
25 memset(p, 0, size);
26 /*
27 * As others have pointed out, this sets the struct to all zeros,

28 * so it doesn't necessarily set the next, prev, and condnr
members
29 * to NULL. I don't think that's causing the problem you're
seeing,
30 * though. -- kst
31 */
32
33 return(p);
34 }
35
36 /* =============================================== */
37
38 typedef struct condgen_t{
39 int *next, *prev;
40 int status;
41 int *condnr;
42 } test_t;
43
44 /*
45 * Added function show_info() to show some possibly relevant
46 * characteristics of your system.
47 */
48 void show_info(void)
49 {
50 void *null_pointer = NULL;
51
52 printf("null_pointer = %p\n", null_pointer);
53 printf("sizeof(void*) = %d\n", (int)sizeof(void*));
54 printf("sizeof(test_t*) = %d\n", (int)sizeof(test_t*));
55 printf("sizeof(int*) = %d\n", (int)sizeof(int*));
56 }
57
58 int main(void)
59 /*
60 * Changed declaration from "int main()" to "int main(void)" -- kst
61 */
62 {
63 test_t *p;
64 p = memalloc(sizeof(test_t));
65 /*
66 * Deleted unnecessary void* intermediate pointer. memalloc()
67 * returns a void*, but you can assign the result directly to
68 * a test_t*. -- kst
69 */
70 show_info();
71 printf("p = %p\n", (void*)p);
72 printf("p->next = %p\n", (void*)p->next);
73 printf("p->prev = %p\n", (void*)p->prev);
74 printf("p->status = %d\n", p->status);
75 printf("p->condnr = %p\n", p->condnr);
76 /*
77 * Finally, show some information. -- kst
78 */
79 getchar();
80 return 0;
81 /*
82 * Note: The "return 0" isn't just to avoid a gcc warning, it's
83 * a necessary part of the program. Without it, you'd be
returning
84 * some arbitrary status to the environment. -- kst
85 */
86 }
87

--
Institut fuer Hochenegiephysik
Nikolsdorfer Gasse 18
1050 Wien
Austria

Tel: (+43-1)5447328/50
0 Kudos
Message 3 of 3
(3,253 Views)