由买买提看人间百态

topics

全部话题 - 话题: struct
首页 上页 1 2 3 4 5 6 7 8 9 10 (共10页)
a***y
发帖数: 2803
1
来自主题: Programming版 - Questions about c code
1个int有4个byte,所以一个struct MATHEAD就有8个字节.
(MATHEAD *)(a)-1表示的是减去一个struct单位的地址,也就是减去8个字节.
这是指针类的加减法的特殊的地方.
h********n
发帖数: 1671
2
来自主题: Programming版 - C++中如何引用模板类中的模板函数
这个例子中,模板类B中引用了模板类A的模板函数f(),但是gcc无法识别这个用法。
template< typename T >
struct A
{
template< typename U >
int f (){ return 0; }
};
template< typename T >
struct B
{
A< T > a; // error
//A a; // ok
int g (){ return a.f< char >(); }
};
int main()
{
B< int > b;
return b.g();
}
U:\tmp\test.cpp: In member function 'int B::g()':
U:\tmp\test.cpp:15:31: error: expected primary-expression before 'char'
U:\tmp\test.cpp:15:31: error: expected ';' before ... 阅读全帖
t****t
发帖数: 6806
3
来自主题: Programming版 - please help debug this code
OK, solved. let me explain this, this is complex.
you called copy(....begin(), ....end(), ostream_iterator<...>(...)), so
ostream_iterator will do the << for you. naturally, ostream_iterator is
within namespace std, so operator<< is called in namespace std.
Now for overload resolution. there are a lot of operator<< within namespace
std; so these are considered. your parameters to operator<< are std::basic_
ostream<...> and std::pair<...>, they are both in namespace std. Therefore
no other namesp... 阅读全帖
l******9
发帖数: 579
4
I am trying to download and run the c code on Linux for
UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets
and XTI, Prentice Hall, 1998, ISBN 0-13-490012-X. It is by W. Stevens
Richard
http://kohala.com/start/unpv12e/unpv12e.tar.gz
But, when I build the code, I got error:
gcc -g -O2 -D_REENTRANT -Wall -c -o connect_nonb.o connect_nonb.c
In file included from connect_nonb.c:1:
unp.h:114: error: redefinition of âstruct in_pktinfoâ
make: *** [connect_nonb.o] Erro... 阅读全帖
s*****e
发帖数: 33
5
来自主题: Programming版 - What does the default constructor do?
Is there any difference between the following two implementations?
struct Foo
{
enum Enum
{
SIZE = 1024;
}

char m_buf[SIZE];
};
struct Foo
{
Foo()
{}
~Foo()
{}
enum Enum
{
SIZE = 1024;
}

char m_buf[SIZE];
};
You may get some surprise under gcc 3.4 or 4.1, see more detail:
http://streetprogrammer.blogspot.com/2011/12/default-constructo
t****t
发帖数: 6806
6
来自主题: Programming版 - What does the default constructor do?
我来给你看看真正的例子是怎么写的, 那个街头程序员明显什么都没搞清楚就在那里写
blog.
new, new什么? 有没有括号? 这都是不一样的.
当然C++在这个事情上是不intuitive, 也难怪大家抱怨.
#include
#include
using namespace std;
struct A {
int c[10];
};
struct B {
B() {};
int c[10];
};
int main()
{
/* fill the memory with garbage and release,
* just to show the difference */
int *g=new int[10000];
for (int i=0; i<10000; i++) g[i]=rand();
delete g;
A* a1=new A;
A* a2=new A();
B* b1=new B;
B* b2=new B();
c... 阅读全帖
m********r
发帖数: 334
7
来自主题: Programming版 - 问一个gcc下bit field的对齐问题
1)
#pragma pack(1)
typedef struct
{
unsigned int a:22;
unsigned int b:10;
unsigned int c:20;
unsigned char d:6;
unsigned char e:6;
unsigned int f:10;
unsigned char g:3;
unsigned char h:3;
} S1 ;
#pragma pack()
2)
typedef struct
{
unsigned int a:22;
unsigned int b:10;
unsigned int c:20;
unsigned char d:6;
unsigned char e:6;
unsigned int f:10;
unsigned char g:3;
unsigned char h:3;
} __attribute__ ((packed)) S2 ;
为什么sizeof(S1)=1... 阅读全帖
d****n
发帖数: 1637
8
来自主题: Programming版 - 问个构造函数的问题
define all parameters in a struct( I googled it.)
//B.h
typedef struct {
int p1,p2,p3,p4,p5.....
} param;
class B{
B(param){
//change param.p1
// change param.p2
// etc.
}
}
//in D.h
#include "B.h"
class D : public B{
D(/*D's private params*/):B( param b){}
};
pros and cons?
s*****w
发帖数: 215
9
来自主题: Programming版 - Marshal C++ struct to C# struct
或者比较弱的问一下C++中
这里为什么用char**
这和string有什么区别?
t****t
发帖数: 6806
10
来自主题: Programming版 - Marshal C++ struct to C# struct
you should ask the original author why he used char**.
usually string is declared as char*, or const char*.
you can consider char** as pointer to string, whether that makes sense to
you is beyond my knowledge. but pointer to string is a completely different
creature from string.
N********n
发帖数: 8363
11
来自主题: Programming版 - Marshal C++ struct to C# struct

Make sure no C++ exceptions ever get thrown into the C# runtime, or
your app could mysteriously crash. One problem of mixed-mode apps.
t*****n
发帖数: 4908
12
来自主题: Programming版 - Marshal C++ struct to C# struct
char**是指向char*指针。
g***i
发帖数: 4272
13
来自主题: Programming版 - 请教一个结构体占内存大小的问题
struct Node
{
int value;
struct Node* next[];
};
系统64位,为啥sizeof(Node)是8?那个没有标定个数的指针数组是怎么个情况?
g***i
发帖数: 4272
14
来自主题: Programming版 - 请教一个结构体占内存大小的问题
还有一点没太搞明白。
你说“即假设next为长度0的数组. 为什么是8呢? int 是4, 但是struct Node*是8字
节的指针, 需要对齐”
int是4, struct Node* 是8,对齐后是啥?
sizeof(Node)结果是8啊
是不是这个flexible array的size是0,对其int的4,结果是8?

.
t****t
发帖数: 6806
15
来自主题: Programming版 - 请教一个结构体占内存大小的问题
the sizeof struct is fixed. using flexible array member in another struct or
array is simply undefined.
X****r
发帖数: 3557
16
来自主题: Programming版 - forward declaration
You have to use the same keyword 'struct' or 'class' when
declaring an incomplete class type. Change 'class A;' in
header2.h to 'struct A;' and both headers can be included
together.
l******d
发帖数: 530
17
来自主题: Programming版 - 用c怎么实现generic stack (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: lwsOsgvd (lwsOsgvd), 信区: JobHunting
标 题: 用c怎么实现generic stack
发信站: BBS 未名空间站 (Tue Apr 24 16:42:43 2012, 美东)
用设计个stack,要求是The stack should be able to take as input a wide
variety of data types: it could range from byte sized to an n-byte sized
structure。
刚好在PIE书里面看到这个
typedef struct Element {
struct Element *next;
void *data;
} Element;
不知道算不算达到要求。
多谢!
J*****n
发帖数: 4859
18
来自主题: Programming版 - static polymorphism一问
template
struct base
{
void interface()
{
// ...
static_cast(this)->implementation();
// ...
}
};
struct derived : base
{
void implementation();
};
上面的程序如果用父类指针指向儿子,那似乎是如下的写法:
base *p = new derived()
这里的话,如果已经事先知道了所指向的对象是derived的,那么直接用derivaed的指
针去指就可以了。何必画蛇添足呢?
还是我的理解有问题?
请高手指点一下。
谢谢。
c*******y
发帖数: 1630
19
来自主题: Programming版 - C++ 菜鸟问一个关于template 的问题。
good, works for both
/usr/include/c++/4.6.3/bits/stl_iterator_base_types.h
/// Partial specialization for pointer types.
template
struct iterator_traits<_Tp*>
{
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef _Tp& reference;
};
/// Partial specialization fo... 阅读全帖
y****e
发帖数: 23939
20
来自主题: Programming版 - Visual C++ 高手帮忙,一个Link Error
首先,这个程序在Linux下用g++编译通过,没有任何问题。但是在Visual C++下编译时
,编译通过,但是出现下面链接错误:
error LNK2019: unresolved external symbol _gsl_vector_ptr referenced in
function "void __cdecl refalidf(struct gsl_vector const *,void *,struct gsl_
vector *)" (?refalidf@@YAXPBUgsl_vector@@PAXPAU1@@Z)
有这样两个函数:
1. static double refalifn(const gsl_vector * v, void *params)
2. static void refalidf(const gsl_vector * v, void *params, gsl_vector *
df)
问题出在第二个函数上。如果只有第一个函数,程序在VC++下也能编译链接通过。可是
这两个函数实在没有太大差别啊?难道是第二个函数有两个gsl_vector *指针?
谢... 阅读全帖
k**********g
发帖数: 989
21
template
struct add_op {
T operator () (const T& src0, const T& src1) const
{ return src0 + src1; }
};
template
struct sub_op {
T operator () (const T& src0, const T& src1) const
{ return src0 - src1; }
};
template
class vec_apply {
void operator () (T* v_out, T* vec_in, long len, ...)
{
oper op; // creates a oper object (functor) on stack - does not take
space, because it will be optimized away when compiler find... 阅读全帖
G*****7
发帖数: 1759
22
来自主题: Programming版 - 非虚函数里调用虚函数无效?
#include
using namespace std;
struct B
{
void fa() {fb();};
virtual void fb() { cout << __FUNCTION__; };
};
struct D: public B
{
void fb(){cout<<"what i want\n";};
} ;
void main()
{
B* d=new D;
d->fb();
d->fa();
}
Console output:
what i want
what i want
c**r
发帖数: 150
23
来自主题: Programming版 - 定义 单链表 数组,会不会很奇怪

谢谢,最后解决了。
void vstr_destroy(struct node *pvlist)
{
struct node *temp, *current;
current=pvlist->next;
pvlist->next=NULL;

while(current!=NULL){
temp=current->next;
free(current);
current=temp;
}
}
谢谢大家的回复和帮助!有什么问题我还会麻烦大家的
c**r
发帖数: 150
24
来自主题: Programming版 - 定义 单链表 数组,会不会很奇怪

谢谢,最后解决了。
void vstr_destroy(struct node *pvlist)
{
struct node *temp, *current;
current=pvlist->next;
pvlist->next=NULL;

while(current!=NULL){
temp=current->next;
free(current);
current=temp;
}
}
谢谢大家的回复和帮助!有什么问题我还会麻烦大家的
h**i
发帖数: 712
25
用 netstat -anp查看的
if((listenfd=socket(AF_INET,SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Socket Error: %s\a\n", strerror(errno));

exit(1);
}
on = 1;
ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

bzero((void*)&server_addr, sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htonl(9000);
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind(listenfd, (struct soc... 阅读全帖
p***o
发帖数: 1252
26
来自主题: Programming版 - 今天被一个面试问题难住了
这需求不是挺常见么, 也跟open source扯不上什么关系, 另外对齐方式估计也得检查。
Win32里面的解决方式就是检查struct的大小,比如说
typedef struct _SECURITY_ATTRIBUTES {
DWORD nLength;
LPVOID lpSecurityDescriptor;
BOOL bInheritHandle;
} SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;
Members
nLength
The size, in bytes, of this structure. Set this value to the size of the SEC
URITY_ATTRIBUTES structure.

does
does
w******p
发帖数: 166
27
来自主题: Programming版 - python question
several ways with s='0001001101111111'
''.join(x if i%4 or i==0 else '-'+x for i,x in enumerate(s))
'-'.join(['{0:04b}'.format(int(x,16)) for x in "%x"%(int(s,2))])
reduce(lambda x, y: x+'-'+y, [s[i::4] for i in range(len(s)/4)])
import re; re.sub(r'(....)', lambda x: '-'+x.group(0), s)[1:]
import struct; '-'.join(struct.unpack(">"+"4s"*(len(s)/4),s))
c****d
发帖数: 116
28
The packet is something like
struct packet
{
unsigned int a;
unsigned int b;
....
};
The size of the struct differs between 32-bit and 64-bit system.
how can I make it the same size across different platforms?
thanks
d**********x
发帖数: 4083
29
单继承只要你了解内存布局,struct里面放上另一个struct,用的时候直接cast即可。
如果要多继承的话,需要调整指针,c里面有没有这种做法我就不知道了
r****o
发帖数: 1950
30
来自主题: Programming版 - 问一个volatile和memcpy一起用的问题
谢谢,你是说在memcpy里面的参数里面加cast?
memcpy((volatile *)&a2, &a1, sizeof(s_t));
还有你看这样行不行?
typedef volatile struct
{
...
}s_t;
s_t a1;
s_t a2;
memcpy(&a2, &a1, sizeof(s_t));
这样a1,a2都是volatile,是不是就可以用memcpy了?
a1本来不必是volatile,但是加上volatile是不是也没有大问题?只是多了一点
overhead而已?

*
////////////
typedef struct
{
...
}s_t;
s_t a1;
volatile s_t a2;
...
//assume a1 is initiliazed with some value, and we want to copy its value to
a2
memcpy(&a2, &a1, sizeof(s_t));
这里能直接这么用memcpy吗?会有什么问题呢?
正确的写法应该怎么写?
o******y
发帖数: 12
31
来自主题: Programming版 - Java question
现在想用JAVA实现和远端通信,怎么定义message format on the wire.
For example, in C, the format is defined like this
struct CMD
{
uint16_t type;
uint16_t operation;
uint16_t field1;
uint16_t fields;
};
after populating this struct, I can call socket send to send out this
message.
In Java, What should I do to implement this ?
Thanks.
s*w
发帖数: 729
32
来自主题: Programming版 - how to destruct list with loop?
#include
using namespace std;
struct node {
int val;
node* next;
node(int v):val(v),next(NULL) {}
};
struct sll {
node* head;
sll():head(NULL) {}
~sll() {
cout << "sll destructing starts here" << endl;
node* p = head;
while(p) { // i am having problems destrucing here with looped list
node* todel = p;
p = p->next;
delete todel; // i thought delete make todel NULL so it breaks loop
todel = NULL; // does this help? nono... 阅读全帖
J*****n
发帖数: 4859
33
来自主题: Programming版 - C# binary file reading problem
I am trying to make a class to read and write list, where T is some
struct.
Here r the question:
Read method didn't work out:
An unhandled exception of type 'System.ArgumentException' occurred in
readbin.exe
Additional information: The output char buffer is too small to contain the
decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.
DecoderReplacementFallback'.
What is the solution?
Thank you.
class BinRW where T:struct
{
private static System.Type _Type;
... 阅读全帖
j******t
发帖数: 788
34
那就再套#define macro
#define P_a (struct A*)p_v
#define P_b (struct B*)p_v
#ifndef CAST_Pv
#define CAST_Pv(type) \
if...
P_a
or P_b
#endif
然后用CAST_Pv(type).
x******a
发帖数: 6336
35
来自主题: Programming版 - 请教C++11
在visual studio express 2012里为什么compile不过去?
#include
#include
#include
#include
int main()
{
std::vector vec{3, 4, 2, 9, 15, 267};
for(auto& elem:vec) elem*=3;
std::ostream_iterator out_it(std::cout, ", ");
std::copy(vec.begin(), vec.end(), out_it);
return 0;
}
Error 1 error C2601: 'vec' : local function definitions are illegal
c:users。。documentscodetest11test11main.cpp 8 1 test11
Error 2 error C2143: synt... 阅读全帖
N*n
发帖数: 456
36
来自主题: Programming版 - Taobao TFS 架构及开源项目
看不太懂前两句。
淘光节是2010 11/11 开始的。
秒杀不清楚何时开始。
in-line data.简单查了一下
Inline Data
The inline data feature was designed to handle the case that a file's data
is so tiny that it readily fits inside the inode, which (theoretically)
reduces disk block consumption and reduces seeks. If the file is smaller
than 60 bytes, then the data are stored inline in inode.i_block. If the rest
of the file would fit inside the extended attribute space, then it might be
found as an extended attribute "system.data" within the... 阅读全帖
n****1
发帖数: 1136
37
你在java界可能很牛,但也太自以为是了吧.
C的struct完全可以做到hidding和encapsulation, 这玩意叫Opaque struct. 看看这个
http://en.wikipedia.org/wiki/Opaque_pointer
现在的开源C程序里面到处都用的这个.
我看你是写java写得太high了,连基础老本都忘了
a****n
发帖数: 1887
38
准确的说, 为什么C++里面既有struct, 又有class,
因为他们在概念上是不同的东西。。。
class 是hide data, expose behaviors, struct 是 expose data
这种实现上的技巧更类似于语言上的hack
n****1
发帖数: 1136
39
比较新的开源C项目里边, dependency都是直接写
struct xxx;
而不是
#include "xxx.h"
如果xxx需要暴露某个public变量, 大家也会写
struct {
int public;
void* priviate;
} xxx;
而不是无脑地 #include "xxx.h"
然后函数指针在开源程序里面到处都是, 不知道你们的项目里用了多少.
g*****g
发帖数: 34805
40
你丫脑残了就打补丁,LOL。尼玛前面提的是struct,一个完全没encapsulation的结构
。一打脸就变成
opaque pointer了。你说 opaque pointer有encapsulation是一回事,struct啥时候跟
opaque pointer
等价了?还有脸谈基础知识。
n****1
发帖数: 1136
41
Can you given give me a precise definition of opaque pointer? It is a
pointer to some struct, right?
Why should people call it opaque pointer if it does not point to a opaque
struct?
Of cause you can assume me to be stupid enough to do that, then you win.
g*****g
发帖数: 34805
42
你分明没提opaque pointer, 提了一下C也有struct然后就大谈OO特性了。正常人提
struct,不会把它跟opaque pointer等价。
g*****g
发帖数: 34805
43
你还没完了,总之提encapsulation,拿C的struct做例子,就是2B行为。不如先去读读
struct的定义再来。
d****i
发帖数: 4809
44
来自主题: Programming版 - mutating input argument不应该鼓励吧
你这个两个貌似都没问题吧,就好比C里面经常有
int foo(struct Person *pp, const struct Name *pn);
前一个mutable(一般既是输入也是输出),后一个immutable(只是输入)。
S*A
发帖数: 7142
45
来自主题: Programming版 - C10M 练习2: 空TCP 连接,1M per 4G RAM
拍脑瓜的简单客户端。假设端口是 80, 启动指定服务器 IP 地址,
不用域名。当然会碰到一个 IP 地址只能发出去 65K 个连接的限制。
如何用 ip alias 使用多个 IP 地址解决这个问题留给大家自己改。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void die(char *reason)
{
perror(reason);
exit(1);
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in... 阅读全帖
S*A
发帖数: 7142
46
来自主题: Programming版 - C10M 练习2: 空TCP 连接,1M per 4G RAM
拍脑瓜的简单客户端。假设端口是 80, 启动指定服务器 IP 地址,
不用域名。当然会碰到一个 IP 地址只能发出去 65K 个连接的限制。
如何用 ip alias 使用多个 IP 地址解决这个问题留给大家自己改。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void die(char *reason)
{
perror(reason);
exit(1);
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in... 阅读全帖
d*******r
发帖数: 3299
47
来自主题: Programming版 - serialization 到底该怎么理解啊?
C 就是用来干这个的呀,这个是协议打包的网络程序。
简单说,就是内存或者硬盘其实都是一个长长的一维数组,你要在上面申请一段连续空
间(或者更复杂,一个链表),把你的数据 (e.g. data 指针指向的 buffer 里的数据写
进去)。然后,在接受端,你再按照你存储的格式,把数据导来。
你可以在这个struct后面加一个buffer field,存数据,用 memcpy之类的函数把数据
copy 进去,用一个指针存起始地址, 还有个变量存数据buffer field 的长度。然后用
TCP socket 把整个 struct 传过去。
或者你们要求是把数据存到 file 里面,然后传输 file (e.g. scp?)?

样:
i**p
发帖数: 902
48
来自主题: Programming版 - 并口驱动的一个问题 (转载)
【 以下文字转载自 Linux 讨论区 】
发信人: isup (No), 信区: Linux
标 题: 并口驱动的一个问题
发信站: BBS 未名空间站 (Wed May 21 21:03:59 2014, 美东)
最近编译运行了《Essential Linux Device Drivers》第5章的例子,
Driver for the Parallel LED Board (led.c)。程序不复杂。其中有一个函数led_
attach(struct parport *port) 是由parport_register_driver(&led_driver)注册的。
请问,led_attach()是何时被调用的?或者说怎么引起系统调用这个函数?
static void led_attach(struct parport *port)
{
/* Register the parallel LED device with parport */
pdev = parport_register_device(port, DEVICE_NAME,
led_preempt, NU... 阅读全帖
D***n
发帖数: 6804
49
来自主题: Programming版 - 今天给c++震惊了
你需要知道编译器在看到你这段话的时候真正干了啥。
1)当编译器看到const的时候,它知道这个东西是不会改变的。
2)当编译器看到static的时候,它知道要把这个东西放到一个特殊的内存区域,并且
在程序载入的时候初始化(理解这点特别重要)。
一个东西又是const又是static,理论上来说,应该在内存静态变量区创建一个变量,
把具体数字塞进去,然后其他人在访问结构的时候可以通过地址访问它....
但是实际上,编译器会在相应的struct的代码段上直接计算出常量并且写进去(内存访
问两次跳跃),一个在代码段的常量当然无法赋值。
你那个程序的main实际上是这样的:
int main() {
A test;
int a=10;
10=15; /* 错误 */
}
理解了这一点你可以省掉那些 C++书上一万个blah, blah, blah

f()
你的f().x是lvalue
看看我的const static int 是不是rvalue
不能进行test.y的赋值
我不知道是因为const还是因为rvalue
struct A {
const static int y=... 阅读全帖
c*******0
发帖数: 5247
50
来自主题: Programming版 - go也是三种paradigm混合的语言

就是最直白的method用法,Go不算正统oop,也就是struct包struct可以搞类似于继承
的概念而已。你必须要method才能做到interface,所以其实method更多是用来给
interface用的。
你要想看method idiomatic的用法,http库是最好的选择
http://golang.org/pkg/net/http/
首页 上页 1 2 3 4 5 6 7 8 9 10 (共10页)