🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c++
Article #313

Standard C routines calling C++ (non-member) functions

#313
From: Bjarne Stroustrup
Date: Mon, 15 Sep 1986 16:19
86 lines
3042 bytes
> From: bershad@ucbvax (Brian Bershad)
> Subject: Standard C routines calling C++ (non-member) functions
> Organization: University of California at Berkeley
>
> I am running into a strange problem when linking in C++ code with Old C.
> 
> The following code shows what is happening:

	/* THIS IS STANDARD C */
	extern void foo();

	main()
	{
  	int x = 23;
  	foo(&x,&x);
	}

> ----------------------------

	/* THIS IS C++ */
	#include <sys/types.h>
	#include <sys/stat.h>

	int lstat(char*, stat*);

	void foo(int *x, int *y)
	{
		struct stat stbuf;
		lstat("/etc/motd", &stbuf);
	
		return;
	}

> ------------------------------------
> 
> and they are compiled as:
> 	% cc -c main.c
> 	% CC -c foo.c main.o
> 

> When I write into &stbuf via the lstat, the foo's return
> address seems to get clobbered, and the program core
> dumps with an illegal instruction at the return statement,
> with the values for x and y getting set to nil.

I conjecture that you are using a SysV declaration of ``struct stat'' in
/usr/include/CC/sys/stat.h from C++ and a BSD4.? declaration ``struct stat''
in /usr/include/sys/stat.h from C. Since the BSD stat is larger than the SysV
stat you are in trouble. Please modify /usr/include/CC/sys/stat.h to fit
your system.

> More annoyingly, the following core dumps in ostream...
> 
> void foo(int* x, int* y)
> {
> 	cerr << *x;
> 	cerr << *y;
> }
> 
> when it is called from a C program (for reasons that are beyond
> me... I can guess on the first example, but on this one, I have no
> idea).

I conjecture that you did not load the program using CC so that the constructors
initializing cin, cout, and cerr were not called. A similar effect (core dump
on the first C++ stream I/O operation) can be obtained by not compiling main()
by CC.

> Since I am writing a fairly substantial package that uses
> somebody else's RPC (with stubs generated in C) routines,
> the fact that this stuff doesn't work is causing me numerous
> headaches.

Sorry about that, and thankyou for complaining where others can benefit from
your experiences. It ought to work. Please confirm or complain again.
 
> What is C++ expecting on the stack frame for non-member functions
> that old C is not putting there???

Nothing, C++ does not diddle with stack layouts or with structure layouts for
functions or structures that can be expressed in C.

>	Brian Bershad
>	University Washington Comp Sci
>	brian@uw-bluechip.arpa
>	bershad@ucbvax.berkeley.edu | ucbvax!bershad
Technical Details
Message-ID: <6059@alice.uUCp>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!columbia!caip!princeton!allegra!alice!bs

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!columbia!caip!princeton!allegra!alice!bs
From: bs@alice.UucP (Bjarne Stroustrup)
Newsgroups: net.lang.c++
Subject: Standard C routines calling C++ (non-member) functions
Message-ID: <6059@alice.uUCp>
Date: Mon, 15-Sep-86 12:19:36 EDT
Article-I.D.: alice.6059
Posted: Mon Sep 15 12:19:36 1986
Date-Received: Mon, 15-Sep-86 21:18:37 EDT
Organization: Bell Labs, Murray Hill
Lines: 88