小编Ori*_*ood的帖子

为什么在编写linux守护进程时必须从tty中分离出来?

当我尝试使用C在linux下编写一个守护进程时,我被告知我应该在fork代码块之后添加以下代码:

/* Preparations */
...

/* Fork a new process */
pid_t cpid = fork();
if (cpid == -1){perror("fork");exit(1);}
if (cpid > 0){exit(0);}

/* WHY detach from tty ? */
int fd = open("/dev/tty", O_RDWR);
ioctl(fd, TIOCNOTTY, NULL);

/* Why set PGID as current PID ? */
setpgid(getpid(), 0);
Run Code Online (Sandbox Code Playgroud)

我的问题是:是否有必要进行上述操作?

c linux terminal daemon

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

声音不能在iPad扬声器上播放,但在耳机和iPod Touch/iPhone扬声器上工作正常

我知道这是一个已被问过多次的问题,我已经检查了大部分与SO相关的答案,但我没有找到解决问题的正确答案.

这是问题所在:

当某个事件被触发时,我试图在游戏中播放一个mp3文件(最多只有2秒),我使用AudioPlayer这样做,下面是代码块:

NSError *error;
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"ding" withExtension: @"mp3"] error:&error] autorelease];
if (error)  {
    NSLog(@"Error creating audio player: %@", [error userInfo]);
}
else {
    BOOL success = [audioPlayer play];
    // This always is "Play sound succeeded"
    NSLog(@"Play sound %@", success ? @"succeeded" : @"failed");
}
Run Code Online (Sandbox Code Playgroud)

当我在iPhone 4s,iTouch 3/4中运行此代码时,声音总是很清晰,但在iPad 1或iPad2中,扬声器没有声音.但是当我插入耳机时,发生了一些奇怪的事情,我的耳机有声音!iPad未处于静音模式且URL正确无误.

我很困惑为什么会这样.

PS:我尝试了以下代码(从HERE获得)输出音频输出端口类型:

CFDictionaryRef asCFType = nil;
UInt32 dataSize = sizeof(asCFType);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &asCFType);
NSDictionary *easyPeasy = (NSDictionary *)asCFType;
NSDictionary *firstOutput = (NSDictionary …
Run Code Online (Sandbox Code Playgroud)

avaudioplayer ipad ios

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

const转换为全局var和程序崩溃(C++)

int main()
{
    const int maxint=100;//The program will crash if this line is put outside the main
    int &msg=const_cast<int&>(maxint);  
    msg=200;  
    cout<<"max:"<<msg<<endl; 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果'const int maxint = 100;',该函数将运行正常.定义被放在main函数内但崩溃并弹出一条错误消息,如果放在外面则说"Access Violation".

有人说这是某种"未定义的行为",我想知道确切的答案以及如何安全地使用const转换?

c++ const-cast

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

返回一个本地char数组为什么strcpy工作但字符串构造函数失败?

今天我遇到了以下代码块:

#include <iostream>
using namespace std;
char *return_char_array(const char *cptr)
{
    char charArray[100] = {0};
    strcpy(charArray, cptr);
    return charArray;
}
int main()
{
    const char *cptr = "test";

    char localCharArray[100] = {0};
    strcpy(localCharArray, return_char_array(cptr)); // output "test"
    cout<<localCharArray<<endl;

    string s = return_char_array(cptr);              // corrupt output
    cout<<s<<endl; 

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

乍一看,我认为输出都会损坏,但令人惊讶的是第一个输出是"测试"而第二个输出是腐败的.有人会告诉我为什么吗?

c++

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

关于函数模板的相同代码块在g ++下编译好,但VC6下的错误,为什么?

我正在阅读C++ Primer第3版的 "功能模板"一章,当我试图按照这个例子时,我发现代码几乎与本书在VC6下编译时遇到错误相同但在g ++下一切正常.我不知道为什么?

这是代码:

#include <iostream>
using namespace std;

template<typename T1, typename T2, typename T3>
T1 my_min(T2 a, T3 b)
{
    return a>b?b:a;
}

int main()
{
    int (*fp)(int, int) = &my_min<int>;
    cout<<fp(3,5)<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

发生的错误VC6如下所示:

error C2440: 'initializing' : cannot convert from '' to 'int (__cdecl *)(int,int)'
None of the functions with this name in scope match the target type
Run Code Online (Sandbox Code Playgroud)

c++ function-templates

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

标签 统计

c++ ×3

avaudioplayer ×1

c ×1

const-cast ×1

daemon ×1

function-templates ×1

ios ×1

ipad ×1

linux ×1

terminal ×1