小编Tra*_*acy的帖子

怀疑C++面试问题

我已经阅读了C++面试问题的答案,其中有一个令我困惑的问题:

问:C++编译器何时创建临时变量?

答:如果函数参数是"const引用",则编译器按以下两种方式生成临时变量.

a)实际参数是正确的类型,但它不是Lvalue

double Cube(const double & num)
{
  num = num * num * num;
  return num;
}

double temp = 2.0;
double value = cube(3.0 + temp); // argument is a expression and not a Lvalue
Run Code Online (Sandbox Code Playgroud)

b)实际参数的类型错误,但是可以转换为正确类型的类型

 long temp = 3L;
 double value = cuberoot(temp); // long to double conversion
Run Code Online (Sandbox Code Playgroud)

我的问题是,一旦函数参数是一个const引用,为什么编译器生成临时变量,是不是自相矛盾?此外,如果函数Cube无法编译,因为它修改了const参数?

c++ const reference temporary

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

请解释此代码以计算字符串的哈希码

我读了一本算法书,其中说明给定字符串的键计算如下

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
Run Code Online (Sandbox Code Playgroud)

使用int算术,其中s [i]是字符串的第i个字符,n是字符串的长度,^表示取幂.(空字符串的哈希值为零.)

或在代码中:

 int h = 0;
 for (int i = 0; i < n; i++) {
   h = 31*h + s.charAt(i);
 }
Run Code Online (Sandbox Code Playgroud)

我的问题是,似乎代码实现并不等同于上面所述的计算方法.例如,给定字符串"ha"(分别为ascii代码104和97)

s [0]*31 ^(2-1)+ s [1]*31 ^ 0 = 104*31 + 97

从代码执行开始,结果是104 + 31*104 + 97

绝对两者不相等,那么如何解释呢?

参考链接:http://www.informatics.sussex.ac.uk/courses/dats/notes/html/node114.html

string algorithm hash hashcode

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

在bash shell中查找命令,并且-name选项有疑问

下面两者有什么区别:

find . -type f -name \*.bmp
find . -type f -name *.bmp 
Run Code Online (Sandbox Code Playgroud)

我测试过,它们都返回相同的结果,所以有什么不同_deep inside_吗?


删除的答案中添加:

因此,为了避免特殊*****字符的shell扩展,只需将*作为参数传递给find命令并让它处理它.

但是在我的机器上,它们都很好,都返回当前目录中和下面的bmp文件,仅举几例,结果如下,为简洁省略了一些

./images/building_color.bmp
./images/building_gray.bmp
./images/car_gray.bmp
./images/temple_color.bmp
./images/boat_gray.bmp
./images/tools_gray.bmp
./images/temple_gray.bmp
./images/tools_color.bmp
./images/car_color.bmp
./images/boat_color.bmp
Run Code Online (Sandbox Code Playgroud)

系统信息:

GNU bash,版本4.1.5(1)-release(i486-pc-linux-gnu)

Linux sysabod-laptop 2.6.32-30-generic#59-Ubuntu SMP Tue Mar 1 21:30:21 UTC 2011 i686 GNU/Linux

bash shell scripting find

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

c函数原型不匹配只是一个警告

请看下面的代码

#include <stdio.h>

void printOut()
{
 static int i = 0;
 if (i < 10)
 {
  printOut(i);
 }
}

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

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

我猜应该有一个错误,因为我调用了不存在的函数prototype.Actually,代码编译好mingw5编译器,这对我来说很奇怪,然后我改为Borland编译器,我收到一条警告消息说没有printOut功能原型,这只是一个警告吗?更重要的是,代码执行良好,没有任何弹出错误窗口.

c compiler-construction static function

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

发送到孤立的 linux 进程组的 SIGCONT 和 SIGHUP 的顺序

APUE 说

由于进程组在父进程终止时是孤立的,POSIX.1 要求新孤立进程组中的每个停止的进程(就像我们的孩子一样)被发送挂断信号(SIGHUP),然后是继续信号(SIGCONT)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#define errexit(msg) do{ perror(msg); exit(EXIT_FAILURE); } while(0)
static void sig_hup(int signo)
{
    printf("SIGHUP received, pid = %d\n", getpid());
}
static void sig_cont(int signo)
{
    printf("SIGCONT received, pid = %d\n", getpid());
}
static void sig_ttin(int signo)
{
    printf("SIGTTIN received, pid = %d\n", getpid());
}
static void pr_ids(char *name)
{
    printf("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
           name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO)); …
Run Code Online (Sandbox Code Playgroud)

c unix linux signals process

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

父子进程,套接字关闭

我被这个模拟服务器 - 客户端交互的代码片段所困,假设sockfd是在服务器端创建的套接字文件描述符.

我的问题是当父和它的子进程'同时'运行时,并且在子进程执行时间片期间,它关闭服务器套接字sockfd,然后当执行流到第二次循环,调用accpet函数时,参数sockfd有效,是否被子进程关闭,即从内核文件描述符表中解除分配?

while (1) {
    //accept a connection from client,get the new socket from client
                       //is the sockfd valid here,is it closed by the child in
                       //the previous loop
     newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); 

     if (newsockfd < 0) 
         error("ERROR on accept");
     pid = fork();
     if (pid < 0)
         error("ERROR on fork");
     if (pid == 0)  {
         close(sockfd);  // can't this cause problem ??
         dostuff(newsockfd);
         exit(0);
     } …
Run Code Online (Sandbox Code Playgroud)

sockets process parent-child

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

两个printf语句之间的比较

请看下面两个c语句

printf("a very long string");
printf("%s","a very long string");
Run Code Online (Sandbox Code Playgroud)

它们会产生相同的结果,但在引擎盖下肯定存在一些差异,那么差异哪个更好?请分享您的想法!

c compiler-construction string printf

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

int指针和浮点指针之间的奇怪区别

请看下面的代码

#include <stdio.h>
#include <stddef.h>

typedef struct _node
{
int a;
char *s;
}Node, *nodePtr;

int main(int argc, char *argv[])
{
char *str = "string"; /*str points to satic storage area*/
Node nd;
nodePtr pNode = NULL;
size_t offset_of_s = offsetof(Node,s);

nd.a = 1;
nd.s = str;

pNode = &nd;

    /*Get addr of s, cast it to a different data types pointer, then de-reference it*/

/*this works, print "string"*/
printf("%s\n", *(int*)((char*)pNode + offset_of_s));

/*this sucks, print (null)*/
printf("%s\n", *(float*)((char*)pNode + …
Run Code Online (Sandbox Code Playgroud)

c floating-point int printf pointers

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

关于tcp半关闭的问题

请看下面的图片:

在此输入图像描述

客户端发起半关闭请求并得到确认,如果我不误解半关闭意味着什么,客户端之后不应该能够发送任何数据,那么它如何能够在以后发回ACK.

ip networking tcp terminate half-close

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

什么是"who | grep $ 1"命令在shell脚本中做什么?

我正在使用名为Beginning Linux Programming(第4版)的书从基础知识中学习shell编程.我对此脚本的一个until-clause 感到困惑:

#!/bin/bash

until who | grep "$1" > /dev/null
do
   sleep 60
done

# Now ring the bell and announce the unexpected user.

echo -e '\a'
echo "***** $1 has just logged in *****"

exit 0
Run Code Online (Sandbox Code Playgroud)

我怀疑who | grep "$1" > /dev/null这里用的是什么?为什么要将grep输出重定向到/dev/null

unix linux shell select-until

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