d*k 发帖数: 207 | 1 请问这正常吗?是不是我的算法还不够好?
我用的方法是dfs,逐行枚举能够放下皇后的位置,对于每个位置,需要O(1)的时间判
断是否能够放下皇后。没有其他剪枝。更新buffer是把当前解存下来,是为了做N-
Queens时候用的。代码如下。
class Solution {
public:
void search(int row, int pos, int* buffer, bool* taken, bool* diag_left_
taken, bool* diag_right_taken, int* count, int row_num) {
if (row == row_num) {
*count += 1;
return;
}
if (pos == row_num) {
return;
}
int diag_left = pos - row + row_num;
int di... 阅读全帖 |
|
s*****a 发帖数: 72 | 2 这个方法不对,已经在 visual studio 2010 X64下面证实过。
sizeof(char) == 1
sizeof(int) == 4
sizeof的返回是以 byte 为单位的,不是以 bits |
|
z******g 发帖数: 271 | 3 #include
#include
#include
typedef struct trie {
int count;
const char *start;
int len;
struct trie *children[256];
} trie;
static inline trie *make_trie_node(const char *start, int len) {
trie *t, temp = {
.start = start,
.len = len
};
t = malloc(sizeof(trie));
memcpy(t, &temp, sizeof(trie));
memset(&t->children, 0, sizeof(t->children));
return t;
}
const char *most_frequent_substr(const char *str, int k, in... 阅读全帖 |
|
s*******1 发帖数: 33 | 4 去面试被要求用纯C写程序,不能有C++ syntax,我写的代码如下:
//Assume ids[] is to get specific_id in aliasID
//num is used to fix the length of ids[]
//alias is used to get the aliasID array
//aliasNum is used to get the length of array alias
/*
successFind = ture means return ids[] with length of num
successFind = false means return ids[] is null
or the length of ids[] is aliasNum (num > aliasNum)
*/
int* GetSomeIDs(int num, aliasID* &alias, int &aliasNum, bool& successFind)
{
successFind = true;
... 阅读全帖 |
|
z*********n 发帖数: 1451 | 5
也放松一下,贴个以前写的编译时间计算int to Roman number的C++ metaprogramming
, 学生时在实验室天天就玩这种东西了:
class R2I {
const static int DIG;
public:
constexpr static char IN[] = "MCDXLVI"; ///This is the INPUT number;
const static int OUT;
};
template struct ID {};
template <> struct ID <'I'> { static const int I = 0; static const int V = 1
; };
template <> struct ID <'V'> { static const int I = 1; static const int V = 5
; };
template <> struct ID <'X'> { static const int I = 2; static const int V =
10... 阅读全帖 |
|
z*********n 发帖数: 1451 | 6
new
你这问的是两码事。。
你手里有 &a,a是数组名对吧,比如int a[3],a是这么定义的吧?不是int *a = b(b是
一个数组,a是一个指针,指向b数组的某个元素),对吧?
如果你手里那个是&a,这不叫指向首元素的指针。。这叫指向数组的指针。。虽然他俩
值可能一样。
如果你有一个指向数组的指针,你当然可以直接用&a的地址减去 (&a-1)的地址来计算
数组长度了,这本质就是一个sizeof(a)。就跟int * p,对p来说,p和p-1的差值就是
一个sizeof(int)一样。
简而言之,如果你有&a,这么算就是数组大小:(int(&a) - int(&a-1)) / sizeof(int)
和main函数还是其他函数无关,main函数也就是一个特殊点的其他函数而已。 |
|
z*********n 发帖数: 1451 | 7
这么理解不完全对
先给你们补充几个基础知识吧:
int a[10];
意义:
a[0] 的类型是int
&a[0]的类型是int *
a 的类型是int[10],读作"10个int元素的数组"
&a 的类型是int (*)[10],读作“指向一个有10个int的数组的指针”
值:
a[0] 的值是a中第一个元素的值
&a[0]的值是a中第一个元素的地址
a 的值是a的地址,也等于a中第一个元素地址,规定。
&a 的值是数组a的地址,这个地址(记得我前面小组长的例子)碰巧是a[0]的地址。
所以你说a其实代表a[0]这个不对,这俩类型不一样,只是值碰巧一样。
a永远是这个叫做a的数组,不是只在&a-1中才是这个数组。
关于a-1:
a-1的类型是int *,因为数组名做减法无意义,隐式转化为int *后,减1.就跟你3/2.1
会自动升级成double类似。
另一个基本常识。
int * p
p和p-1之间地址差一个sizeof(int)
char *p
p和p-1之间地址差一个sizeof(char)
数组一样。
int (*p)[10] = &a;
p和p-1之... 阅读全帖 |
|
d**********o 发帖数: 1321 | 8 hw3b c-.y file
上面这一楼贴了载止hw3b deadline时我match的结果(也就是老师可以以这些不match
的ERROR为借口不给后来我补上的成绩),但是因为当时我还是没有写完,后来感恩节
期间就接着又写了一些,而且hw5是based on hw3 & hw3b的基础上(当我hw5是based
on更好的hw3的结果时,我应该可以得更多的分吧)。
hw4因为写得比较顺利,就不曾保留任何交上去作业的output,没有什么一目了然的结
果是我可以贴在这里的。原本我是想要把自己最的一次作业hw5贴出来的,但那已经是
一个完整的compiler,而且以后我还需要用自己的course project来找工作,所以一定
就不贴最终结果了。那就贴一个hw3b的c-.y文件吧,它集中的hw1、hw2、hw3、 hw3b的
结果,是我自己hw3b *.y文件的最完整版本。这些作业里面也有很多机关一一人为增加
的难度,比如那六七个IO相关的function,不仅traverse tree、build syntax tree的
时候会成为一个考点(把它们作为一个node连在syntax... 阅读全帖 |
|
d**********o 发帖数: 1321 | 9 hw3b c-.y file
上面这一楼贴了载止hw3b deadline时我match的结果(也就是老师可以以这些不match
的ERROR为借口不给后来我补上的成绩),但是因为当时我还是没有写完,后来感恩节
期间就接着又写了一些,而且hw5是based on hw3 & hw3b的基础上(当我hw5是based
on更好的hw3的结果时,我应该可以得更多的分吧)。
hw4因为写得比较顺利,就不曾保留任何交上去作业的output,没有什么一目了然的结
果是我可以贴在这里的。原本我是想要把自己最的一次作业hw5贴出来的,但那已经是
一个完整的compiler,而且以后我还需要用自己的course project来找工作,所以一定
就不贴最终结果了。那就贴一个hw3b的c-.y文件吧,它集中的hw1、hw2、hw3、 hw3b的
结果,是我自己hw3b *.y文件的最完整版本。这些作业里面也有很多机关一一人为增加
的难度,比如那六七个IO相关的function,不仅traverse tree、build syntax tree的
时候会成为一个考点(把它们作为一个node连在syntax... 阅读全帖 |
|
h*****n 发帖数: 439 | 10 first reason:
sizeof ( *.wav ) >> sizeof ( *.mp3 ) >> sizeof ( *.mid )
second reason:
some flash clips do not contain sound by themselves, they just
download sound from the web when necessary. |
|
x**********d 发帖数: 693 | 11 我想把CPU上的一个double数组拷贝靠GPU的complexdouble数组,然后处理完之再把GPU
的complexdouble数组拷贝到CPU的double数组。。
思路就是host的double数组赋值到host的complexdouble数组,然后host的
complexdouble数组memcopy到device的complexdouble数组,and the vice versa。。。
不知道哪里错了。。大神们给看下呗。。感激不尽。。
/////// Host to Device
double *h_data= new double[height*width];
cufftDoubleComplex *h_idata;
h_idata=new cufftDoubleComplex[width * height];
cufftDoubleComplex *d_idata;
cudaMalloc((void**)&d_idata,sizeof(cufftDoubleComplex)*width*height);
for(int i=0;i阅读全帖 |
|
c**t 发帖数: 2744 | 12 Two processes to merge data into same target/physical table:
A -> T; B -> T
SizeOf(A): 10K; SizeOf(B): 30K; SizeOf(T): 3 Million
As the tow processes running in parallel, it always cause deadlock:
Merge into T using A/B,
I am thinking to merge&commit A/B record by record to eliminate deadlock. Is
there any better way to avoid deadlock? |
|
t****t 发帖数: 6806 | 13 on linux gcc, you may use gcc -m32 to compile the program into 32 bit anyway
. you need to install 32 bit glibc though. 32/64 bit glibc don't conflict.
the most significant sizeof() change between gcc -m32 and gcc -m64 is the
sizeof(T*) and sizeof(long) is changed from 32 to 64.
other types, such as char, int, short, float, double, are not changed. |
|
S*A 发帖数: 7142 | 14
这个一个 project 一个样,这个 project 给的数据就是这么多。
要不用 python 呢,怎么快完成怎么来。
这个不需要测量 mmap. 因为 mmap 没有用过。
Please get your fact straight.
你的 slice object 20 bytes 是如何得出来的呢?先不说是 64 位系统
上面,就算 32 位 Python 都肯定是错的。你要算 sizeof (PySliceObject)
而不是 sizeof (PyObject).
我的系统上 64 bit. sizeof(PySliceObject) 目测一下是 48 byte. 我估算
50 byte 很离谱么? 你要硬说 48 << 50 那我也没有办法。
bigbuffer is byte array. node needs to be int array.
每次使用还转换一下。
tight.
所以我说这个改进不 work 嘛,如果 array.array 支持 mmap memory view
就 work 了。说了半天还是同样的问题,就是你说 Python 应该没有那么弱
的... 阅读全帖 |
|
w***g 发帖数: 5958 | 15 struct Numbers {
size_t N;
float data[1]; /* 有的编译器要求数组长度至少为1 */
};
写的时候先把N写进去,然后fwrite(xx.data, sizeof(float), xx.N, file);
读的时候先把N读出来,然后
xx = (struct Numbers *)malloc(sizeof(struct Numbers) + (N-1) * sizeof(float)
);
然后xx里的data就可以当N个元素的素组用了。 |
|
c***d 发帖数: 996 | 16 ☆─────────────────────────────────────☆
scorpin (scorpin) 于 (Thu Oct 4 15:16:26 2007) 提到:
How to check the size of an integer on a machine?
☆─────────────────────────────────────☆
OldMonk (old monk) 于 (Thu Oct 4 15:21:42 2007) 提到:
sizeof(int)?
☆─────────────────────────────────────☆
scorpin (scorpin) 于 (Thu Oct 4 15:23:35 2007) 提到:
我倒,就是问你怎么sizeof啊。
☆─────────────────────────────────────☆
kukutf (五脚蟹★酷酷豆腐) 于 (Thu Oct 4 15:25:54 2007) 提到:
sizeof是c关键字
☆──────────────────── |
|
f**********y 发帖数: 230 | 17 一个socket的小程序:
发送方:
int len = strlen(str);
write(sock,&len,sizeof(int));
write(sock,str,len);
接受方:
int len;
read(sock,&len,sizeof(int));
char* str=(char*)malloc(len*sizeof(char));
read(sock,str,len); |
|
h**o 发帖数: 548 | 18 Thanks, I have found the reason.
the reason is in func2(),I declared a char* A and xmalloc it, and then I
strlcpy(A, B, sizeof(B)) in which sizeof(B) is larger than sizeof(A), then
when func2 returns, it causes to fatal.
Thanks for your answer. |
|
s*****k 发帖数: 604 | 19 来自主题: Programming版 - 数组问题 有两个变量
char a[] = "Hello";
char *b = "Hello";
稍微了解指针和数组差别的人可以都知道这个区别
sizeof(a) = 6;
sizeof(b) = 4;
如果有这样一个函数,
void siz(char a[100])
{
printf("%d\n", sizeof(a));
}
请问调用这个函数为什么输出是 4? |
|
c**a 发帖数: 316 | 20 class A
{
int x;
};
class B
{
int y;
};
class C : public A, public C
{
int z;
};
sizeof(A) = 4
sizeof(B) = 4
sizeof(C) = 12
这都没问题。
但是,
C c;
B b;
B* p = &c;
C* q = &c;
B* p1 =&b;
p和q 居然都指向同一地址,
p->y; p1->b;
由于 c 是derived, 其内部layout 和 B 不一样,
我想象应该有 不同的 offset 来使其work。 但是不是这样。
p 和 p1 都分别指向 c 和b 的开始。
p->y; p1->b; 为什么都能正常工作呢?
而且 并没有 额外分配空间, 来在 runtime 实现 offset (像Vtable 那样)。 |
|
b***y 发帖数: 2799 | 21 可能是我没说清楚,第一句
void *rawMemory =
operator new[](10*sizeof(EquipmentPiece));
的目的是申请一块10*sizeof(EquipmentPiece)大小的RAW内存,可以直接用
void *rawMemory =
operator new(10*sizeof(EquipmentPiece));
用那个[]的目的是什么? |
|
X****r 发帖数: 3557 | 22 对于T* p的指针运算,p+n == (T*)((char*)p + sizeof(T)*n)
在这里a的类型是int [5],&a的类型就是int (*)[5],所以
ptr = (int*)(&a+1)
= (int *)((char*)&a + sizeof(int [5])*1)
= (int *)((char*)&a + sizeof(int)*5)
= a+5
即*(ptr-1) = *(a+4) = a[4] = 50 |
|
X****r 发帖数: 3557 | 23 qsort passes pointers to the objects being compared. Your objects
in the array are "char *", thus the comparison function parameters
have actual type "char **".
For example, if you're sorting integers, you would do:
int myintcmp(const void *p1, const void *p2) {
return *(const int*)p1 - *(const int *)p2;
}
int data[] = {1,3,2,4};
qsort(data, sizeof(data)/sizeof(data[0]), sizeof(data[0]), myintcmp);
Just replace "int" in above example with "char *" you'll see.
const*)str2); |
|
d*******n 发帖数: 524 | 24 我运行了下面的code
void f(int a[]) {
cout << "inside f:" << sizeof a << endl;
}
int main() {
int a[] = {2,3,4,5,6,7,8,9};
cout << "outside: " << sizeof a << endl;
f(a);
char e;
cin >> e;
return 0;
}
发现输出是:
outside: 32
inside f:4
也就是说在f内部用sizeof的话得到不是这个数组的size
那么在数组内部如何得到数组的size呢? |
|
h*****g 发帖数: 944 | 25 【 以下文字转载自 JobHunting 讨论区 】
发信人: huasing (Menlo Park), 信区: JobHunting
标 题: C++里get array size的问题
发信站: BBS 未名空间站 (Wed Jul 21 13:45:52 2010, 美东)
C++里是不是现在没有现成的get array size/length的function??
如果没有的话,我在网上看到了一个code
#include
template char (&array(T(&)[N]))[N];
#define length(a) (sizeof a / sizeof a[0])
int main()
{
int a[10];
std::cout << sizeof array(a) << '\n';
std::cout << length(a) << '\n';
}
请问这个code对所有的data type都work吗? 我试试了,好象是可以的。中间有什么错
误吗? |
|
d****p 发帖数: 685 | 26 Yes, I agree with you that sizeof(array)/sizeof(array_elem) is better.
Lz's code however technically is right - declare a function which takes in a
pointer to an array whose length is known at compile time and returns a
pointer to an array whose length is also known.
A pointer to an array is slightly different with an array from the
perspective of compiler though the sizeof operator will produce the same
result for them.
Basically you cannot return an array from a function but you can return a
p |
|
H***a 发帖数: 735 | 27 Can also use like:
/** 1D **/
int * foo( int size )
{
return malloc( sizeof(int) * size);
}
/** 2D **/
int ** foo( int row, int col )
{
int ** ptr = (int **) malloc( sizeof(int *) * row);
int i = 0;
for (i=0; i < row; ++i)
{
ptr[i] = (int *) malloc( sizeof(int) * col);
}
return ptr;
} |
|
E******T 发帖数: 59 | 28 float **x;
x = (float **)malloc(k*sizeof(float *)); /* Make space for the data */
in this example, sizeof(float *) and sizeof(float) has any difference? the
denotation * means a pointer?
Thanks |
|
s****n 发帖数: 700 | 29 unsigned int i;
i = 1 << (sizeof(i) * 8 - 1);
i will be overflowed, there is no problem if I use sizeof(i) - 2.
In the case i is 32bit, sizeof(i)*8 - 1 =31. How come it will overflow? |
|
d****n 发帖数: 1637 | 30 my shabby code for questions 4.
//File resovle.c
#include
#include
#include
#define MAXLINECHARSIZE 1000
inline int fsplit(char *line, char **items, char delim);
int main(int argc, char * argv[]){
/***prepare && allocate buffer/space***/
int n,nbytes=100, maxcol=10, maxchar=1000;
char **items;
{
items=(char **) malloc(maxcol*sizeof(char *));
int i;
for(i=0; i
items[i]=(char*)malloc(maxchar*sizeo... 阅读全帖 |
|
h*c 发帖数: 1859 | 31
C still use #define? no const int?
#define SEC_IN_A_YEAR (12333333320238)
这个问题很难,有bug
如果这么定义
#define min(x,y) ((x)<(y)? (x): (y))
int a;
double a;
int * a;
int a[10];
数
int (*f)(int);
static data
static function
<=6
sizeof (int) + sizeof(char)
sizeof(int)
3
undefined?
:-(
char const * p vs char p[] |
|
l***t 发帖数: 10 | 32 let me try:
~~~~~~~~~~~
1.使用 #define 定义一个值为一年的秒数的常量,不考虑润年。
#define sec_per_year (60*60*24*365)UL
~~~~~~~~~~~
2.使用 #define 定义一个返回两个数中较小的一个的宏。
#define MIN(a,b) ((a)<=(b)?(a):(b))
~~~~~~~~~~~~
3.将变量a定义成如下类型:
1. 有符号整数
int a;
2. 双精度浮点数
double a;
3. 指向一个有符号整数的指针
int *a;
4. 一个十个成员的有符号整数数组
int a[10];
5. 一个函数指针,指向的函数返回类型为有符号整数,有一个有符号整数类型的参数
int (*a)(int);
~~~~~~~~~~~~
4.C语言中的static的用处是?
~~~~~~~~~~~~
5. 写出下面函数被调用时的输出。
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") :
puts... 阅读全帖 |
|
A**u 发帖数: 2458 | 33 我觉得,你这个不行的原因在
#if sizeof(T) == sizeof(float)
sizeof(T)要运行时才知道
你改成
#if XXX
#else
#endif
编译时候加上 g++ -DXXX 选择float, g++ 选择double
或者
你改成void take (float * ) {}
void take (double *) {}
template
void dosomething (T * pt)
{
take(pt);
}
这样可行吗 |
|
A**u 发帖数: 2458 | 34 我觉得,你这个不行的原因在
#if sizeof(T) == sizeof(float)
sizeof(T)要运行时才知道
你改成
#if XXX
#else
#endif
编译时候加上 g++ -DXXX 选择float, g++ 选择double
或者
你改成void take (float * ) {}
void take (double *) {}
template
void dosomething (T * pt)
{
take(pt);
}
这样可行吗 |
|
F********g 发帖数: 475 | 35 /* add chars */
char test_str[32] = "Mr John Smith ";
void string_add_char(char *str)
{
unsigned int indx = 0;
char string_m[32];
//char *string_m;
while(*(str+indx) != '\0')
{
if(*(str+indx) != ' ')
{
indx++;
}
else
{
indx++;
if(*(str+indx) == ' ')
{
break;
}
else
{
strcpy(string_m,str+indx);
printf("%s\r\n",string_m);
indx += 2;
strcpy(str+indx,string_m);
*(str+indx-3*sizeof(char))='%';
... 阅读全帖 |
|
g****e 发帖数: 172 | 36 【 以下文字转载自 JobHunting 讨论区 】
发信人: gangle (nothing), 信区: JobHunting
标 题: 为什么要这样计算数中元素的个数?
发信站: BBS 未名空间站 (Tue Jun 19 11:06:22 2012, 美东)
char *words[] = {"stately", "plump", "buck", "mulligan"};
// calculate how many elements in words
size_t words_size = sizeof(words)/sizeof(char *);
为什么不是直接 size_t words_size = sizeof(words)? |
|
h**i 发帖数: 712 | 37 用 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... 阅读全帖 |
|
d****n 发帖数: 1637 | 38 献丑了,have fun!
#include
#include
#include
#include
#define IS_WHITE_SPACE(c ) ( isblank((c)))
char * replace_white_space( const char *source, size_t source_len){
size_t used=0, inc=0, allocated=source_len+1;
char *ret= (char *)malloc(sizeof(char)*allocated );
const char *s=source;
while(*s++){
(IS_WHITE_SPACE (*(s-1)))? (inc=3):(inc=1);
if ((used+=inc)>=allocated)
ret=(char *)realloc(ret, sizeof(char)*(alloc... 阅读全帖 |
|
y**********n 发帖数: 37 | 39 要把数据文件稍微处理一下,想写个简单的C程序弄一下。但编译不通过,真心求问。
很久没怎么碰过编程了,大侠不要笑话 :)
目的就是要申请一个三维数组 data_storage[125][125][200]
short signed int*** data_storage = NULL;
data_storage = (short signed int***)malloc(sizeof(short signed int)*125);
int i=0, k=0;
for(i=0; i<125;i++)
{
data_storage[i] = (short signed int**)malloc(sizeof(short
signed int)*125);
for(k=0; k<125; k++)
{
data_storage[i][k] = (short signed int*)malloc(sizeof(
short signed int)*200); ... 阅读全帖 |
|
r****o 发帖数: 1950 | 40 谢谢,你是说在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吗?会有什么问题呢?
正确的写法应该怎么写? |
|
S*A 发帖数: 7142 | 41 这个练习1 已经发现了,C10M 没有强壮的内存支持是
没戏的。单单是 kernel 部分就已经消耗很多内存了。
所以我们把目标调整一下,1M per 4G RAM。
这样 64G 内存就有可能实现 10M。
练习2 就是看看,我们如果保留 1M/4G 的空TCP
连接,可不可以。完全不往 TCP 里面发东西。就是
建立连接而已。这样也不存在 epoll 问题。
和实验1一样,我会发些拍脑瓜想出来的简单代码。
你直接用这个代码冲刺 1M/4G 会碰到些实际问题。
有兴趣的同学跟着做一下实验,看看有没有办法解
决这些问题。
BTW,我是实验出了 1M/4G (服务器方),所以是
有可能的。建立1M 连接还花了不少时间。
服务器方代码。端口是80, 大家可以自己改。
include
#include
#include
#include
#include
#include
#include
#include 阅读全帖 |
|
S*A 发帖数: 7142 | 42 拍脑瓜的简单客户端。假设端口是 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 | 43 这个练习1 已经发现了,C10M 没有强壮的内存支持是
没戏的。单单是 kernel 部分就已经消耗很多内存了。
所以我们把目标调整一下,1M per 4G RAM。
这样 64G 内存就有可能实现 10M。
练习2 就是看看,我们如果保留 1M/4G 的空TCP
连接,可不可以。完全不往 TCP 里面发东西。就是
建立连接而已。这样也不存在 epoll 问题。
和实验1一样,我会发些拍脑瓜想出来的简单代码。
你直接用这个代码冲刺 1M/4G 会碰到些实际问题。
有兴趣的同学跟着做一下实验,看看有没有办法解
决这些问题。
BTW,我是实验出了 1M/4G (服务器方),所以是
有可能的。建立1M 连接还花了不少时间。
服务器方代码。端口是80, 大家可以自己改。
include
#include
#include
#include
#include
#include
#include
#include 阅读全帖 |
|
S*A 发帖数: 7142 | 44 拍脑瓜的简单客户端。假设端口是 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****i 发帖数: 4809 | 45 Plain old vanilla pure C code. Disclaimer: copied from online
#include
#include
static void swap(void *x, void *y, size_t l) {
char *a = x, *b = y, c;
while(l--) {
c = *a;
*a++ = *b;
*b++ = c;
}
}
static void sort(char *array, size_t size, int (*cmp)(void*,void*), int
begin, int end) {
if (end > begin) {
void *pivot = array + begin;
int l = begin + size;
int r = end;
while(l < r) {
if (cmp(array+l,pivot) <= 0) {
... 阅读全帖 |
|
r*****8 发帖数: 2560 | 46 问题1. 为什么 sizeof(test))总是显示8?
问题2. *test 的大小从12变到36字母没问题吗?
#include
#include
void main(void)
{
char *test = "abcdefghijk";
printf("Size of test is %li \n", sizeof(test));
// 结果显示:8,应该是12啊,怎么会是8?
printf("The string test is %s \n", test);
// 结果打印出 "abcdefghijk"
test = "abcdefghijk abcdefghijk abcdefghijk";
printf("Size of test is %li \n", sizeof(test));
// 结果显示还是8,应该是36啊,怎么会是8?
printf("The stri... 阅读全帖 |
|
f****4 发帖数: 1359 | 47 struct packed_struct packed_data = {0};
packed_data.f1 = 45;
packed_data.f2 = 14;
packed_data.f3 = 0;
char buffer[2];
memcpy( buffer, &packed_data, sizeof(packed_struct));
struct packed_struct *ptr = (struct packed_struct *) buffer;
cout<< sizeof(packed_struct) <
cout<< sizeof(buffer) <
cout<f1<
cout<f2<
cout<f3< |
|
p*****2 发帖数: 21240 | 48
github
我看了,不就是我说的这样吗? 一个return。这就是你说的现代化C程序?
aeEventLoop *aeCreateEventLoop(int setsize) {
aeEventLoop *eventLoop;
int i;
if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
eventLoop->setsize = setsize;
eventLoop->lastTime = time(NULL);
eventLoop->timeEventHead = ... 阅读全帖 |
|
f**********d 发帖数: 4960 | 49 来自主题: Programming版 - c的问题 试了报错,其实我是要逐行读入文件,并把每一行用";"分隔成不同fields,再把
fields存入array里。
文件第一行是header,之后每行为 v1;v2;v3;v4;v5
现在*Words[j] = *pch;报错了,说access violation writing location 0xccccccccc.
fp = fopen("Data Files\ChestClinic.txt", "r");
if (fp == NULL)
{
printf("file is null");
}
i = 0;
while (fgets(line, sizeof(line), fp))
{
if (i == 0)
{
printf("The format of network structure file is:n");
printf("%sn", line);
... 阅读全帖 |
|
f**********d 发帖数: 4960 | 50 c里面不允许declare动态长度的array.
但因为我的array的长度是从文件中读入,事先并不知道array的长度。
所以我用以下方法:
float *MyArray = malloc(sizeof(float) * N); // N为array的长度的变量
for (i = 0; i < N; ++i)
{
MyArray[i] = Seg[i]; // Seg[N]为array内容的array.
}
但是这样做之后,sizeof(MyArray)为4,也就是一个float的长度。而如果指定N为常数
,比如2。
那么sizeof(MyArray)长度就是8。
两个问题:我这样定义事先不知道长度的array的方法是否正确,如果有问题,那么如
何解决? |
|