社区
C++ 语言
帖子详情
求New函数源码
oosky2004
2005-08-24 08:18:05
如题,想看看new函数是怎么实现的?
还有比如其他函数:printf()。。。
...全文
351
15
打赏
收藏
求New函数源码
如题,想看看new函数是怎么实现的? 还有比如其他函数:printf()。。。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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分钟先!
c++
源码
之 标准库
new
, operator
new
, placement
new
, array
new
在c++中管理内存的一些手段与细节 那么这篇博客的内容主要是在学习了jjhou老师的内存管理后我自己总结的一些知识点,关于侯捷老师的内存管理的内容可以自己搜索那么直接进入正题了 1.
new
和operator
new
依照bjarne的c++ programming language的11.2.3章节所说,
new
,delete,operator
new
,operator delete...
c++
new
运算符是如何调用构造
函数
的
内存申请和对象构造 本文内容简短,只为记录下一次思考过程,事情起源于一句话,“
new
操作符会调用operator
new
分配内存再调用构造
函数
构造对象”,但最近再次看到这句话的时候越看越有疑问,怎么样调用构造
函数
??于是就带着这个问题顺便看下operator
new
和placement
new
的
源码
加深下印象了。 众所周知构造
函数
是在实例化一...
FFmpeg
源码
:avformat_
new
_stream
函数
分析
avformat_
new
_stream
函数
分析
VS 2010 CRT ::operator
new
源码
分析
从我们写代码说起 C++面试常见的一个问题是:
new
与malloc的区别,然后你去网上搜索这个问题,常见回答如下: malloc : 只能单独给对象申请空间,不能进行构造
函数
的调用
new
: 不仅能申请动态空间,还能调用构造
函数
进行对成员变量初始化 我们此篇文章对哪一块
源码
分析呢?对,就是对
new
的分配内存的那部分
源码
进行分析。::operator
new
这是一个重载,重载的功能就只是...
js
源码
学习之
new
关键字
new
关键字实现与构造
函数
理解 & 箭头
函数
为什么不能作为构造
函数
C++ 语言
65,211
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章