求New函数源码

oosky2004 2005-08-24 08:18:05
如题,想看看new函数是怎么实现的?
还有比如其他函数:printf()。。。

...全文
314 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowbirdfly 2005-08-25
  • 打赏
  • 举报
回复
是啊~
printf()函数有啊~
但是new是运算符,又不是函数,怎么可能函数的代码呢???
边城狂人 2005-08-25
  • 打赏
  • 举报
回复
new 是函数,那就怪了……!
不过 new 可以重载好像是真的。

printf……如果找到源码我也要一份。
oosky2004 2005-08-25
  • 打赏
  • 举报
回复
----------------------------------vfprintf()--------------------太长了,贴不上来。

/* The function itself. */
int
vfprintf (FILE *s, const CHAR_T *format, va_list ap)
{
/* The character used as thousands separator. */
#ifdef COMPILE_WPRINTF
wchar_t thousands_sep = L'\0';
#else
const char *thousands_sep = NULL;
#endif

/* The string describing the size of groups of digits. */
const char *grouping;

/* Place to accumulate the result. */
int done;

/* Current character in format string. */
const UCHAR_T *f;

/* End of leading constant string. */
const UCHAR_T *lead_str_end;

/* Points to next format specifier. */
const UCHAR_T *end_of_spec;

/* Buffer intermediate results. */
CHAR_T work_buffer[1000];
CHAR_T *workstart = NULL;
CHAR_T *workend;

/* State for restartable multibyte character handling functions. */
#ifndef COMPILE_WPRINTF
mbstate_t mbstate;
#endif

/* We have to save the original argument pointer. */
va_list ap_save;

/* Count number of specifiers we already processed. */
int nspecs_done;

/* For the %m format we may need the current `errno' value. */
int save_errno = errno;

/* 1 if format is in read-only memory, -1 if it is in writable memory,
0 if unknown. */
int readonly_format = 0;

/* This table maps a character into a number representing a
class. In each step there is a destination label for each
class. */
static const int jump_table[] =
{
/* ' ' */ 1, 0, 0, /* '#' */ 4,
0, /* '%' */ 14, 0, /* '\''*/ 6,
0, 0, /* '*' */ 7, /* '+' */ 2,
0, /* '-' */ 3, /* '.' */ 9, 0,
/* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
/* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
/* '8' */ 8, /* '9' */ 8, 0, 0,
0, 0, 0, 0,
0, /* 'A' */ 26, 0, /* 'C' */ 25,
0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
0, /* 'I' */ 29, 0, 0,
/* 'L' */ 12, 0, 0, 0,
0, 0, 0, /* 'S' */ 21,
0, 0, 0, 0,
/* 'X' */ 18, 0, /* 'Z' */ 13, 0,
0, 0, 0, 0,
0, /* 'a' */ 26, 0, /* 'c' */ 20,
/* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
/* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
/* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
/* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
/* 't' */ 27, /* 'u' */ 16, 0, 0,
/* 'x' */ 18, 0, /* 'z' */ 13
};

#define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
#define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
#if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
/* 'int' is enough and it saves some space on 64 bit systems. */
# define JUMP_TABLE_TYPE const int
# define JUMP(ChExpr, table) \
do \
{ \
int offset; \
void *__unbounded ptr; \
spec = (ChExpr); \
offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
: table[CHAR_CLASS (spec)]; \
ptr = &&do_form_unknown + offset; \
goto *ptr; \
} \
while (0)
#else
# \
oosky2004 2005-08-25
  • 打赏
  • 举报
回复
只找到printf()
------------------------------printf()--------------------------
#include <stdarg.h>
#include <stdio.h>

#undef printf

/* Write formatted output to stdout from the format string FORMAT. */
/* VARARGS1 */
int
printf (const char *format, ...)
{
va_list arg;
int done;

va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);

return done;
}

#undef _IO_printf
/* This is for libg++. */
strong_alias (printf, _IO_printf);
------------------------------------------------------------------
zhouhuahai 2005-08-25
  • 打赏
  • 举报
回复
楼主可以从<<CSDN C/C++电子杂志>>第一期中 老迈所著的<<可变参数>>一节中找到printf的原理.
ningzhiyu 2005-08-25
  • 打赏
  • 举报
回复
还有,

“new是函数吗???!!!面壁10分钟先!”太过激了吧,呵呵

在使用角度new是操作符,在new实现中,new也是用函数实现的吧
ningzhiyu 2005-08-25
  • 打赏
  • 举报
回复
http://directory.fsf.org/libs/c/
下个glibc
stdio-common下有printf等函数实现代码

new的应该在gcc/g++中用
http://directory.fsf.org/devel/compilers/gpp.html
但真还不好找……找着了告诉我一声:)
oosky2004 2005-08-25
  • 打赏
  • 举报
回复
不好意思,说误嘴了。new是运算符!

SeaWave 2005-08-25
  • 打赏
  • 举报
回复
回复人: xxandxx(luck) ( ) 信誉:100 2005-08-25 13:50:00 得分: 0
new 既然是void,为什么还return res呢?new的代码有问题啊
---------------------
谁说net是void的,它是返回void *,返回一个指针。


oosky2004 2005-08-25
  • 打赏
  • 举报
回复
Kenmark
正解。
我啃 2005-08-25
  • 打赏
  • 举报
回复
……,我觉得UNIX中内核用到的PRINTF函数写的不错,VOID的变量不代表没有返回值而是类型为“空”,也就是所有的指针都可以指向它,但是VOID*类型的指针不能用*(因为它没有类型,不知道读取多少内存单元)
xxandxx 2005-08-25
  • 打赏
  • 举报
回复
new 既然是void,为什么还return res呢?new的代码有问题啊
lyclowlevel 2005-08-25
  • 打赏
  • 举报
回复
void *new(size_t size);
basesky 2005-08-25
  • 打赏
  • 举报
回复
new 操作符的源码


void * operator new( unsigned int cb )
{
void *res = _nh_malloc( cb, 1 );

return res;
}
donkey0811 2005-08-24
  • 打赏
  • 举报
回复
new是函数吗???!!!面壁10分钟先!

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧