小编use*_*648的帖子

有人可以告诉我为什么我会在这个简单的C程序中出错吗?

在我结束第一次循环后,我继续遇到段故障,而对于我的生活,我不知道为什么.我正在扫描的文件只有18行中的18个字符串.我认为问题是我正在使用名为picks的双指针的方式,但我不确切知道为什么.我只是试图扫描长度小于15个字符的字符串,所以我没有看到问题.有人可以请帮助.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100

int main( int argc,char *argv[] )
{

   char* string = malloc( 15*sizeof(char) );
   char** picks = malloc(15*sizeof(char*));
   FILE* pick_file = fopen( argv[l], "r" );
   int num_picks;


   for( num_picks=0 ; fgets( string, MAX_LENGTH, pick_file ) != NULL ; num_picks++ )
     {
       scanf( "%s", picks+num_picks );
     }
   //this is where i seg fault
   int x;
   for(x=0; x<num_picks;x++)
     printf("s\n", picks+x);
}
Run Code Online (Sandbox Code Playgroud)

c malloc segmentation-fault

4
推荐指数
2
解决办法
241
查看次数

如何在动画期间更改CALayer的颜色?

我希望图层从绿色开始,然后慢慢变为黄色,橙色,最后是红色.我该怎么做呢?

CAShapeLayer *layer = [CAShapeLayer layer];
[layer setStrokeColor:[UIColor greenColor].CGColor];
[layer setLineWidth:5.0f];
[layer setFillColor:[UIColor clearColor].CGColor];

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:button.bounds cornerRadius:10.0f];
layer.path = path.CGPath;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.duration = 4.0f;

[layer addAnimation:animation forKey:@"myStroke"];
[button.layer addSublayer:layer];
Run Code Online (Sandbox Code Playgroud)

objective-c calayer ios

4
推荐指数
1
解决办法
7117
查看次数

您可以修改此BNF语法以始终包含奇数个狗吗?

您可以修改此BNF语法以始终包含奇数个狗吗?

<pets> ::= <pets> <pet> | <pet>
<pet>  ::= dog | cat
Run Code Online (Sandbox Code Playgroud)

“宠物”的示例:

    dog cat
    cat dog
    dog dog dog
    dog dog cat cat dog
    dog cat dog dog
Run Code Online (Sandbox Code Playgroud)

不是“宠物”的示例:

cat
dog cat dog
cat cat
Run Code Online (Sandbox Code Playgroud)

syntax parsing bnf

3
推荐指数
1
解决办法
609
查看次数

如何在PostgreSQL存储过程中使查询成为原子?

我认为如果一个进程从唯一的用户ID中选择余额并尝试执行插入,则会错误地更新余额,但是另一个进程会在此之前读取余额.我该如何解决?

CREATE OR REPLACE FUNCTION incBalance(INTEGER, BIGINT) RETURNS void AS $$   
DECLARE   
    balanceRecord record;
    newBalance bigint;  
BEGIN   
    FOR balanceRecord IN    
        SELECT balance FROM users WHERE userid = $1
    LOOP
        newBalance := balanceRecord.balance + $2;
        UPDATE users SET balance = newBalance WHERE userid = $1;   

    END LOOP; 
    RETURN;   
END;   
$$ LANGUAGE plpgsql;  
Run Code Online (Sandbox Code Playgroud)

postgresql concurrency atomic plpgsql

3
推荐指数
1
解决办法
3979
查看次数

比较函数必须返回int,不长

class C:
    def __init__(self,n,x):
        self.n = n
        self.x = x

a = C('a',1)
b = C('b',2)
c = C('c',3)

classList = [b,a,c]

for q in classList: print q.n,

classList.sort(lambda a,b: long(a.x - b.x))

for q in classList: print q.n,
Run Code Online (Sandbox Code Playgroud)

运行上面的代码会得到错误TypeError: comparison function must return int, not long.是否有另一种干净的方法来通过某些类变量对类对象进行排序?

python sorting

3
推荐指数
1
解决办法
1552
查看次数

如何在gevent streamServer中只输入一次SSL PEM密码短语?

每次客户端连接到服务器时,我都会收到提示输入PEM密码短语.我想只在服务器启动时输入它,而不必再次输入它.Twisted Matrix框架只需要启动时的密码短语,为什么不gevent?或者我使用gevent错了?如果我使用不需要PEM密码的证书,下面的代码工作正常,但我想使用带密码短语的证书.

from gevent.server import StreamServer
from gevent.pool import Pool
from gevent import monkey

class SocketPool(object):

    def __init__(self): self.pool = Pool(1000)

    def listen(self, socket):
        while True:
            line = socket.recv(1024)
            if not line: break 
            print line
            socket.close()
            break

    def add_handler(self, socket, address):
        print "connection made:", address
        if self.pool.full(): raise Exception("At maximum pool size")
        else: self.pool.spawn(self.listen, socket)

    def shutdown(self): self.pool.kill()

monkey.patch_all()
sockPool = SocketPool() 
server = StreamServer(('', 5000), sockPool.add_handler, keyfile='key.pem', certfile='cert.pem')
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)

ssl gevent python-2.7

3
推荐指数
1
解决办法
1475
查看次数

如何简单地读取由空格或空格分隔的标准输入的输入

您好我是在尝试学习python,在C++中从stdin读取字符串我就是这么做的

string str;
while (cin>>str)
    do_something(str)
Run Code Online (Sandbox Code Playgroud)

但在python中,我必须使用

line = raw_input()
Run Code Online (Sandbox Code Playgroud)

然后

x = line.split()
Run Code Online (Sandbox Code Playgroud)

然后我必须遍历列表x来访问每个str do_something(str)

这似乎是很多代码只是为了让每个字符串由空格或空格分隔所以我的问题是,有更简单的方法吗?

python stdin raw-input

2
推荐指数
1
解决办法
7967
查看次数

我可以使用像javascript这样的东西组合两个html按钮和他们的onclick方法吗?

<html> 
    <div class="button">
         <input type="button" onclick="method1();" id="1" value="Button1"/>
    </div>

     <div class="button">
         <input type="button" onclick="method2();" id="2" value="Button2"/>
     </div>
</html> 
Run Code Online (Sandbox Code Playgroud)

无需修改Html,我可以将两个按钮组合成一个按钮吗?单击该按钮时,是否可以调用这两个方法?可以用Javascript/JQuery完成吗?如果是这样,你会怎么做?

换句话说,我可以隐藏一个按钮并在不更改html的情况下触发两个方法.

html javascript jquery

2
推荐指数
1
解决办法
1518
查看次数

我应该使用什么网络库来编写服务器来处理300k以上的客户端?

我正在第一次尝试编写像Zynga Poker这样的iOS/Android扑克应用程序,我必须同时处理多达300,000个客户端连接,而且随着我的用户群增长,我还要做更多.客户将连接到未填充的房间,其中5-9个玩家与其他玩家互动,并且能够互相发送消息.我做了很多研究,似乎Twisted Matrix是事实上的标准,但我对gevent也很感兴趣,因为它使用协程而不是回调来处理异步编程.但令我担心的一件事是来自这个网站的程序员,http://news.ycombinator.com/item?id = 2288081写了这个

"这就是为什么我不明白为什么许多高端的实时网络创业公司(如Convore),仍然使用劣质解决方案,如Python eventlib或gevent,它们最多让它们处理数百到数千个客户端."

这是真的?所以我在使用哪个框架之间徘徊.或者那里有更好的东西吗?是300k连接要求python的大部分?如果是,我愿意用java或C++来做服务器,但我宁愿使用python.

asynchronous twisted gevent

2
推荐指数
1
解决办法
342
查看次数

如何在预准备语句中有两个SQL查询?

我想做这样的事情.

PREPARE addFriend(INTEGER, INTEGER) AS
    INSERT INTO friendRelation (u_id, friendid) VALUES ($1, $2),
    INSERT INTO friendRelation (u_id, friendid) VALUES ($2, $1);
Run Code Online (Sandbox Code Playgroud)

我在文档中没有看到这一点,所以要么不能做,要么我做错了.如果你在准备好的语句中不能有2个查询,那么我还不知道postgres的其他功能可以做到这一点吗?

sql postgresql prepared-statement

2
推荐指数
1
解决办法
1772
查看次数

在这种情况下,最好的数据结构和算法是什么?

我有一个程序,只是一个大循环.我首先有一个空集.在for循环的每次迭代中,我需要查看并从集合中删除最小值.同样在每次迭代中,我可以添加0到8个值的任何值(值是随机的).我应该使用哪种内置Java数据结构?我考虑使用ArrayList进行冒泡排序,然后取出第一个索引.我正在寻找最快的算法来完成这项任务.

java sorting algorithm data-structures

1
推荐指数
1
解决办法
400
查看次数

将目标C文件扩展名从.m更改为.mm会导致错误

最初我在LoginViewController.m中有包含下面警告的文件,我忽略了它.代码工作正常.

warning: sending 'LoginViewController *' to parameter of incompatible type 'id<NSStreamDelegate>'
Run Code Online (Sandbox Code Playgroud)

但现在我将文件扩展名更改为.mm(LoginViewController.mm).现在我无法再构建项目了,因为这个错误.

Cannot initialize a parameter of type 'id<NSStreamDelegate>' with an lvalue of type 'LoginViewController *'
Run Code Online (Sandbox Code Playgroud)

怎么了?

cocoa-touch protocols objective-c ios

1
推荐指数
1
解决办法
914
查看次数

稳定的匹配问题

我目前正在阅读算法的书,并遇到了稳定的匹配问题.一个问题浮现在脑海里,我很好奇,但这本书没有回答.在每个SMP中,总是可以有一对,每个最喜欢另一个?就像在经典的婚姻例子中一样.是否总有一对有一个女人和一个男人在哪里都排在彼此的首选?

algorithm proof pattern-matching stable-marriage

0
推荐指数
1
解决办法
520
查看次数