问题列表 - 第30439页

获取函数参数的地址是否合法?

这个代码片段是否在ANSI C中定义良好?在我的系统(Linux x86_64)上,它似乎运行得很好并打印一个地址,但总是这样吗?例如,参数可能通过寄存器传递,并且获取该地址似乎不正确.

#include <stdio.h>

void foo(int a)
{
   printf("%p\n", &a);
}

int main(void)
{
   foo(42);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:看起来GCC会在获取地址之前将寄存器传递的值放入堆栈.

<foo>:
  55                      push   rbp
  48 89 e5                mov    rbp,rsp
  48 83 ec 10             sub    rsp,0x10
  89 7d fc                mov    DWORD PTR [rbp-0x4],edi
  b8 1c 06 40 00          mov    eax,0x40061c
  48 8d 55 fc             lea    rdx,[rbp-0x4]
  48 89 d6                mov    rsi,rdx
  48 89 c7                mov    rdi,rax
  b8 00 00 00 00          mov    eax,0x0
  e8 d8 fe ff ff          call   4003c0 <printf@plt> …
Run Code Online (Sandbox Code Playgroud)

c function

11
推荐指数
2
解决办法
2922
查看次数

面试问题 - 根据规则将文本拆分为子字符串

根据以下规则将文本拆分为子字符串:

  • a)每个子串的长度应小于或等于M.
  • b)如果子字符串包含任何数字字符,则子字符串的长度应小于或等于N(N <M)
  • c)子串的总数应尽可能小

我不知道如何解决这个问题,我猜它与"动态编程"有关.任何人都可以帮我用C#或Java实现它吗?非常感谢.

algorithm

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

如何在不单击提交按钮的情况下提交表单?

我希望在用户选择其中一个单选按钮选项时提交表单.我知道如何使用选择字段执行此操作:

<select name='something' onchange="this.form.submit()">
Run Code Online (Sandbox Code Playgroud)

但如何用单选按钮做到这一点?

javascript

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

Oracle - 使用前导或尾随空格查找值

我试图找出某个列是否需要TRIM函数.

如何确定表中的此列是否具有在实际数据之前或之后具有空格的记录.

sql oracle

7
推荐指数
2
解决办法
3万
查看次数

使用自定义哈希函数创建一个namedtuple

说我有namedtuple这样的:

FooTuple = namedtuple("FooTuple", "item1, item2")
Run Code Online (Sandbox Code Playgroud)

我希望以下函数用于散列:

foo_hash(self):
    return hash(self.item1) * (self.item2)
Run Code Online (Sandbox Code Playgroud)

我想要这个,因为我想要item1item2无关的顺序(我将对比较运算符做同样的事情).我想到了两种方法来做到这一点.第一个是:

FooTuple.__hash__ = foo_hash
Run Code Online (Sandbox Code Playgroud)

这有效,但感觉被黑了.所以我尝试了子类化FooTuple:

class EnhancedFooTuple(FooTuple):
    def __init__(self, item1, item2):
        FooTuple.__init__(self, item1, item2)

    # custom hash function here
Run Code Online (Sandbox Code Playgroud)

但后来我明白了:

DeprecationWarning: object.__init__() takes no parameters
Run Code Online (Sandbox Code Playgroud)

那么,我该怎么办?或者这是一个坏主意,我应该从头开始编写我自己的课程?

python inheritance overriding tuples

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

使用Ada进行软件工程:存根; 单独和编译单位

我有机械工程背景,但我有兴趣与Ada学习良好的软件工程实践.我有几个问题.

Q1.如果我理解正确,那么有人可以编写一个包规范(ads)文件,编译它然后编译使用该包的主程序.稍后,当人们知道要在包体中包含什么时,可以编写和编译后者.之后,现在可以运行主程序.我试过这个,我想确认这是一个好习惯.

Q2.我的第二个问题是关于存根(子单元)和SEPARATE的使用.假设我有一个主程序如下:

    WITH Ada.Float_Text_IO;
    WITH Ada.Text_IO;
    WITH Ada.Integer_Text_IO;

    PROCEDURE TEST2 IS
    A,B      : FLOAT;
    N        : INTEGER;

    PROCEDURE INPUT(A,B: OUT FLOAT; N: OUT INTEGER) IS SEPARATE;

    BEGIN -- main program
      INPUT(A,B,N);
      Ada.Float_Text_IO.Put(Item => A);
      Ada.Text_IO.New_line;
      Ada.Integer_Text_IO.Put(Item => N);
    END TEST2;
Run Code Online (Sandbox Code Playgroud)

然后我在单独的文件中有过程INPUT:

separate(TEST2)
PROCEDURE INPUT(A,B: OUT FLOAT; N: OUT INTEGER) IS
   BEGIN
      Ada.Float_Text_IO.Get(Item => A);
      Ada.Text_IO.New_line;
      Ada.Float_Text_IO.Get(Item => B);
      Ada.Text_IO.New_line;
      Ada.Integer_Text_IO.Get(Item => N);
   END INPUT;
Run Code Online (Sandbox Code Playgroud)

我的问题:

a)AdaGIDE建议我将INPUT过程文件保存为input.adb.但是在编译主程序test2时,我收到警告:

warning: subunit "TEST2.INPUT" in file "test2-input.adb" not found
cannot generate code for file …
Run Code Online (Sandbox Code Playgroud)

compilation ada stub

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

数据库:多个表还是只有一个表?

For example我有photosvideos表,我可以评论这些,但当我发送到数据库哪个方式更好?

  1. 要有2个评论表: photo_commentsvideo_comments

  2. 或者有一个表comments并在表格中创建一个行 type,如果它是a photo_comment或者那么放在那里 video_comment

我认为1速度更快,因为当我需要查询表时,我的数据更少,但可能2更容易使用.

请让我知道什么是最好的方式,速度对我来说非常重要.

我说的是一个拥有数百万条数据,数百万条评论的非常庞大的系统,所以我想要以最快的方式获得结果,对我来说无关紧要,如果我需要更多代码或者需要记住一些东西加上,结果更重要!

database performance database-design comments

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

检查是否定义了复选框的更好方法是什么?

目前我正在使用

var email, fax, sms = false;  
    if($('#uemail:checked').val() != undefined)  
        email = true;  
    if($('#ufax:checked').val() != undefined)  
        fax = true;  
    if($('#usms:checked').val() != undefined)  
        sms = true;  
Run Code Online (Sandbox Code Playgroud)

但它写这么长的路.

有没有更好的方法来写这个?

javascript jquery

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

缩进在这里扮演什么角色?为什么一个缩进不起作用?

这是RWH书中的示例程序.我想知道为什么第一个工作很好但第二个甚至不能编译?唯一的区别是第一个使用2个标签,where mainWith func = do而第二个仅使用1.不确定这意味着什么区别?为什么第二个无法编译?还有为什么do construct可以空?

非常感谢,Alex

-- Real World Haskell Sample Code Chapter 4:
-- http://book.realworldhaskell.org/read/functional-programming.html

import System.Environment (getArgs)

interactWith func input output = do
    s <- readFile input
    writeFile output (func s)

main = mainWith myFunction
    where mainWith func = do
            args <- getArgs
            case args of 
                [fin, fout] -> do
                    interactWith func fin fout
                _ -> putStrLn "error: exactly two arguments needed"

myFunction = id


-- The following code has a compilation error …
Run Code Online (Sandbox Code Playgroud)

haskell indentation

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

C++:将char插入字符串

所以我试图将从字符串中获取的字符插入另一个字符串.在这里,我的行动:1.我想使用简单:

someString.insert(somePosition, myChar);
Run Code Online (Sandbox Code Playgroud)

2.我收到错误,因为insert需要(在我的情况下)char*或string
3.我通过stringstream将char转换为char*:

stringstream conversion;
char* myCharInsert;
conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters;
conversion >> myCharInsert;
someString.insert(somePosition, myCharInsert);
Run Code Online (Sandbox Code Playgroud)

一切似乎都在成功编译,但程序崩溃了

conversion >> myCharInsert;
Run Code Online (Sandbox Code Playgroud)

线.

5.我正在尝试用字符串替换char*:

stringstream conversion;
char* myCharInsert;
conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters;
conversion >> myCharInsert;
someString.insert(somePosition, myCharInsert);
Run Code Online (Sandbox Code Playgroud)

一切似乎都没问题,但是当它someAnotherString.at(someOtherPosition)变成空间时,程序会崩溃.

那么我该怎么做呢?

c++ string insert stringstream

21
推荐指数
1
解决办法
6万
查看次数