c和C ++兼容性 [英] c and C++ compatibility

查看:110
本文介绍了c和C ++兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在组合C和C ++的功能时遇到了问题。我是
试图创建一个显示文件,功能

和想要记录某条消息的代码的行号的日志记录功能。

函数被称为MainDisplay并输出到一个名为mylog.log的文件。

出于某种原因,当我尝试和/或$时,我有时会出现分段错误b $ b打开文件的输出流。代码类似于

这个:


//文件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显示(const char *文件,

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 + = di splay2;


log_stream.open(" my_log.log",ios :: app);

log_stream<< s;

log_stream.close();

}


// end display.h

上述内容将从以下任何文件中调用:


//文件randomfile.cpp


#include" display.h"


void randomfunction(){

char * name =" bob" ;;

MainDisplay("我的名字是%s \ n",name);

}


//结束randomfile.cpp


但是,有时,并非总是如此,当我尝试
时,我会出现分段错误并且执行

log_stream.open(" my_log.log",ios) :: app);

从调试中我发现,如果我从一个执行大量C风格字符串的函数调用MainDisplay

,就会发生这种情况操作。


怎么回事?

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?

推荐答案

Prashant写道:
Prashant wrote:
我遇到了组合C和C ++函数的问题。我正在尝试创建一个日志记录功能,显示文件,函数
和想要记录某个消息的代码的行号。
函数称为MainDisplay并输出到名为mylog.log的文件。
出于某种原因,当我尝试打开输出流到文件时,我有时会出现分段错误。代码看起来像
这个:

//文件display.h
#include< stdarg.h>
#include< stdio.h>
#include< iostream>


不是C标题。

#include< fstream.h>


不是C头;甚至不是C ++标题。

using namespace std;


C中的语法错误。

#define MainDisplay(msg,...)显示(__ FILE __,\
__ PRETTY_FUNCTION __,\
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__, \




未定义的初始化程序


为什么还要继续?没有C编译器会接受这个东西,而且没有C ++
应该预计
编译器。



Undefined initializer

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


Prashant写道:
Prashant wrote:
我是有一个组合C和C ++函数的问题。我正在尝试创建一个日志功能,显示文件,函数
和想要记录某个消息的代码的行号。
函数被称为MainDisplay并输出到一个名为mylog.log的文件。
出于某种原因,当我尝试打开输出流到文件时,我有时会出现分段错误。看起来像
这个:

//文件display.h
#include< stdarg。 h>
#include< stdio.h>
#include< iostream>


不是C标题。

#include< fstream.h>


不是C头;甚至不是C ++标题。

using namespace std;


C中的语法错误。

#define MainDisplay(msg,...)显示(__ FILE __,\
__ PRETTY_FUNCTION __,\
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__, \




未定义的标识符


为什么还要继续?没有C编译器会接受这个东西,而且没有C ++
应该预计
编译器。



Undefined identifier

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




" Prashant"< pj *** @ ualberta。 ca>在消息中写道

"Prashant" <pj***@ualberta.ca> wrote in message

但是,有时,并非总是如此,当我尝试
并且执行
log_stream.open(" my_log)时,我遇到了分段错误.log",ios :: app);
从调试我发现,如果我从一个执行大量C风格字符串操作的函数调用MainDisplay
,就会发生这种情况。这是怎么回事?

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?



我不熟悉C ++的iostream调用。你需要确保

log-stream对象i我不会以任何方式搞砸,而且ios :: app

(无论是什么)是有效的。

最可能的解释是你的C-样式字符串操作是
垃圾记忆。


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.


这篇关于c和C ++兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆