小编ple*_* me的帖子

使stdin无阻塞

我有一个练习,我需要缓慢打印文件(间隔1秒),直到文件结束,除非用户键入一个字符.

到目前为止,程序以一秒的间隔输出文件很好,但是当我输入一个字符时,没有任何反应.我的猜测是我以某种方式使用选择错误.

这是我最终提交的最终计划.

#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    FILE* infile;
    char str[100];
    fd_set readset;
    struct timeval tv;

    // open a file
    if((infile = fopen("infile", "r")) == NULL)
    {
        (void)printf("Couldn't open the file\n");
        exit(1);
    }
    // file was opened successfully
    else
    {       
        // while we are not at the end of a file
        while(fgets(str, 100, infile) != NULL)
        {
            FD_ZERO(&readset);
            FD_SET(fileno(stdin), &readset);
            // set the time value to 1 second
            tv.tv_sec …
Run Code Online (Sandbox Code Playgroud)

c

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

程序耗尽我的所有记忆

我正在进行纹理合成项目.我差不多完成了,但是我的进程在运行时占用了大约2GB的内存.我以为我释放了所有的calloc'ed阵列,但我可能错了.C++不是我的专长.

我知道三重指针在C++中是错误的方式,但我现在对它们更加满意.如果您需要查看更多代码,请告诉我们.谢谢.

MetaPic声明

class MetaPic
{
    public:
        Pic* source;
        Pixel1*** meta;
        int x;
        int y;
        int z;
        MetaPic();
        MetaPic(Pic*);
        MetaPic(const MetaPic&);
        MetaPic(int, int, int);
        MetaPic& operator=(const MetaPic&);
        ~MetaPic();
        void allocateMetaPic();
        void copyPixelData();
        void copyToOutput(Pic*&);
        void copyToMetaOutput(MetaPic&, int, int);
        void copyToSample(MetaPic&, int, int);
        void freeMetaPic();

};
Run Code Online (Sandbox Code Playgroud)

分配三重指针

    void MetaPic::allocateMetaPic()
{
    meta = (Pixel1***)calloc(x, sizeof(Pixel1**));

    for(int i = 0; i < x; i++)
    {
        meta[i] = (Pixel1**)calloc(y, sizeof(Pixel1*));
        for(int j = 0; j < y; j++)
        {
            meta[i][j] = (Pixel1*)calloc(z, sizeof(Pixel1));
        } …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management

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

标签 统计

c ×1

c++ ×1

memory-management ×1