小编sep*_*p2k的帖子

C++ 代码的行为取决于编译器

我对 c 和 c++ 真的很陌生

我试着用结构来创建一个类似列表的结构,基本上可以包含浮点数和列表。

此代码可编译,但其行为因编译器而异:

  • 使用最新版本的visual studio社区,它输出5然后0.

  • 使用在线外壳,我得到5然后5

当向量通过函数时,我想要得到的第二个。

这是代码:

#include <iostream>
#include <vector>

using namespace std;

struct Box {
    Box(char t) {
        type = t;
    }
    union Value {
        float number;
        vector<Box>* elements;
    };
    Value value;
    char type;
};

Box newBox() {
    Box aBox('l');

    vector<Box> newVec;
    newVec.assign(5, Box('n'));


    aBox.value.elements = &newVec;
    cout << aBox.value.elements->size() << "\n";
    return aBox;

}

int main()
{
    Box test = newBox();
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ struct pointers

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

神秘编译(BASIC?)遗留程序

我有一个遗留程序,我假设是用 BASIC 编写的(一个以 .bas 结尾的文件)。好像已经编译过了!当在十六进制编辑器中打开时,字符串和注释都是可读的,而完成计算的部分则不可读。AFAIK,BASIC 是一种解释语言。

问题:

  • 过去是否有 BASIC 的编译器或运行时环境将编译后的输出存储为 .bas 文件?
  • 有反编译器吗?

basic legacy-code

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

同一程序的不同输出

使用 GCC 编译以下代码时,仅从用户获取 3 个输入,而使用在线 C 编译器编译则需要 5 个输入。为什么会出现这种情况??

#include <stdio.h>
struct student {
    char firstName[50];
    int roll;
    float marks;
}; 

int main() {
    struct student s[3];
    int i,n;
    //printf("Enter n students:\n");
    //scanf("%d",&n);

    // storing information
    for (i = 1; i <=5; ++i) {
        
        printf("Enter first name: ");
        scanf("%s", s[i].firstName);
        printf("Enter marks: ");
        scanf("%f", &s[i].marks);
    }
    printf("Displaying Information:\n\n");

    // displaying information
    for (i = 1; i <=5; ++i) {
        
        printf("First name: %s\n",s[i].firstName);
        
        printf("Marks: %.f", s[i].marks);
        
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c structure

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

Quicksort + Profiling

我正试图描述一个快速排序代码.代码如下:

qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
Run Code Online (Sandbox Code Playgroud)

请帮帮我!

profiling haskell quicksort

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

编译器行为异常c ++

我正在使用这个简单的程序.

#include<iostream>
using namespace std;
int main(){
        int arr[200]={0};
        int i = 200;
        arr[i] = 1;
        cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)

我希望结果是200; 在我的系统中,它的显示结果为1.任何人都可以解释异常行为.

c++

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

我正在通过一本书学习C++.我做错了什么?

我正在通过一本书学习C++.它告诉我键入这个编译并链接它,以便我看看我是否可以阅读它.但是当我运行它时会出现错误.有什么问题?

#include <iostream>  
int main()  
{   
int x = 8;      
int y = 6;      
std::cout << std::end1;     
std::cout << x - y << " " << x * y << " " << x + y;     
std::cout << std::end1;     return 0;   
}
Run Code Online (Sandbox Code Playgroud)

c++ hyperlink

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

评估顺序:未定义的行为?还是编译器缺陷?

我从C编译器得到以下测试代码的asm代码错误.这是由于未定义的行为?

void SimulatedTest(void)
{                                   
    if ( (a) || (b && c || d) == 1 )
    {
        i = 2;
    }
    else
    {
        i = 4;
    }
}
Run Code Online (Sandbox Code Playgroud)

标准说:

6.5.16分配操作员

操作数的评估顺序未指定.如果尝试修改赋值运算符的结果或在下一个序列点之后访问它,则行为未定义

C运算符优先规则

  1. ()
  2. ==
  3. || &&

对于问题情况:if((a)||(b && c || d)== 1)编译器按以下顺序计算表达式并生成错误的代码

1.(b && c || d) - > R1

2.R1 == 1 - > R2

3.(a)|| R2

但是,编译器会为以下情况生成正确的代码

案例1 : . 当没有关系'=='操作时

if ( (a) || (b && c || d) )//compiler generates expected code
Run Code Online (Sandbox Code Playgroud)

情况2:为逻辑OR运算添加括号时 …

c ansi undefined-behavior unspecified-behavior

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

CLion 的 CMAKE 错误

我是 C 编程的初学者,如果我在 CLion 上开始一个项目,我会收到此错误代码:

C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\danie\CLionProjects\untitled2
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is unknown
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_CXX_COMPILER:

g++.exe …
Run Code Online (Sandbox Code Playgroud)

c mingw cmake clion

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

这是什么语法?

(x_train, y_train), (x_test, y_test) = mnist.load_data()

这是 tensorflow 示例,但我不明白这意味着什么我知道 x_train、y_train、x_test、y_test 的目的,但我想知道它们是如何分配的。这是一种什么样的机制。谢谢

python-3.x tensorflow

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

在APL中打印前n个奇数

我刚开始探索APL.虽然熟悉C++和python,但我执行了一个简单的程序'打印前n个奇数',其中n是APL中的输入.请帮助APL程序员.

apl

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