我正在使用Microsoft C/C++编译一些C文件,并且它抱怨在块中声明局部变量.当然,在块的开头声明它们是好的.我可以用什么编译器开关来抑制我得到的错误?
非常感激,
短剑的一种
我的CLI程序在windows上编译并运行良好.在linux上编译很好,但在运行时会导致分段错误.
我转向stackoverflow寻求帮助,发现了一些类似于我要问的建议valgrind的问题,我刚刚安装了它(哇!).
所以我通过valgrind运行我的程序,得到了令人沮丧的大量输出,但我将从第一条错误消息开始:
==11951== Command: ./vt
==11951==
Loading...
Load default database? (y/n)y
Opened input file vtdb.~sv, reading contents...
==11951== Invalid write of size 1
==11951== at 0x400FA9: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== Address 0x53b05bb is 0 bytes after a block of size 11 alloc'd
==11951== at 0x4C28FAC: malloc (vg_replace_malloc.c:236)
==11951== by 0x400EAC: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951==
...finished. …Run Code Online (Sandbox Code Playgroud) 为什么这两个结构定义编译正常:
struct foo {
int a;
} __attribute__((packed));
typedef struct __attribute__((packed)) {
int a;
} bar;
Run Code Online (Sandbox Code Playgroud)
虽然这个发出警告:
typedef struct {
int a;
} baz __attribute__((packed));
warning: ‘packed’ attribute ignored [-Wattributes]
Run Code Online (Sandbox Code Playgroud)
这一个给出了错误和警告:
typedef struct qux __attribute__((packed)) {
int a;
} qux;
error: expected identifier or ‘(’ before ‘{’ token
warning: data definition has no type or storage class [enabled by default]
Run Code Online (Sandbox Code Playgroud)
作为新手C程序员,最后两个定义不起作用的事实似乎是语言设计者/编译器编写者的一个相当随意的选择.是否有一个原因?我正在使用gcc 4.7.3.
我想source在一些shell脚本c使用exec().
什么是source?是一个binary executable还是一个shell script?我在哪里可以找到linux文件系统?
我跑了
charan@PC-113:~$ which source
charan@PC-113:~$
Run Code Online (Sandbox Code Playgroud) 我用 game.c 文件制作了一个游戏。之后,我制作了一个用于编译游戏的 Makefile。通过在终端中编写“make”,游戏编译,我也可以运行游戏。但是当我用“gedit game.c”重新打开 game.c 文件时,我收到一条警告:
**(gedit:5242):警告**:无法连接到辅助功能总线:无法连接到套接字/tmp/dbus-32NmbcAsWU:连接被拒绝
虽然我的 c 文件被打开了,但我收到了这个警告。我该如何解决这个警告?:/ :(
谢谢
我想我在这里遗漏了一些明显的东西.我有这样的代码:
int *a = <some_address>;
int *b = <another_address>;
[...]
int *tmp = a;
a = b; b = tmp;
Run Code Online (Sandbox Code Playgroud)
我想原子地这样做.我一直__c11_atomic_exchange在OSX上查看OSAtomic方法,但似乎没有任何东西可以原子地执行直接交换.他们都可以原子地将值写入2个变量中的1个,但不能同时写入两个变量.
有任何想法吗?
所以我试图在C中打印出一个字符串,当我打印出来时,我总是在字符串的末尾获得额外的字符.代码:
char binaryNumber[16] = "1111000011110000";
printf("binary integer: %s\n", binaryNumber);
Run Code Online (Sandbox Code Playgroud)
输出:
二进制整数:1111000011110000▒▒▒▒
你能帮我弄清楚为什么会这样吗?我认为这是我的代码中其他一些问题的根源.在我以更复杂的方式创建字符串之前我遇到了这个问题,并且在这种情况下也获得了额外的字符,但它们是不同的.所以我用最基本的方法制作了一个字符串(这里显示的方法),我仍然遇到问题
在Windows中,当您使用open dir和dirent打开文件时,它将按字母顺序为您提供文件,但是在Linux中,没有任何方法可以在Linux中对文件进行排序吗?
继续使用代码获取错误,这是从旧版VS到2010版的转换:
#include <string>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <math.h>
using std::string;
#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define POP_SIZE 100 //must be an even number
#define CHROMO_LENGTH 300
#define GENE_LENGTH 4
#define MAX_ALLOWABLE_GENERATIONS 400
//returns a float between 0 & 1
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))
//----------------------------------------------------------------------------------------
//
// define a data structure which will define a chromosome
//
//----------------------------------------------------------------------------------------
struct chromo_typ
{
//the binary bit string is held in a std::string
string bits;
float fitness;
chromo_typ(): bits(""), fitness(0.0f){};
chromo_typ(string …Run Code Online (Sandbox Code Playgroud) 就像我们可以做的那样,在fd上轮询/ epoll/select,我们不能在msg队列id上.我找到了一些非标准的方法来将msgqueue-id设为fd,但是后来它是非标准的.所以我的问题是,为什么linux极客,没有在msg队列id上实现poll/select?这会导致严重的问题吗?
我需要实现这种机制.我怎样才能做到这一点 ?