🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#309
From: Brian Bershad
Date: Sun, 14 Sep 1986 10:42
81 lines
2284 bytes
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.

After many hours, I discovered that if I say

void foo(int *x, int *y)
{
	struct stat* stbuf = new stat;
	.....
	delete stbuf;
	return;
}

things will work.... granted, this is what I want to
do, but not how I want to say it.

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).

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.

What is C++ expecting on the stack frame for non-member functions
that old C is not putting there???


	Brian Bershad
	University Washington Comp Sci
	brian@uw-bluechip.arpa
	bershad@ucbvax.berkeley.edu | ucbvax!bershad
Technical Details
Message-ID: <15671@ucbvax.BERKELEY.EDU>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!lll-crg!nike!oliveb!hplabs!ucbvax!bershad

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!lll-crg!nike!oliveb!hplabs!ucbvax!bershad
From: bershad@ucbvax (Brian Bershad)
Newsgroups: net.lang.c++
Subject: Standard C routines calling C++ (non-member) functions
Message-ID: <15671@ucbvax.BERKELEY.EDU>
Date: Sun, 14-Sep-86 06:42:03 EDT
Article-I.D.: ucbvax.15671
Posted: Sun Sep 14 06:42:03 1986
Date-Received: Sun, 14-Sep-86 20:22:35 EDT
Sender: bershad@ucbvax.BERKELEY.EDU
Reply-To: bershad@ucbvax (Brian Bershad)
Organization: University of California at Berkeley
Lines: 81