c and C++ compatibility

P

Prashant

Hi, i'm having an issue with combining functions from C and C++. I'm
trying to create a logging function that displays the file, function
and line number of the code that wants to log a certain message. The
function is called MainDisplay and outputs to a file called mylog.log.
For some reason, I SOMETIMES get a segmentation fault when I try and
open an output stream to the file. The code looks something like
this:

//file display.h
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <fstream.h>
using namespace std;

#define MainDisplay(msg, ...) Show(__FILE__,\
__PRETTY_FUNCTION__, \
__LINE__, msg, ## __VA_ARGS___);


void Show(const char* file,
const char* fcn,
int line,
char* display, ...) {

va_list va;
va_start(va, display);

char display2[256];
vsprintf(display2, display, va);
va_end(va);

char linestr[10];
sprintf(linestr, "%d", line);

string s = file;
s += ": function ";
s += fcn;
s += ": line ";
s += linestr;
s += ": ";
s += display2;

log_stream.open("my_log.log", ios::app);
log_stream << s;
log_stream.close();
}

//end display.h

The above would be called from any file as follows:

//file randomfile.cpp

#include "display.h"

void randomfunction() {
char* name = "bob";
MainDisplay("Hi, my name is %s\n", name);
}

//end randomfile.cpp

But, sometimes, and not always, I get a segmentation fault when I try
and do
log_stream.open("my_log.log", ios::app);
From debugging i've found that this happens if i'm calling MainDisplay
from a function that does a lot of C-style string manipulation.

Whats going on here?
 
M

Martin Ambuhl

Prashant said:
Hi, i'm having an issue with combining functions from C and C++. I'm
trying to create a logging function that displays the file, function
and line number of the code that wants to log a certain message. The
function is called MainDisplay and outputs to a file called mylog.log.
For some reason, I SOMETIMES get a segmentation fault when I try and
open an output stream to the file. The code looks something like
this:

//file display.h
#include <stdarg.h>
#include <stdio.h>
#include <iostream>

Not a C header.
#include <fstream.h>

Not a C header; not even a C++ header.
using namespace std;

A syntax error in C.
#define MainDisplay(msg, ...) Show(__FILE__,\
__PRETTY_FUNCTION__, \

Undefined initializer

Why bother going on? No C compiler will accept this stuff, and no C++
compiler should be expected to.
 
M

Martin Ambuhl

Prashant said:
Hi, i'm having an issue with combining functions from C and C++. I'm
trying to create a logging function that displays the file, function
and line number of the code that wants to log a certain message. The
function is called MainDisplay and outputs to a file called mylog.log.
For some reason, I SOMETIMES get a segmentation fault when I try and
open an output stream to the file. The code looks something like
this:

//file display.h
#include <stdarg.h>
#include <stdio.h>
#include <iostream>

Not a C header.
#include <fstream.h>

Not a C header; not even a C++ header.
using namespace std;

A syntax error in C.
#define MainDisplay(msg, ...) Show(__FILE__,\
__PRETTY_FUNCTION__, \

Undefined identifier

Why bother going on? No C compiler will accept this stuff, and no C++
compiler should be expected to.
 
M

Malcolm

Prashant said:
But, sometimes, and not always, I get a segmentation fault when I try
and do
log_stream.open("my_log.log", ios::app);
From debugging i've found that this happens if i'm calling MainDisplay
from a function that does a lot of C-style string manipulation.

Whats going on here?
I'm not familiar with the C++ iostream call. You need to make sure that the
log-stream object isn't being messed with in any way, also that ios::app
(whatever that is) is valid.
The most likely explanation is that your C-style string manipulation is
trashing memory.
 
P

Prashant

Malcolm said:
I'm not familiar with the C++ iostream call. You need to make sure that the
log-stream object isn't being messed with in any way, also that ios::app
(whatever that is) is valid.
The most likely explanation is that your C-style string manipulation is
trashing memory.

Yeah, why does that happen? Is C style string manipulation somehow
incompatible with C++? I get strange segmentation faults elsewhere I
use C-strings for no apparent reason. I've switched to the C++ string
objects, but I want to figure how why this incompatibility exists.
 
J

Jens.Toerring

Yeah, why does that happen? Is C style string manipulation somehow
incompatible with C++? I get strange segmentation faults elsewhere I
use C-strings for no apparent reason. I've switched to the C++ string
objects, but I want to figure how why this incompatibility exists.

There are probably no incompatibilities. Chances are rather that you
got it wrong and are trashing memory, i.e. write to memory you don't
own etc., as Malcolm already pointed out. At least that's the typical
reason for segmentation faults. But since you don't show the code you
are using we'll never know.
Regards, Jens
 
M

Malcolm

Prashant said:
Yeah, why does that happen? Is C style string manipulation somehow
incompatible with C++?
It's not totally incompatible, but if you mix and match C and C++ strings
then you have to be careful. A C++ string can be extended, for instance,
whilst a C string needs memory explicitly reserved to allow you to add more
characters.
I get strange segmentation faults elsewhere I use C-strings for no apparent
reason. I've switched to the C++ string objects, but I want to figure how why
this incompatibility exists.
C makes it extremely easy to read or write to memory you don't own. For
instance the following

void foo(char *str)
{
char *copy;

copy = malloc( strlen(str) );
if(copy)
strcpy(copy, str);

...
}

forgets the terminating NUL and writes to memory you don't own. On many
systems malloc() will actually return more memory than you ask for, most of
the time, so the function will appear to work until the length of the string
is a exact power of two (maybe) when it will segfault.

I'm not saying you make exactly this mistake, but it's the kind of thing
that is extremely esay to do.
 
D

Default User

Prashant said:
Yeah, why does that happen? Is C style string manipulation somehow
incompatible with C++? I get strange segmentation faults elsewhere I
use C-strings for no apparent reason. I've switched to the C++ string
objects, but I want to figure how why this incompatibility exists.


We gots to see some code. There's no intrinsic reason why you can't use
C-style strings successfully, so most likely you are using them
incorrectly.



Brian Rodenborn
 
K

Keith Thompson

Default User said:
We gots to see some code. There's no intrinsic reason why you can't use
C-style strings successfully, so most likely you are using them
incorrectly.

C-style string functions and the functions that manipulate them are
part of the C++ language definition. C++ string objects, on the other
hand, are not part of C.

Any questions about interactions between them belong in comp.lang.c++,
not in comp.lang.c. I'm afraid we can't help you here.
 
P

Prashant

Malcolm said:
It's not totally incompatible, but if you mix and match C and C++ strings
then you have to be careful. A C++ string can be extended, for instance,
whilst a C string needs memory explicitly reserved to allow you to add more
characters.
C makes it extremely easy to read or write to memory you don't own. For
instance the following

void foo(char *str)
{
char *copy;

copy = malloc( strlen(str) );
if(copy)
strcpy(copy, str);

...
}

forgets the terminating NUL and writes to memory you don't own. On many
systems malloc() will actually return more memory than you ask for, most of
the time, so the function will appear to work until the length of the string
is a exact power of two (maybe) when it will segfault.

I'm not saying you make exactly this mistake, but it's the kind of thing
that is extremely esay to do.


I'm using new to allocate my C-strings. But you guys were right in
that its trashing memory somehow. I switched everything to use C++
strings and everything seems to work fine now. Does anybody know of a
good reference on memory allocation? I think it would be good to read
up on this since it seems like I don't really have a firm grasp of the
intricacies.
 
K

Kelsey Bjarnason

[snips]

....
I'm using new to allocate my C-strings. But you guys were right in
that its trashing memory somehow. I switched everything to use C++
strings and everything seems to work fine now. Does anybody know of a
good reference on memory allocation? I think it would be good to read
up on this since it seems like I don't really have a firm grasp of the
intricacies.

Allocation isn't the problem, you do that just fine (even checking to see
if the allocation worked or not). The problem is in figuring out how much
to allocate. Consider the string literal "123" - how long is it? That
depends. strlen() says it's 3. Counting bytes says it's four: '1', '2',
'3' and '\0'. Your allocation above allocates space based on the length
of the string, but neglects space for the terminating \0.

Your only shortfall in that snippet above was confusing "length" with
"space required". You can make similar mistakes by, say, trying to
calculate the size of a struct based on the size of its members; if you
forget the padding (if any), your numbers may not reflect the real size.
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top