473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

external linking

I'm not sure if this belongs in comp.lang.c or in a compiler group, but
I'm experiencing some confusion. Consider:

[tmp]$ cat a.c
const char t[]={0xef, 0xbe, 0xad, 0xde};
const char *a=t;
int bar(void);

int
main()
{
return bar();
}
[tmp]$ cat b.c
extern const char *t;

int
bar(void)
{
return t[0];
}
[tmp]$ gcc -c b.c
[tmp]$ gcc a.c b.o
[tmp]$ ./a.out
Segmentation fault

Looking at the running process through gdb, we see that the segfault
occurs because the attempt to reference t[0] in bar attempts to
dereference the address 0xdeadbeef, which is an illegal address. If
we replace 't' with 'a' in baz, everything works fine. I'm trying to
understand the proper way to initialize constant arrays that are used
accross multiple files (don't want to define them as static as I end up
with multiple copies in the a.out, which I suppose is compiler
dependent and gcc is not doing the right thing with -fmerge-constants
(well, I called it as -Os, and my reading of the man page is that this
implies -fmerge-constants)).

Any insight appreciated.

Nov 15 '05 #1
3 1615
"bill" <bi**********@gmail.com> writes:
I'm not sure if this belongs in comp.lang.c or in a compiler group, but
I'm experiencing some confusion. Consider:

[tmp]$ cat a.c
const char t[]={0xef, 0xbe, 0xad, 0xde};
const char *a=t;
int bar(void);

int
main()
{
return bar();
}
[tmp]$ cat b.c
extern const char *t;

int
bar(void)
{
return t[0];
}
[tmp]$ gcc -c b.c
[tmp]$ gcc a.c b.o
[tmp]$ ./a.out
Segmentation fault

Looking at the running process through gdb, we see that the segfault
occurs because the attempt to reference t[0] in bar attempts to
dereference the address 0xdeadbeef, which is an illegal address. If
we replace 't' with 'a' in baz, everything works fine. I'm trying to
understand the proper way to initialize constant arrays that are used
accross multiple files (don't want to define them as static as I end up
with multiple copies in the a.out, which I suppose is compiler
dependent and gcc is not doing the right thing with -fmerge-constants
(well, I called it as -Os, and my reading of the man page is that this
implies -fmerge-constants)).

Any insight appreciated.


The reason your program isn't working is that, in a.c, you
declare/define 't' with

const char t[]=...;

which is to say t is declared to be an array, and then in
b.c you declare 't' with

extern const char *t;

which is to say t is declared to be a pointer. The mismatch
of types -- array in one case, pointer in another -- causes
inappropriate code to be generated that accesses the array
't' as though it were a pointer. The result is what you
see.

To fix the problem, declare 't' in b.c as an array rather
than a pointer:

extern const char t[];

If you don't understand the difference between arrays and
pointers, then you need to do some more reading; but I'm
guessing you understand enough to know that they are
different. The declaration 'char t[];' declares an array,
and the declaration 'char *t;' declares a pointer. These
types are not "compatible", to use the technical term of the
Standard, and when used together in different compilation
units this way will usually cause problems.

To add to the confusion, when a variable is a function
parameter, a declaration like

int foo( char name[] ){ ... }

says that the parameter 'name' is a *pointer* rather than an
array: an array-like declaration is changed into a pointer
type in a function parameter context. But that rule applies
only for function parameters, not to variables declared at
the top level (or anywhere besides parameters, for that
matter).
Nov 15 '05 #2
bill wrote:
I'm not sure if this belongs in comp.lang.c or in a compiler group, but
I'm experiencing some confusion. Consider:

[tmp]$ cat a.c
const char t[]={0xef, 0xbe, 0xad, 0xde}; [...] [tmp]$ cat b.c
extern const char *t;


const char t[] = { /* whatever */ }; /* is an array */
const chat *t; /* is a pointer */

Repeat the mantra: "An array is not a pointer."
Nov 15 '05 #3
Tim Rentsch <tx*@alumnus.caltech.edu> writes:
[...]
If you don't understand the difference between arrays and
pointers, then you need to do some more reading;

[...]

Section 6 of the FAQ <http://www.eskimo.com/~scs/C-faq/faq.html> would
be a good start.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString...
3
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open...
2
by: Freddy | last post by:
I am not an experienced programmer, but I had a VC++ program I am trying to eliminate all the VC++ commands from it...and keeping it as a normal C/C++ program......I guess I have succeeded so far,...
2
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import...
5
by: Joerg M. Colberg | last post by:
My apologies if this is a trivial problem. I have been trying to solve this for a few days now and I just can't get it done. Here's what I have. I have a C++ project in VisualStudio.NET whose code...
2
by: Ian Taite | last post by:
Help, I am trying to nail a linking problem, to avoid having to lose about 2 weeks work. I have a backup of my project that compiles and links OK however changes I made sometime since then have...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
by: Maydogg6 | last post by:
I need a hand with some stubborn link errors. I'm trying to recreate and old program from 6.0 into .NET, but for some reason when I try to compile I'm getting linking errors for all my function...
0
by: dotyet | last post by:
Hi Everyone, I am trying to build a DB2 UDB UDF which can perform regex over the table data. The user defined function will call an external .dll file to do the task. I am referring to the...
0
by: Ryan Gaffuri | last post by:
hlink72@hotmail.com (Eric) wrote in message news:<ab8d8b14.0308220550.54fb5f22@posting.google.com>... LNK1120 is a standard C++ error. you using Visual C++? Means your references a class that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.