d**d 发帖数: 389 | 1 我用linux下面的POSIX timer, timer_create(),timer_settime(),
为什么在调用了timer_settime()以后,立马就有一个time-out callback? 然后再每过
5秒后有一个time out?
难道不是我调用timer_settime()以后,timer开始计时, 等到5秒以后再出现第一
time out callback 吗?
非常感谢!
代码如下:
#include
#include
#include
#include
#include
void
handle (sigval_t v)
{
time_t t;
char p[32];
time (&t);
strftime(p,sizeof(p),"%T ",localtime(&t));
printf("%s thread 0x%x,val=%d,signal captured.... 阅读全帖 |
|
J**********y 发帖数: 1891 | 2 #include "stdafx.h"
#include
#include
using namespace std;
class Base
{
public:
Base(){ cout << "Base-ctor"<
~Base(){cout << "Base-dtor"<
virtual void f(int) {cout << "Base::f(int)"<
virtual void f(double) {cout << "Base::f(double)"<
virtual void g(int i=10) {cout << "Base::g()"<
void g2(int i=10) {cout << "Base::g2()" << i<
};
class Derived : public Base
{
public:
Derived() {cout << "Derived-ctor... 阅读全帖 |
|
S**I 发帖数: 15689 | 3 you can initialize an array, then use that array to initialize the vector:
string a[] = {"ABC", "DEF", "GHI"};
vector A::s(a, a+sizeof(a)/sizeof(a[0])); |
|
S**I 发帖数: 15689 | 4 you can initialize an array, then use that array to initialize the vector:
string a[] = {"ABC", "DEF", "GHI"};
vector A::s(a, a+sizeof(a)/sizeof(a[0])); |
|
a***y 发帖数: 2803 | 5 int** myArray;
myArray = (int**) malloc(nrows * sizeof(int*));
for (int i = 0; i < nrows; i++)
myArray[i] = (int*) malloc(2 * sizeof(int)); |
|
t****t 发帖数: 6806 | 6 事实上, 原来的写法没有问题, 应该是可以通过的. int[2]是一个type, sizeof(int[2
])也没有问题.
但是如果用C++编译器(或者源文件后缀是.cpp/.C/.cxx之类), 需要作强制转换, 因为
malloc返回的是void*:
int (*myArray)[2] = (int(*)[2])malloc(nrows*sizeof(int[2])); |
|
c**********e 发帖数: 2007 | 7 【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q93 - Q95
发信站: BBS 未名空间站 (Fri Oct 14 23:32:57 2011, 美东)
C++ Q93: formal parameter T
What is the purpose of declaring the formal parameter T?
A. T specifies that any data sent to the function template must be of type T
B. T is a place-holder for a C++ data type
C. T can only hold a single data type int
D. T can hold any language data type
C++ Q94: Pointer
Multiple choices: Identify the true statements about the use of... 阅读全帖 |
|
j*a 发帖数: 14423 | 8 makes sense
sizeof(p)*3==sizeof(a) is true
space refered by a must be writable
or |
|
e****d 发帖数: 895 | 9 sizeof(p) is the size of the pointer, not char size.
also, sizeof(a) includes '\0' |
|
r***h 发帖数: 70 | 10 在看Programming interview exposed
不太明白里面讲link list插入新head的程序的例子.给出2个例子,书上说第一个是错的
,第二个才对.不明白为什么必须用**head而不是*head,难道head本身做为pointer pass
给函数不改变指针的内容吗? 请帮忙讲解一下,谢谢!
For example, the following code is incorrect because it fails to update the
head pointer in the calling function:
int BadInsert(element *head)
{
element *newElem;
newElem = (element *) malloc(sizeof(element));
if (!newElem) return 0;
newElem->next = head;
head = newElem;
return 1;
}
The correct way to update the head pointer in C is to pas... 阅读全帖 |
|
d****n 发帖数: 1637 | 11 malloc, calloc, realloc are in HEAP
alloca is in STACK
#####example####
buffered IO with char input
################
n=0;m=1024
char *s=(char *)calloc(m, sizeof(char));
char c;
while((c=getch())!=EOF){
if(n==m) s=(char *)realloc(s, (m<<=1),sizeof(char) );
*(s+(n++))=c;
}
*(s+n)='\0';
printf("%s\n", s);
free(s); |
|
m********r 发帖数: 334 | 12 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... 阅读全帖 |
|
o****e 发帖数: 916 | 13 here is an example:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct MyStruct
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(MyStruct));
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst = 256)]
public string name;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.I4)]
public int value;
} |
|
p*****e 发帖数: 53 | 14 实在看不出哪里不对,只能来这里求助高手帮忙看看了
非常感谢!!!
1.
int IsSecretPassword(char *ptrstring)
{
char temp[1024];
if(ptrstring)
{
strcpy(temp, ptrstring);
for (int i =0; i
{
temp[i] = toupper(temp[i]);
}
if (strcmp(temp, "TEST")==0)
return 1;
else
return 0;
}
return 0;
}
2.
int* MakeArray(int nsize)
{
int *ptr = 0;
if(nsize <=0)
return NULL;
ptr = (int*)malloc(sizeof(int... 阅读全帖 |
|
c***r 发帖数: 4631 | 15 俺系业余爱好的,上来玩玩。
1,
int IsSecretPassword(char *ptrstring)
{
if( null == ptrstring ) return 0;
int n = strlen(ptrstring);
if( n > 0)
{
char * temp = malloc(n+1);
int r;
strcpy(temp, ptrstring);
for (int i =0; i
{
temp[i] = toupper(temp[i]);
}
if (strcmp(temp, "TEST")==0)
{
r=1;
}
else
{
r = 0;
}
free(temp);
return r;
}
return 0;
}
2,
int* MakeArray(u... 阅读全帖 |
|
d****n 发帖数: 1637 | 16 C面试题
~~~~~~~~~~~
1.使用 #define 定义一个值为一年的秒数的常量,不考虑润年。
~~~~~~~~~~~
2.使用 #define 定义一个返回两个数中较小的一个的宏。
~~~~~~~~~~~~
3.将变量a定义成如下类型:
1. 有符号整数
2. 双精度浮点数
3. 指向一个有符号整数的指针
4. 一个十个成员的有符号整数数组
5. 一个函数指针,指向的函数返回类型为有符号整数,有一个有符号整数类型的参数
~~~~~~~~~~~~
4.C语言中的static的用处是?
~~~~~~~~~~~~
5. 写出下面函数被调用时的输出。
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") :
puts(" < = 6");
}
~~~~~~~~~~~~
6.写出下面程序的输出
#include
#include
typedef struct
{
char flag;
int value;
}SampleSt... 阅读全帖 |
|
t****t 发帖数: 6806 | 17 for one thing, the questions are assuming sizeof(int) and sizeof(T*) is
known, which is incorrect.
for another thing, IDE is not necessary for a compiler. |
|
t****t 发帖数: 6806 | 18 sizeof(int)是implementation defined. 但是你认为是1那99%是错的, 就算有sizeof(
int)==1的实现, 别人也得认为你是错的. |
|
p*********t 发帖数: 2690 | 19 俺的6的答案。
2 * sizeof (int)
sizeof(int)
6.写出下面程序的输出 |
|
d****n 发帖数: 1637 | 20 okay,没代码没真相。
here is my shabby codes and I want to share with you all.
output and analysis will be post soon
//file name matrix_op.c
#include
#include
#include
#include
#include
#include
#define _MX_INIT(type_t, row, col) ({ int i;\
type_t ** ret;\
ret=(type_t ** )malloc( row *sizeof(type_t*));\
for(i=0;i
(ret);\
})
#defin... 阅读全帖 |
|
h**********c 发帖数: 4120 | 21 Try to figure it out but failed,as the following:
void takefloat (float * ) {}
void takedouble (double *) {}
template
void dosomething (T * pt)
{
#if sizeof(T) == sizeof(float)
takefloat (pt);
#else
takedouble(pt);
#endif
}
Compilation failed. |
|
h**********c 发帖数: 4120 | 22 Try to figure it out but failed,as the following:
void takefloat (float * ) {}
void takedouble (double *) {}
template
void dosomething (T * pt)
{
#if sizeof(T) == sizeof(float)
takefloat (pt);
#else
takedouble(pt);
#endif
}
Compilation failed. |
|
l***y 发帖数: 4671 | 23 为了避免 copy 大数组 (实际运算是 1000-by-1000,并且大量重复访问),想用个指针
来指向它。
int main () {
static int A[10][10][10];
static int B[10][10][10];
static int C[10][10][10];
A[1][2][3] = 123;
B[1][2][3] = 1230;
C[1][2][3] = 12300;
int (*p)[10][10][10]; // line a
char cFlag;
cin >> cFlag;
while (cFlag == 'A' || cFlag == 'B' ||cFlag == 'C') {
switch (cFlag) {
case 'A':
p = &A;
break;
case 'B':
p = &B;
break;
case 'C':
p = &C;
break;
}
printf(... 阅读全帖 |
|
c****p 发帖数: 6474 | 24 代码:
int main()
{
int* p;
p = 0;
p = (int*)malloc(sizeof(int));
printf("%dbits: %x\n", sizeof(p), p);
*p = 10;
return 0;
}
结果:
8bits: 1bf3010
这个结果不能说明malloc的返回值是32位的int啊。 |
|
g***l 发帖数: 2753 | 25 下面这段代码中,为什么不能用static_cast把一个(float*)的指针强制转换成
(int*),而用 reinterpret_cast? 请问这是规定吗? 假设 sizeof(float) ==
sizeof(int).
[localhost]$ g++ -g sample.cpp -o sample
sample.cpp: In function ‘int main()’:
sample.cpp:13: error: invalid static_cast from type ‘float*’ to type ‘int
*’
在C中,可以这么转,gcc也会发warning,但是不会编译失败。
谢谢了。
1
2 #include
3 using namespace std;
4 int main()
5 {
6 int inumber=1000;
7 float fnumber=1234.56;
8
9 int* piaddr = &inumber;
10 float* pfa... 阅读全帖 |
|
l********a 发帖数: 1154 | 26 words是个指针,指向一个指针数组,sizeof出来是指针数组的长度
再除以每个指针的长度,就是数组元素的个数了
只有[]定义的数组才能求长度吧?要是words被定义成char **words这种,就不能sizeof
求出长度了.不知道对不对 |
|
p**o 发帖数: 3409 | 27 手写了一些C扩展,有些返回多重指针的函数不知道怎么用SWIG来包来供Python调用……
比如下面这个strsplit()函数,返回的是char**,怎么改才能让Python收到一个list (
of strings)?
http://www.swig.org/tutorial.html
我只是照tutorial简单地把函数声明抄进.i文件,Python中调用时返回的是
#include
#include
#include
/* Split an input string 'instr', using a set of given delimiters, to an
array of strings of at most 'maxparts' parts. */
char **strsplit (const char *instr, const char *delimiters, size_t maxparts)
{
char *... 阅读全帖 |
|
c**r 发帖数: 150 | 28
////////////////////////////////////////////////////////////////////
void node_insert(int Loc, struct node *pvlist, struct node *newvstr, int *
Clist)
//Clist记载了某个node对应的链表的长度
{
int i=1, clist=*Clist;
struct node *temp, *temp1;
temp=temp1=pvlist;
if(Loc==0){
if(temp->value==-1){
temp->value=newvstr->value;
}
else{
newvstr->next=temp->next;
temp=newvstr;
}
}
else{//问题出现在这!!
while(i
temp1=temp1->next;
i++;... 阅读全帖 |
|
c**r 发帖数: 150 | 29
////////////////////////////////////////////////////////////////////
void node_insert(int Loc, struct node *pvlist, struct node *newvstr, int *
Clist)
//Clist记载了某个node对应的链表的长度
{
int i=1, clist=*Clist;
struct node *temp, *temp1;
temp=temp1=pvlist;
if(Loc==0){
if(temp->value==-1){
temp->value=newvstr->value;
}
else{
newvstr->next=temp->next;
temp=newvstr;
}
}
else{//问题出现在这!!
while(i
temp1=temp1->next;
i++;... 阅读全帖 |
|
d****n 发帖数: 1637 | 30 又拓展了一下,给出parent link和buffed level computation
#include
#include
int *buff=NULL;
int buff_size=1024;
int buff_used=0;
int level(int query ){
while( buff[buff_used-1]
{
if ( buff_size <=buff_used ){
buff_size<=1;
buff=(int *)realloc( buff,sizeof(int)*buff_size );
}
buff_used++;
buff[buff_used]=(buff_used+1 )*(buff_used+1+1)/2;
}
... 阅读全帖 |
|
i*****o 发帖数: 1714 | 31 假定last是这个struct的最后一个member:(void*)A->last - (void*)A + sizeof(A->
last) == sizeof(A) 就说明里面有a,否则没有。
★ 发自iPhone App: ChineseWeb 7.7 |
|
i*****o 发帖数: 1714 | 32 假定last是这个struct的最后一个member:(void*)A->last - (void*)A + sizeof(A->
last) == sizeof(A) 就说明里面有a,否则没有。
★ 发自iPhone App: ChineseWeb 7.7 |
|
r***6 发帖数: 401 | 33 There must be some type indicator in the file to indicate the record type.
Further more, string has to be char *, and vector has to have a size. so
sizeof(B) and sizeof(C) varies. So you definitely need record separator/
indicator. |
|
t****t 发帖数: 6806 | 34 这个是对的, 简单的说就是character literal has type int or type char. 前面那
位说char的长度是int一样的, 不管从中文还是英文还是从语言的角度, 都是不通的.
实际上我猜他想说的是sizeof('a')==sizeof(int) (in C), 但是辞不达意. |
|
d****i 发帖数: 4809 | 35 如果只是要返回数组大小的话,可以这样吧:
void getptr(struct ID **ptr_array, int * num_of_array)
{
// some code here
*num_of_array = num();
}
In calling function:
struct ID *array[10];
int length;
getptr(array, &length);
这里假设num()是如下定义:
int num()
{
return sizeof(array)/sizeof(struct ID *);
}
就是 |
|
r*****e 发帖数: 792 | 36 malloc里边的sizeof不能一直都是sizeof(int)吧,应该有int**, int*吧。
跟你前面ptr cast应该一致。 |
|
m*********t 发帖数: 527 | 37 short signed int 和指针的长度不一样。你自己看看 sizeof(short signed int)
和 sizeof(any data pointer)。你这个情况下估计 short int 是 2, 但是指针是 4
(32
bit) 或者 8(64 bit). |
|
m*********t 发帖数: 527 | 38 It does not matter which data type the pointer is pointed to. So sizeof(T**)
== sizeof(T*) on modern computers. |
|
p*****w 发帖数: 429 | 39 这样可以自己定义吗
?
#ifndef int32
#if sizeof(long)==4
#define int32 long
#endif
#if sizeof(int)==4
#endif |
|
d****n 发帖数: 1241 | 40 undefined behavior可能是C里边最"邪恶"的一个东西了。。。
听到过一个很有意思的undefined behavior是关于整数溢出的,
signed integer overflow是一个常见的undefined behavior.
但是unsigned integer在某些情况下也会导致undefined behavior,
比如两个unsigned short相加,如果ABI规定sizeof(unsigned short)是16,
sizeof(int)是32, 那么C标准里的integer promotion规则会先把
unsigned short变成signed int, 然后就存在integer overflow的可能性了。呵呵 |
|
vi 发帖数: 309 | 41 奇怪的问题,谁帮忙看一下,谢谢!
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
list tlist(arr, arr+(sizeof(arr)/sizeof(int)));
int psum;
for_each(tlist.begin(), tlist.end(), [&psum](int &_val) {
_val += psum;
psum = _val;
});
for (auto _val: tlist) { cout << _val << " "; };
cout << endl;
cout << *(tlist.begin());
}
g++ default:
$2 4 7 11 16 22 29 37 46
2$
if compile with -O1 then it will produce the correct output:
$1 3 6 10 15 21 28 36 45
1$ |
|
d****n 发帖数: 1241 | 42 c99标准规定sizeof(int_type)是implementation defined的。
但是我感觉他问的不是sizeof(...)输出的差别
Ubuntu |
|
d****n 发帖数: 1241 | 43 * 首先,struct的大小依赖于两个因素,一是field的大小,二是field的alignment,
这两个都是由ABI决定的,比如在64位的机器上,如果编译器选择遵守gcc/llvm所支持
的x86_64 ABI标准,那么double是8byte对齐,如果在32位的机器上,linux下gcc支持
的x86 abi是4byte对齐. 大小的话,double类型都是8 bytes. 在缺省情况下,如果某
个field不符合对齐标准,那么编译器会插入padding bytes,让这个field符合对齐的
标准。比如:
struct S {
char f1; // size是1,对齐是1
int f2; // size是4,对齐也是4
};
sizeof(struct S)是8,因为编译器会在f1后,加入3个padding bytes, 让f2符合对齐
的规范。
* 如果在定义struct的时候,添加了类似packed的attribute, 那么编译器会忽略对齐
的要求。
* auto跟动态类型没有关系,在你的例子里,pointer类型是编译器在静态的时候,根
据一定的类型推导规... 阅读全帖 |
|
n*****t 发帖数: 22014 | 44 我们蓝翔技校水平差,码的程序肯定错误百出,大家不许笑。
单进程,也不用锁了。5M 票,320M request,原始数据都是偷 memory 的,测试结果是
3 秒不到。request 前 2 bit 是 opcode,最后几位是 ticket ID。
1、我这个没搜空闲票,先声明一下。
2、IO 会多占一点时间,呵呵,不过估计也就多几秒的时间,懒得整太复杂了。
3、HW 就不提了,丢脸,唯一能透露的是 virtualpc
#include
#include
#include
#define SIZE 5*1024*1024
#define LOOPS 64
unsigned char tickets[SIZE];
unsigned long *reqs;
unsigned char *results;
int main() {
int i;
unsigned char op;
unsigned char *ticket;
unsigned long *... 阅读全帖 |
|
i**p 发帖数: 902 | 45 I am trying to run tiny_tty in LDD3. When I use "cat /dev/ttty0" to read
from it, there is no output and the command is blocked.
Checking the trace, I notice both tty_insert_flip_char() and tty_flip_buffer
_push() are called. However, the data is not sent to the user by tty core.
Instead, it is sent back to the tiny_tty driver's tiny_write() callback
function. What is wrong there?
The kernel version is 2.6.32-61-generic.
static void tiny_timer(unsigned long timer_data)
{
struct tiny_serial *... 阅读全帖 |
|
m*********a 发帖数: 3299 | 46 void reverseWords(char *s) {
struct strStack{
char str[80];
struct strStack *next;
};
struct strStack *root,*tmp;
root=NULL;
int i;
while (*s!='\0'){
while (*s==' ') s++;
if (*s=='\0') break;
if (root==NULL){
root=malloc(sizeof(struct strStack));
root->next=NULL;
}
else {
tmp=malloc(sizeof(struct strStack));
... 阅读全帖 |
|
V*********r 发帖数: 666 | 47
你对python的“变量”、“赋值”(“传参”也算是赋值)的理解有偏差。
打个比方,下面的python代码
x = 1
y = x
x = 2.0
print(y) # output is 1
生硬地翻译成C语言,大致等效于
int *t1 = (int *) malloc(sizeof(int));
*t1 = 1;
void *x = t;
void *y = x;
float *t2 = (float *) malloc(sizeof(float));
*t2 = 2.0;
x = t2;
printf("%d", *y); |
|
V*********r 发帖数: 666 | 48
恰恰相反,Python赋值和“传参”,在Python语言层面,都是纯粹地传引用,不是传值;
在CPython实现中,其实是传(二级)指针,只不过指针的概念没有往上暴露给Python。
比如这段python代码,a-->b传参时,传给b的就是a这个符号所指示的那个对象的引用。
def modify(b):
b[1] = 9
a = [0,1]
modify(a)
print(a[1]) # output 9
Python“传参”其实就是赋值,完完全全地等效。上述代码等效于:
a = [0,1]
b = a
b[1] = 9
print(a[1]) # output 9
第一段Python代码的大致等效的C语言代码如下:
#include
void modify(void *b) {
int **c = (int **) b;
(*c)[1] = 9;
}
int main()
{
int *a = malloc(2 * sizeof(int));
a[0] = 0;
a[1] = 1;
modi... 阅读全帖 |
|
d****i 发帖数: 4809 | 49 Must be some errors in your Makefile.
I compiled using GCC 4.4 on CentOS without C++11 flag turned on. It returns
no error:
$ g++ -o dump dump.cpp
dump.cpp:
#include
#include //ostream_iterator
#include //cerr
#include //std::copy
template
void dump_to_file(const char* filename, const std::vector& v_d){
std::ofstream ofs(filename);
if(!ofs){
std::cerr<<"Unable to open the file to write!n";
return ;
}
std::... 阅读全帖 |
|
|