小编Cro*_*man的帖子

如何在多个线程中使用printf()

我正在实现一个使用不同内核的多线程程序,并且同时执行许多线程.每个线程都进行一次printf()调用,结果不可读.

我如何制作printf()原子,以便printf()一个线程中的printf()调用与另一个线程中的调用不冲突?

c linux printf multithreading multiprocess

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

指向函数参数与函数参数的指针?

我想了解的是什么2点声明之间的区别,f1以及f2,如下所示:在f1我宣布参数是一个指针类型的函数void(),是如何f2从不同的声明f1?声明是否相同?在main我可以用样机的功能调用两者void ().我理解传递值/指针/引用的概念,但这些是函数,并没有真正理解它们的区别.它不像我可以"修改"作为参数传递的函数f1...谢谢!

PS:当碰到众所周知的最令人烦恼的解析问题时,我遇到了这个问题:)

#include <iostream>

using namespace std;

void f1(void (*x)())
{
    x();
}

void f2(void x())
{
    x();
}

void g1()
{
    cout << "Invoking f1(g1())" << endl;
}

void g2()
{
    cout << "Invoking f2(g2())" << endl;
}


int main() 
{
    f1(g1);
    f2(g2);
}
Run Code Online (Sandbox Code Playgroud)

程序编译,输出是

Invoking f1(g1())
Invoking f2(g2())
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

g ++ 4.7.2上的-std = c ++ 11的CppUTest错误

我一直在使用CppUTest与g ++ 4.7.2一段时间没有问题.但是,我刚刚打开-std=c++11选项,所以我可以开始使用std::unique_ptr它立即失败.

即使只是编译主模块:

#include <CppUTest/CommandLineTestRunner.h>

int main(int argc, char ** argv) {
    return CommandLineTestRunner::RunAllTests(argc, argv);
}
Run Code Online (Sandbox Code Playgroud)

失败的变化:

In file included from /usr/include/CppUTest/TestHarness.h:77:0,
                 from /usr/include/CppUTest/CommandLineTestRunner.h:31,
                 from tests/testmain.cpp:15:
/usr/include/CppUTest/MemoryLeakWarningPlugin.h:56:53: error: declaration of ‘void* operator new(size_t) throw (std::bad_alloc)’ has a different exception specifier
In file included from /usr/include/c++/4.7/ext/new_allocator.h:34:0,
                 from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34,
                 from /usr/include/c++/4.7/bits/allocator.h:48,
                 from /usr/include/c++/4.7/string:43,
                 from /usr/include/CppUTest/SimpleString.h:136,
                 from /usr/include/CppUTest/Utest.h:34,
                 from /usr/include/CppUTest/TestHarness.h:71,
                 from /usr/include/CppUTest/CommandLineTestRunner.h:31,
                 from tests/testmain.cpp:15:
/usr/include/c++/4.7/new:93:7: error: from previous declaration ‘void* operator new(std::size_t)’
Run Code Online (Sandbox Code Playgroud)

删除该-std=c++11选项使一切工作再次正常.

CppUTest文档对与重载的新运算符冲突的宏做了一些评论,并建议首先包括#including标准头,但我得到这个问题而根本不包括任何头,尽管它看起来像CppUTest/CommandLineTestRunner.h包括 …

cpputest c++11 g++-4.7

6
推荐指数
0
解决办法
738
查看次数

阻止用户将负数传递给接受unsigned int的函数

所以这是代码:

int create_mask(unsigned b, unsigned e)
{
  unsigned int mask=1;

  if(b<e || b<0 || e<0)
  {
    printf("Wrong values, starting bit can't be smaller than ending.\n");
    printf("Both got to be >= 0.\n");
    exit(EXIT_FAILURE);
  }
  while(b>0)
  {
    printf("%u\n", b);
    mask<<=1;
    if(b>e)
      mask|=1;
    b--;
  }

  return ~mask; /* negates mask for later purpose that is clearing corresponding bits */
}
Run Code Online (Sandbox Code Playgroud)

函数为某些位操作创建掩码,但应该采用两个无符号整数b和e,两者都是非负数.问题是如何防止用户输入负数?当用(-1,0)调用函数时,它启动循环,并且错误地退出.

c negative-number unsigned-integer

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

在C中初始化结构数组

我正在做一个简单的学生数据库程序练习,我不确定如何初始化一组结构.我正在尝试stdt[]使用编译时已知的值初始化数组的前3个元素,然后将从用户输入填充接下来的3个学生的信息.当我编译时,我收到错误:

lab7.c: In function ‘main’:

lab7.c:16:9: error: expected expression before ‘{’ token
 stdt[0]={"John","Bishop","s1234","Inf",'m',18};
         ^

lab7.c:17:9: error: expected expression before ‘{’ token
 stdt[1]={"Lady","Cook","s2345","Eng",'f',21};
         ^

lab7.c:18:9: error: expected expression before ‘{’ token
 stdt[2]={"James","Jackson","s33456","Eng",'m',17};
         ^
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能正确?

这是迄今为止的代码:

#include <stdlib.h>
#include <stdio.h>

typedef struct {
    char *name;
    char *surname;
    char *UUN;
    char *department;
    char gender;
    int age;
} student_t;

int main() {
    int i;
    student_t stdt[6];
    stdt[0]={"John","Bishop","s1234","Inf",'m',18};
    stdt[1]={"Lady","Cook","s2345","Eng",'f',21};
    stdt[2]={"James","Jackson","s33456","Eng",'m',17};

    for(i=3;i<6;i++) {
        printf("First name: \n");
        scanf("%s",stdt[i].name);
        printf("Last name: \n");
        scanf("%s",stdt[i].surname);
        printf("UUN: \n");
        scanf("%s",stdt[i].UUN); …
Run Code Online (Sandbox Code Playgroud)

c arrays struct

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

我应该读/写多少字节到套接字?

我对应该通过 Unix 上的 C 中的套接字写入/读取的字节数有一些疑问。我习惯于发送 1024 个字节,但有时当我发送短字符串时这真的太多了。

我从文件中读取了一个字符串,我不知道这个字符串有多少字节,它每次都可以变化,可以是 10、20 或 1000。我只知道它 < 1024。所以,当我编写代码,我不知道在客户端读取的字节大小,(在我可以使用的服务器上strlen())。那么,无论我从文件中读取的字符串的长度如何,始终读取最大字节数(在这种情况下为 1024)的唯一解决方案是什么?

例如,使用此代码:

read(socket,stringBuff,SIZE);
Run Code Online (Sandbox Code Playgroud)

SIZE如果我想读取一个 10 字节的字符串,如果是 10 而不是 1024 不是更好吗?

c sockets

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

为什么eclipse会在这里产生语法错误?

我的代码:

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class TAFrame {

    private JFrame mainFrame;
    private JPanel mainPanel;
    private JButton button;
    private JTextArea textArea; //eclipse say Syntax error } expected


    mainFrame = new JFrame("mainFrame");
    mainPanel = new JPanel();
    button = new JButton("click me");
    area = new JTextArea(10, 15); 


}
Run Code Online (Sandbox Code Playgroud)

无法找到解决方案,但我认为这很容易让人尴尬:/

java syntax

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

为什么我会收到错误"Segmentation fault(core dumped)"?

我刚刚开始使用C编程,我正在制作一个计算特定数量Fibonacci数的程序.它工作正常,除了我收到错误"Segmentation fault(core dumped)".我的代码出了什么问题?

#include <stdio.h>

int main() {
    int max;
    long long int fiNum[] = {1, 1};

    printf("How many numbers do you want to get? ");
    scanf("%d", &max);

    int i = 1;
    long long int x;

    while ( i < max ) {
        x = fiNum[i] + fiNum[i-1];
        printf("%lld ", x);
        i++;
        fiNum[i] = x;
    }

    printf("\nDone!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我要求让10个数字输出时:

2 3 5 8 13 21 34 55 89 
Done!
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

我正在使用Linux(Ubuntu)顺便说一句.

提前致谢.

c

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

从文件中进行十六进制转换

我的代码出了什么问题?我试图逐行读取文件并将0x"十六进制数"形式的数字转换为整数.它只返回1行然后结束,我的输入是这样的

0x9C40

0x3B9AC9FF

0x754893gf

0x754893gf

0x754893gf

0x754893gf

  #include <stdio.h>
#include <stdlib.h>


 #include <stdio.h>

 #define MAXCHAR 1000
 int main() {
     FILE *fp;
    char str[MAXCHAR];
    char* filename = "C:\\test.txt";
    int number;

    fp = fopen(filename, "r");
   if (fp == NULL){
      printf("Could not open file %s",filename);
      return 1;
     }
   while (fgets(str, MAXCHAR, fp) != NULL)
       number = (int)strtol(str, NULL, 0);
    printf("%d\n", number);
   fclose(fp);
    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

c

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

C++ code speed, which is faster?

Yes this is a homework question i just need a push in the right direction

Which code block of C++ is faster and why? I Think it is the top one because the [i] array is being used in order, or am i wrong here?.

    double A[100][100];
    ...
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            A[i][j] = i * j;
        }
     }


    double A[100][100];
    ...
    for (int …
Run Code Online (Sandbox Code Playgroud)

c++

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