🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #2034

bug#15 ddj p124

#2034
From: Christopher G Busch
Date: Fri, 01 Sep 1995 04:54
33 lines
1218 bytes
On page 124 in Dr Dobbs Journal, the PC-Lint ad shows this bug #15.

----a1.cpp---
extern int *a;
void initialize(int*);

int main()
{
   initialize(a);
}

-------------
----a2.cpp----
int a[10];
void initialize(int *p)
{
   for(int i=0;i<10;i++) p[i]=i;
}
------------

I noticed that the type declarations for "a" didnt match, do I figured the
problem was there.  'extern int *a' should be changed to 'extern int a[]'
to avoid seg faults.   I have noticed this bug before in C/C++ programming
and have been careful about it.

My question is this:  Why is it, when C/C++ treats arrays as pointers that
the above bug will occur?  If *a and a[10] were different, why would it
link together correctly?  Shouldnt you get duplicate identifiers or some
error?

What exactly happens on the machine level?

--CB
Technical Details
Message-ID: <4263mu$7ib@ub.d.umn.edu>
Path: arc1-ca.pugleaf.net!umn.edu!not-for-mail

Show Full Headers
Complete Article Headers
Newsgroups: rec.games.programmer,comp.lang.c,comp.lang.c++
Path: arc1-ca.pugleaf.net!umn.edu!not-for-mail
From: cbusch@ub.d.umn.edu (Christopher G Busch)
Subject: bug#15 ddj p124
Message-ID: <4263mu$7ib@ub.d.umn.edu>
Sender: news@news.cis.umn.edu (Usenet News Administration)
Nntp-Posting-Host: ub.d.umn.edu
Organization: University of Minnesota, Duluth
Date: Fri, 1 Sep 1995 04:54:54 GMT