小编klu*_*utt的帖子

标准库中的某些函数看起来只是包装器或别名的原因是什么?

当我查看 glibc 的源代码时,我有时会偶然发现作为包装器的函数,它们什么也不做,只能作为别名使用。例如:

int
rand (void)
{
  return (int) __random ();
}
Run Code Online (Sandbox Code Playgroud)

出现这种情况的原因是什么?为什么不直接__random()把它的身体放进rand()去呢?

c

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

如何在不使用字符串的情况下将密码从 java 发送到 postgresql?

我知道这char[]String密码更可取,因为它String是不可变的。但是我一直无法弄清楚如何将它传递给准备好的语句。考虑这个代码:

Connection con = MyDBManager.connect();
PreparedStatement stm = con.prepareStatement(
    "INSERT INTO my_table(username, password) VALUES(? ?)");
String username = "Jonny";
stm.setString(username);
char[] password = new char[] {'a','b','c','d'};
stm.SetString(password.toString());
Run Code Online (Sandbox Code Playgroud)

它有效,但我怀疑调用会password.toString()破坏使用 a 的全部目的char[],那么我该怎么办?

是的,我知道密码不应该以明文形式存储。密码是散列的,但我仍然想使用char[]

这似乎有效,而且我还将数据库中的字段从 更改textbytea并且(我认为)理论上应该提供与char[]预期相同的好处,但我强烈感到我做得不对:

byte[] password = new byte[] {'a','b','c','d'};
stm.SetBytes(password);
Run Code Online (Sandbox Code Playgroud)

也许我的方法完全错误,但我的要求是密码哈希永远不应存在于不可变对象中。我对将其发送到数据库的其他方式持开放态度PreparedStatement

回复评论:

我从@rzwitserloot 的回答中学到了很多东西,但不幸的是它没有帮助。永远不要在不可变对象中使用密码哈希是不可协商的要求。

java passwords postgresql password-protection

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

#include <lib.h>给出符号未找到,为什么?

我有这个代码:

#include <iostream>
#include <mp4.h>

int main (int argc, char * const argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";

    MP4Read("filename", MP4_DETAILS_ALL );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我已经添加了-I/opt/local/include和-L/opt/local/lib到路径(通过macports安装后mp4库所在的位置),但我得到的是:

未定义的符号:"_ MP4Read",引自:main.o中的_main ld:未找到符号

即使XCode找到它并自动完成正确...

c++ xcode

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

为什么我在尝试使用.c文件中的头文件时遇到类型错误?

我真的不明白这里发生了什么.我试图访问.c文件中的结构的成员,但是当我尝试访问struct变量时它给出了'error-type'.有人知道这里发生了什么吗?

CPU.h头文件:

#ifndef _CPU_H
#define _CPU_H

#include <stdint.h>

typedef struct cpu_registers
{
    union
    {
        struct
        {
            uint8_t f;
            uint8_t a;
        };
        uint16_t af;
    };
    union
    {
        struct
        {
            uint8_t c;
            uint8_t b;
        };
        uint16_t bc;
    };
} cpu_registers;

#endif /* _CPU_H */
Run Code Online (Sandbox Code Playgroud)

CPU.c文件:

#include "CPU.h"

cpu_registers regs;
regs.af = 0xFFFF;
Run Code Online (Sandbox Code Playgroud)

以下是使用clang编译时出现的错误:

CPU.c:4:1: error: unknown type name 'regs'
regs.af = 0xFFFF;
^
CPU.c:4:5: error: expected identifier or '('
regs.af = 0xFFFF;
    ^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)

c struct

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

在c中用2个未知参数求解方程的最快算法?

我正在计算x和y的可能组合的值.它可以工作,但是当我输入大数字时,它需要太长时间.你对更好的算法有什么想法吗?

ax + by = c

程序的输入是a,b和c,它们应该是非负数.我的代码看起来像这样:

int combs=0;
for(int x=0; x < c; x++) {
    for(int y=0; y < c; y++) {
        if( (a*x) + (b*y) == c) {
            combs++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c algorithm equation

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

这种随机化的实现在 C 中是如何工作的?

自从我在https://www.tutorialspoint.com/c_standard_library/c_function_rand.htm上找到这个特定的文档以来,我一直在考虑这行特定的代码。srand((unsigned)time(&t));每当我不得不生成一些东西时,我使用它srand(time(NULL))是为了不生成同样的东西,每次我运行程序,但是当我遇到这个来了,我一直在想:是否有任何区别srand((unsigned)time(&t)),并srand(time(NULL))因为对我来说,他们看起来他们做同样的thing.Why是使用的time_t变量,为什么是?地址运算符用于srand()

#include <stdio.h>
#include<stdlib.h>
int main(){
    int i,n;
    time_t t;

    n = 5;

    srand((unsigned)time(&t)); 

    for (i = 0; i < n; i++) {
        printf("%d\n", rand() % 50);
    }
return(0);
}
Run Code Online (Sandbox Code Playgroud)

c srand

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

即使按值传递,多维数组值也会被修改

我通常将2D数组(按值)传递给函数“ elem”,然后将其进一步传递给另一个函数“ interchange”,该函数执行行交换操作并显示它。但是问题是,在我从交换返回到main()之后,数组的值已从交换更改为结果数组,即使从技术上讲它们对于三个不同的函数(main,elem和interchange)必须是三个不同的变量)。为什么会这样,我该怎么做才能使main()中的数组保持不变?

//include files... 

void interchange(float c[10][10],int m,int n)
{
    int i,j,p,q;
    float temp;

        printf("\nEnter the two row numbers to interchange:");
        scanf("%d%d",&p,&q);

        if((--p<m)&&(--q<n))
        {
            for(i=0;i<m;i++)
            {
                temp=c[p][i];
                c[p][i]=c[q][i];
                c[q][i]=temp;
            }
        } else
        {
            printf("Row numbers must be less than matrix order.\n");
            return;
        }

    printf("\nResultant matrix is:\n"); //print the array in interchange,c
    printf("\n");
    for(i=0;i<m;i++)
        {for(j=0;j<n;j++)
            {
                printf("%f\t",c[i][j]); 
            }
         printf("\n");
        }
}

void elem(float b[10][10],int m,int n)
{
    int ch;
    do
    {
        printf("\n1:Row interchange\t 2:Exit elementary transformations\n\nEnter the choice:");
        scanf("%d",&ch);  //get …
Run Code Online (Sandbox Code Playgroud)

c pass-by-reference multidimensional-array

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

在循环主体中使用逗号运算符

我试图用两种不同的方式编写一个while循环,并注意到在两次条件下使用','不能按我认为的方式工作。我只是对实际发生的事情感到好奇。这是代码:

#include <stdio.h>

int main()
{
    int i = 0, lim = 1000, c;
    char s[lim];
    while (i < lim - 1, (c = getchar()) != '\n')
    {
        if (c != EOF)
        {
            s[i] = c;
        }
        else
        {   
            break;
        }
        ++i;
    }
    printf("%s\n", s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>

int main()
{
    int i = 0, lim = 1000, c;
    char s[lim];
    while (i < lim - 1, (c = getchar()) != '\n', c != EOF)
    {
        s[i] = …
Run Code Online (Sandbox Code Playgroud)

c loops while-loop

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

如果取消引用空指针,在 CPU 级别会发生什么?

假设我有以下程序:

#include <signal.h>
#include <stddef.h>
#include <stdlib.h>

static void myHandler(int sig){
        abort();
}

int main(void){
        signal(SIGSEGV,myHandler);
        char* ptr=NULL;
        *ptr='a';
        return 0;
}

Run Code Online (Sandbox Code Playgroud)

如您所见,我注册了一个信号处理程序和一些行,我取消引用了一个空指针 ==> 触发了 SIGSEGV。但它是如何触发的?如果我使用strace(输出剥离)运行它:

//Set signal handler (In glibc signal simply wraps a call to sigaction)
rt_sigaction(SIGSEGV, {sa_handler=0x563b125e1060, sa_mask=[SEGV], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7ffbe4fe0d30}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
//SIGSEGV is raised
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---
rt_sigprocmask(SIG_UNBLOCK, [ABRT], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, ~[RTMIN RT_1], [SEGV], 8) = 0
Run Code Online (Sandbox Code Playgroud)

但是缺少一些东西,信号如何从 CPU 传递到程序?我的理解:

[Dereferences null pointer] -> …
Run Code Online (Sandbox Code Playgroud)

c linux cpu assembly signal-handling

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

为什么不重新分配指向 const int 的指针会导致编译错误?

这是一个来自测试的问题。

int main() {
  const int i=1;
  const int *p=&i;
  int j=2;
  const int *q=&j;
  j=3;
  printf("%d", *p+*q);
  int k = 0;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

据我了解,双方pq都指向一个const int

我认为当我声明指向非常量值的指针时会出现编译错误,但它工作正常;这是为什么?

c pointers

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