我正在编写一个跨平台程序.我希望这个程序在Windows和Linux下运行,所以我有两个不同的代码段用于这两个平台.如果操作系统是Windows,我想要运行第一个代码段; 如果它是Linux,那么我希望第二个代码段运行.
所以我编写了以下代码,但在Windows和Linux上构建时都会出错.我该怎么做才能解决它?
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
#define OS_Windows 0
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#elif defined(_WIN32) || defined(WIN32) /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
#define OS_Windows 1
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define DIV 1048576
#define WIDTH 7
#endif
int main(int argc, char *argv[])
{
if(OS_Windows)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There …Run Code Online (Sandbox Code Playgroud) 我有一个程序的两个版本基本上做同样的事情,在文件中得到一行的最大长度,我有一个大约8千行的文件,我的C代码有点原始(当然!)比我在C++中的代码.C程序运行大约需要2秒钟,而C++程序运行需要10秒钟(我正在测试两个案例的同一文件).但为什么?我期待它花费相同的时间或更多但不会慢8秒!
我在C中的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _DEBUG
#define DEBUG_PATH "../Debug/"
#else
#define DEBUG_PATH ""
#endif
const char FILE_NAME[] = DEBUG_PATH "data.noun";
int main()
{
int sPos = 0;
int maxCount = 0;
int cPos = 0;
int ch;
FILE *in_file;
in_file = fopen(FILE_NAME, "r");
if (in_file == NULL)
{
printf("Cannot open %s\n", FILE_NAME);
exit(8);
}
while (1)
{
ch = fgetc(in_file);
if(ch == 0x0A || ch == EOF) // \n or \r or \r\n or end of …Run Code Online (Sandbox Code Playgroud) gcc 4.4.4下面的简单代码段错误
#include<stdio.h>
typedef struct Foo Foo;
struct Foo {
char f[25];
};
Foo foo(){
Foo f = {"Hello, World!"};
return f;
}
int main(){
printf("%s\n", foo().f);
}
Run Code Online (Sandbox Code Playgroud)
将最后一行更改为
Foo f = foo(); printf("%s\n", f.f);
Run Code Online (Sandbox Code Playgroud)
工作良好.这两个版本在编译时都有效-std=c99.我只是调用未定义的行为,或者标准中的某些内容已更改,这允许代码在C99下工作?为什么在C89下崩溃?
我写过一本表现不佳的翻译,几周来一直在苦苦挣扎.在下面简单的bechmark
#include<stdio.h>
int main()
{
int x;
char buf[2048];
FILE *test = fopen("test.out", "wb");
setvbuf(test, buf, _IOFBF, sizeof buf);
for(x=0;x<1024*1024; x++)
fprintf(test, "%04d", x);
fclose(test);
return 0
}
Run Code Online (Sandbox Code Playgroud)
我们看到以下结果
bash-3.1$ gcc -O2 -static test.c -o test
bash-3.1$ time ./test
real 0m0.334s
user 0m0.015s
sys 0m0.016s
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,在添加"-std = c99"标志的那一刻,性能崩溃了:
bash-3.1$ gcc -O2 -static -std=c99 test.c -o test
bash-3.1$ time ./test
real 0m2.477s
user 0m0.015s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
我正在使用的编译器是gcc 4.6.2 mingw32.
生成的文件大约是12M,所以这两者之间的差异大约为21MB/s.
运行diff显示生成的文件是相同的.
我认为这与文件锁定有关fprintf,程序大量使用,但我无法找到在C99版本中关闭它的方法.
我尝试flockfile了在程序开始时使用的流,并funlockfile在最后使用了相应的流,但是遇到了关于隐式声明的编译器错误,以及声称对这些函数的未定义引用的链接器错误. …
我只是试图使用一个指向整数数组的void指针,我试着看看我是否可以通过将数组转换回int来打印它.但它给了我一些随机价值.你能告诉我哪里出错了吗?
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[5];
int x;
int j;
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
void *arr=a;
for(j=0;j<4;j++){
x = *(int *)(arr+j);
printf("%d",x);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
133554432131072512
Run Code Online (Sandbox Code Playgroud)
为什么它没有固定数组的元素a []即1,2,3,4?
这有点长...对于初学者我在Linux 2.6.33,gcc 4.4.4.
我编写了一个小程序,它创建一个命名管道并读取它,直到它看到某个字符串,然后它取消了FIFO,并重新执行自己.
#include<unistd.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
int fifo;
#define put(x) write(1, x, (sizeof x)-1)
void reader(int a)
{
char buf[26];
int n;
while((n=read(fifo, buf, 25))>0){
buf[25] = '\0';
if(!strncmp(buf, "moo", 3)){
put("exec()-ing\n");
close(fifo);
unlink("lefifo");
execl("/home/dave/a.out", "a.out", 0);
}
write(1, buf, n);
}
}
main()
{
signal(SIGIO, reader);
mknod("lefifo", 0600|S_IFIFO,0);
fifo = open("lefifo", O_RDONLY|O_NONBLOCK );
fcntl(fifo, F_SETOWN, getpid());
fcntl(fifo, F_SETFL, O_ASYNC);
for(;;)
pause();
}
Run Code Online (Sandbox Code Playgroud)
编译后,在后台运行时,我可以回显lefifo并按预期工作,直到我输入以"moo"开头的字符串.以下示例会话:
$ gcc fifo.c
$ ./a.out&
$ echo klar > lefifo
klar …Run Code Online (Sandbox Code Playgroud) 如果struct a a1 = {0};将结构的所有元素(不同类型)struct a a2 = {5};初始化为零,那么应该将其初始化为5..否?
#include <stdio.h>
typedef struct _a {
int i;
int j;
int k;
}a;
int main(void)
{
a a0;
a a1 = {0};
a a2 = {5};
printf("a0.i = %d \n", a0.i);
printf("a0.j = %d \n", a0.j);
printf("a0.k = %d \n", a0.k);
printf("a1.i = %d \n", a1.i);
printf("a1.j = %d \n", a1.j);
printf("a1.k = %d \n", a1.k);
printf("a2.i = %d \n", a2.i);
printf("a2.j = %d \n", …Run Code Online (Sandbox Code Playgroud) 在阅读这个SO问题的答案时,我了解到越界指针算法是未定义的.的确,根据C99 6.5.6第8段
如果指针操作数和结果指向相同的数组对象,或一个过去的数组对象的最后一个元素的元素两者,所述评估也不得产生溢出; 否则,行为未定义.
释放该对象会使该保证无效吗?7.20.3.2"自由功能"似乎没有提及它,只是提到"空间被解除分配".由于6.5.6特别提到溢出,它似乎是一个整数溢出问题,free不会影响.指向对象的指针是否是"引用它"的行为?
换句话说,是:
char *foo = malloc(10);
free(foo);
foo++;
Run Code Online (Sandbox Code Playgroud)
未定义?或者"溢出"的用法是不同的?
在macbook(OSX 10.9.5(13F34))上有以下简单程序:
#include <stdio.h>
#include <signal.h>
static void nop(int unused) { }
int
main(void) {
struct sigaction sa, osa;
sigset_t mask;
sigemptyset(&sa.sa_mask);
printf("Errno after sigempty sa_mask: %d\n", errno);
sigemptyset(&osa.sa_mask);
printf("Errno after sigempty oldsa_mask: %d\n", errno);
sa.sa_flags = 0;
sa.sa_handler = nop;
sigprocmask(0, NULL, &mask);
printf("Errno after sigprocmask mask: %d\n", errno);
printf("%d\n", sigismember(&mask, SIGALRM));
sigaction(SIGALRM, &sa, &osa);
printf("Errno after sigaction sa osa: %d\n", errno);
printf("%d\n", sigismember(&osa.sa_mask, SIGALRM));
printf("%d\n", sigismember(&sa.sa_mask, SIGALRM));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
神秘印刷:
Errno after sigempty sa_mask: 0
Errno after …Run Code Online (Sandbox Code Playgroud) 我试图在Linux中的c中创建一个简单的文件服务器.我的想法是,我在客户端输入要从服务器传输的文件的名称.一旦建立连接,服务器就会重新获取该文件名.但是无论我传递什么参数,它总是打开当前目录中的第一个文件.这是服务器代码
getcwd(buf3, sizeof(buf3));
dirp = opendir(buf3);
while (dptr = readdir(dirp)) {
if (strcmp(buf, dptr->d_name) == 0);
break;
printf("%s\n", dptr->d_name);
}
string(dptr->d_name);
f = fopen(namelist[n1]->d_name, "r");
//f1=fopen(buf,"r");
if (f1 == NULL)
err_sys("open error");
while (1) //service client till it closes connection
{
fgets(buf2, 50, f1);
buflen = strlen(buf2);
printf("Data Read From File: %d\n", buflen);
if ((n = send(clifd, buf2, buflen, 0)) < 0)
err_sys("send error");
else if (n == 0) {
printf("\nNo Data Recieved");
break;
}
buf2[n] = 0;
}; //end …Run Code Online (Sandbox Code Playgroud) Singleton中的Double Lock检查通常写为:
public static Singleton getInstance()
{
if (instance == null)
{
synchronized(Singleton.class) { //1
if (instance == null) //2
instance = new Singleton(); //3
}
}
return instance; //4
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,假设有十个线程正在调用此方法,所有这些线程都超过了第一个if条件,然后一个线程进入synchronized块并创建实例.即使创建了实例,它们也需要等待并顺序通过synchronized块,剩下的9个线程将逐个出现.我希望只要任何线程创建Singleton实例,所有其他线程就不应该等待.告诉我是否有解决方案?