我遇到了"消化"valgrind输出的问题.这是一个片段:
==15145== Invalid write of size 8
==15145== at 0x40168E: split_node_at_letter (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x4018E7: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401A35: insert_word (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401BD5: main (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== Address 0x52237d8 is 8 bytes before a block of size 16 alloc'd
==15145== at 0x4C29BCF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==15145== by 0x401063: add_to_trie_word_list (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x40173B: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x40183D: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401906: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401A35: insert_word (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii) …Run Code Online (Sandbox Code Playgroud) 为什么使用gcc5(5.3.0)编译下面的最小测试用例C代码会对带有const参数的warning()函数调用生成[-Wincompatible-pointer-types]警告,并使用ConstMyDouble typedef强制转换该参数,但是不是对于不使用ConstMyDouble转换的nowarning()调用,也不是对使用非const typedef MyDouble的noconst()调用,以及如何修复它?
当在constdef中使用[const]并且使用typedef将参数强制转换为函数时,似乎有一种微妙之处.
最令人困惑的部分是警告信息:
minimal.c:17:7: note: expected ‘const double (*)[2]’ but argument is
of type ‘const ConstMyDouble (*)[2] {aka const double (*)[2]}’
Run Code Online (Sandbox Code Playgroud)
这似乎是说const double(*)[2]与(又名)const double(*)[2]不一样
/* Usage:
*
* ./minimal ; echo ${PIPESTATUS[0]}
* => echo command will output 99 (BASH)
*
* Compile and link, default:
*
* gcc5 minimal.c -o minimal
* => Casts argument to minimal to [ConstMyDouble], a typedef
* => Generates [-Wincompatible-pointer-types] warnings
*
*/
typedef double MyDouble;
typedef …Run Code Online (Sandbox Code Playgroud) int main()
{
char *p;
p = (char* ) malloc(sizeof(char) * 0);
printf("Hello Enter the data without spaces :\n");
scanf("%s",p);
printf("The entered string is %s\n",p);
//puts(p);
}
Run Code Online (Sandbox Code Playgroud)
在编译上面的代码并运行它时,程序能够读取字符串,即使我们为指针p分配了一个0字节的内存.
声明中究竟发生了p = (char* ) malloc(0)什么?
如果我首先将浮点计算的值赋给变量,然后使用隐式类型转换将其赋值给unsigned int,我得到一个答案.但是如果我将相同的计算直接分配给unsigned int,再次使用隐式类型转换,我得到一个不同的答案.
下面是我编译并运行以演示的示例代码:
#include <iostream>
int
main( int argc, char** argv )
{
float payloadInTons = 6550.3;
// Above, payloadInTons is given a value.
// Below, two different ways are used to type cast that same value,
// but the results do not match.
float tempVal = payloadInTons * 10.0;
unsigned int right = tempVal;
std::cout << " right = " << right << std::endl;
unsigned int rawPayloadN = payloadInTons * 10.0;
std::cout << " wrong = " << …Run Code Online (Sandbox Code Playgroud) 我无法在我的数字海洋托管服务器上通过yum重新安装PHP.
我安装了PHP 5.4但想要5.6.我添加/启用了remi repo,运行yum -y remove php*然后尝试重新安装php.
当我跑步时,yum -y install php我得到以下内容:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.centos.webair.com
* epel: mirrors.coreix.net
* extras: mirrors.centos.webair.com
* remi: remi.check-update.co.uk
* remi-php56: remi.check-update.co.uk
* updates: mirrors.centos.webair.com
* webtatic: uk.repo.webtatic.com
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.6.8-1.el6.remi will be installed
--> Processing Dependency: php-common(x86-64) = 5.6.8-1.el6.remi for package: php-5.6.8-1.el6.remi.x86_64
--> Processing Dependency: php-cli(x86-64) = 5.6.8-1.el6.remi for package: php-5.6.8-1.el6.remi.x86_64
--> Processing Dependency: httpd-mmn …Run Code Online (Sandbox Code Playgroud) 我想做两件事
创建一个私有实例变量,它是一个映射
在我的构造函数中创建一个空实例,它实现了一个地图并将其分配给前一个私有实例变量.
我拥有的私有实例是
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
Run Code Online (Sandbox Code Playgroud)
但是如何在构造函数中创建一个实例变量,该变量将在构造函数创建时引用私有变量同义词库.
例如
public class Book{
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
public Book(){
super();
/* What do i put here as an empty instance
* variable that implements a map and how
* do i assign it to thesaurus?
*/
}
Run Code Online (Sandbox Code Playgroud) 我正在为std :: cin >>序列寻找一些模拟scanf("%1d",&sequence).
例如:
for ( ; scanf("%1d", &sequence) == 1; ) {
printf("%d ", sequence);
}
Run Code Online (Sandbox Code Playgroud)
stdin: 5341235
stdout: 5 3 4 1 2 3 5
它在C++中是如何工作的?
for ( ; std::cin >> *some_magic* sequence; ) {
std::cout << sequence << " ";
}
Run Code Online (Sandbox Code Playgroud) 这只是在我测试我分离的更大程序的一部分时发生的.原始函数将以我需要的特殊方式从字符串中删除非ascii字符,就是这个程序
#include <stdio.h>
#include <wchar.h>
int main(int argc, char *argv[])
{
fwprintf(stdout, L"-- Example\n");
fprintf(stdout, "-- Example\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不会-- Example在我的linux(Fedora 22)系统上打印第二个.虽然fwprintf()再次使用或fprintf(stderr, "-- Example\n");将工作.
我最近开始编写一些使用套接字的C++代码,我希望它是异步的.我读过许多关于如何使用poll和select来使我的套接字异步(使用poll或select等待send或recv缓冲区)的帖子,但在我的服务器端我有一个struct pollfd数组,每次都有侦听套接字接受连接,它将它添加到struct pollfd数组,以便它可以监视该套接字的recv(POLLIN).
我的问题是,如果我有5000个套接字连接到我的服务器上的监听套接字,那么struct pollfd的数组大小将为5000,因为它将监视所有连接的套接字但是我知道如何检查是否一个套接字的recv已准备就绪,是通过循环遍历struct pollfd数组中的所有项来查找其revents等于POLLIN的项.这只是效率低下,当连接套接字的数量因为非常大.有一个更好的方法吗?
boost :: asio库如何处理async_accept,async_send等...?我应该怎么处理?
我被告知这个代码片段相当于 (int)sqrt(n)
int s(int n) {
for (int i = 1, k = 0; n > 0; i += 2) {
if (k + i > n)
return i / 2;
k += i;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作,但我不明白它是如何工作的?