build error on Linux - initializer element is not computable at load time

B

B. Wood

I have a small program (see below) that demonstrates an error I am
getting when I build the program on a linux platform.

I first initialize an unsigned char array (lines 16).

I then initialize my array of my typedeffed structure (dataStruct)
(lines 18-24). I use my initialized unsigned character array to
initialize the unsigned character pointer member of the structure
(line 21).

I then do a for loop, dumping the initialized data in the array of
structures.

This compiles fine under Windows 2000 using Microsoft 32-bit C/C++
Optimizing Compiler Version 12.00.8804, with no errors or warnings and
executes as expected.

On a linux platform, running Red Hat Linux 7.1 2.96-98 using gcc
version 2.96 20000731 I get the following error:

- initializer element is not computable at load time

I get the error on line 21 where I initialize my structures unsigned
char pointer member with the initialized unsigned character array.

I get the same error on line 23 where the closing brace of the
structure member initialize is.

Does anyone have any ideas on what this could be.

1 typedef struct dataStruct_
2 {
3 unsigned char * buffer;
4 unsigned int length;
5 }
6 dataStruct;
7
8 int
9 main
10 (
11 int argc,
12 char * argv[]
13 )
14 {
15
16 unsigned char buffer[] = { 1,2,3,4,5 };
17
18 dataStruct data[] =
19 {
20 {
21 buffer,
22 sizeof(buffer)
23 }
24 };
25
26 int i;
27
28 for (i = 0; i < 5; i++)
29 {
30 printf("%d\n", data[0].buffer);
31 }
32 printf("data size = %d\n", data[0].length);
33
34 return (EXIT_SUCCESS);
35
36 }
 
M

Mathew Hendry

I have a small program (see below) that demonstrates an error I am
getting when I build the program on a linux platform.

I first initialize an unsigned char array (lines 16).

I then initialize my array of my typedeffed structure (dataStruct)
(lines 18-24). I use my initialized unsigned character array to
initialize the unsigned character pointer member of the structure
(line 21).

I then do a for loop, dumping the initialized data in the array of
structures.

This compiles fine under Windows 2000 using Microsoft 32-bit C/C++
Optimizing Compiler Version 12.00.8804, with no errors or warnings and
executes as expected.

Try it with compiler extensions disabled (I'm using a later version of the
compiler but I'm pretty sure yours will behave in the same way).

D:\source\clc>cl /Za 031016_1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

031016_1.c
031016_1.c(21) : error C2093: 'buffer' : cannot be initialized using address
of automatic variable '
buffer'
031016_1.c(16) : see declaration of 'buffer'
031016_1.c(21) : error C2097: illegal initialization
031016_1.c(34) : error C2065: 'EXIT_SUCCESS' : undeclared identifier

You'll need to replace the initialization with a post-inititialization
assignment.

(And fix the EXIT_SUCCESS error, and the missing prototype for printf).

-- Mat.
 
E

Eddahbi Karim

On Thu, 16 Oct 2003 17:56:24 +0100

[snip]
(And fix the EXIT_SUCCESS error, and the missing prototype for
printf).

-- Mat.

#include <stdio.h>
#include <stdlib.h>

:)
 
B

B. Wood

Eddahbi Karim said:
On Thu, 16 Oct 2003 17:56:24 +0100

[snip]
(And fix the EXIT_SUCCESS error, and the missing prototype for
printf).

-- Mat.

#include <stdio.h>
#include <stdlib.h>

:)

--

Yes, I realize I didn't include in my posting the includes and some
other things, I just wanted to post the heart of the code.
Try it with compiler extensions disabled (I'm using a later version of the
compiler but I'm pretty sure yours will behave in the same way).

D:\source\clc>cl /Za 031016_1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

When you say try compiler extensions, you do mean the gcc compiler on
the Linux platorm right. Because the next part of your post shows you
running the Microsoft compiler. I am not having any problems with
that compiler. It is the gcc compiler on the Linux box that has the
problem.
 
M

Mathew Hendry

When you say try compiler extensions, you do mean the gcc compiler on
the Linux platorm right. Because the next part of your post shows you
running the Microsoft compiler. I am not having any problems with
that compiler. It is the gcc compiler on the Linux box that has the
problem.

I'm saying that it only works on your Microsoft compiler because you have
compiler extensions enabled. The /Za option (as above) turns them off and
forces the compiler to report the error - the code is not portable and gcc
is correct to complain.

-- Mat.
 
K

Kevin Bracey

In message <[email protected]>
Mathew Hendry said:
I'm saying that it only works on your Microsoft compiler because you have
compiler extensions enabled. The /Za option (as above) turns them off and
forces the compiler to report the error - the code is not portable and gcc
is correct to complain.

That's assuming we're trying to write C90, by the way. The original poster's
code is valid C99, so a modern version of gcc with its partial C99 support
will probably handle it.
 
I

Irrwahn Grausewitz

Kevin Bracey said:
In message <[email protected]>


That's assuming we're trying to write C90, by the way. The original poster's
code is valid C99, so a modern version of gcc with its partial C99 support
will probably handle it.

gcc 3.2 does.
 
G

Glen Herrmannsfeldt

B. Wood said:
I have a small program (see below) that demonstrates an error I am
getting when I build the program on a linux platform.

I first initialize an unsigned char array (lines 16).

I then initialize my array of my typedeffed structure (dataStruct)
(lines 18-24). I use my initialized unsigned character array to
initialize the unsigned character pointer member of the structure
(line 21).
(snip)

16 unsigned char buffer[] = { 1,2,3,4,5 };
17
18 dataStruct data[] =

You could also make buffer static, if that works with the logic of the
program.

If you don't change the values, that should work. Though not changing the
data in a buffer would be unusual.

-- glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top