Visual c ++,调试模式.
if(k>=0)
011D14CE cmp dword ptr [k],0
011D14D2 jl bez+28h (11D14D8h)
return true;
011D14D4 mov al,1
011D14D6 jmp bez+33h (11D14E3h)
return false;
011D14D8 xor al,al
011D14DA jmp bez+33h (11D14E3h)
Run Code Online (Sandbox Code Playgroud)
和平等的代码:
return (k>=0)?(true):(false);
011D14DC cmp dword ptr [k],0
011D14E0 setge al
Run Code Online (Sandbox Code Playgroud)
什么更快?当我在函数调用中使用第二个构造时,什么更快?
if(i>0)
Foo(true);
else
Foo(false);
Run Code Online (Sandbox Code Playgroud)
要么:
Foo((i>0)?(true):(false))
Run Code Online (Sandbox Code Playgroud)
?
下面是一个代码部分,无法根据数组是否为静态来检索数组的大小.
struct foo
{
static const char* const a[30];
const char* const b[30];
const int ia = __countOf(a) // compiles
const int ib = __countOf(b) // has compile errors related to initialization
};
Run Code Online (Sandbox Code Playgroud) 所以我正在编写一个处理输入文件中的值的程序.我的变量包括总数,taxtotal,小计等等.它们已经被声明和初始化了,但是我收到两条错误消息:"未初始化的局部变量'小计'使用过'"和变量"taxtotal"相同. 源代码:
#include "stdafx.h"
#include<stdio.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ifstream shoppingBasketFile;
shoppingBasketFile.open("HW3_Data.txt");
bool isTaxed = false;
char taxValue = 0;
char inPrice[64];
char name[128];
double price, taxtotal, subtotal, total = 0;
if (shoppingBasketFile.is_open())
{
// display the header info:
cout << "o Thank you for shopping at StuffMart" << endl;
cout << setw(3) << left << "o "
<< setw(20) << left << "Item"
<< setw(12) << "Unit Price"
<< …Run Code Online (Sandbox Code Playgroud) 这是我的代码;
#include <iostream>
using namespace std;
int main()
{
int *D = new int[20000];
D = { 0 };
cout << D[0];
}
Run Code Online (Sandbox Code Playgroud)
在cout语句中,无论数组的大小如何,我都会收到访问冲突错误.为什么?
我正在Visual C ++项目中编写一个函数,该函数以2000字节为增量通过WinAPI读取文件的内容,并将其作为std :: string返回。
当文件远大于缓冲区(例如100 KB)时,会出现问题,在有效数据中间,我在文件中的多个位置添加了垃圾。这是一个长0xcccccccc...序列,由3-4个其他字节终止,通常出现在一个单词的中间。否则,该功能不会失败,并且不会丢失任何有效数据。
我没有检查确切的位置,但似乎这发生在缓冲区大小增加(或缓冲区大小增加的乘数)上。如果我将缓冲区的大小增加到大于测试文件的大小,那么问题就消失了。是什么原因导致这种情况发生?我究竟做错了什么?
std::string read_file(std::string filename) {
HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
std::string errortext("Error opening " + filename + ", bad handle value: " + to_string((int)hFile));
MessageBox(hwnd, errortext.c_str(), "Error", 0);
return "";
}
char buffer[2000] = "";
std::string entire_file = "";
DWORD dwBytesRead = 0;
while (ReadFile(hFile, buffer, sizeof(buffer), &dwBytesRead, NULL))
{
if (!dwBytesRead)
break;
entire_file += buffer;
}
CloseHandle(hFile);
return entire_file;
}
Run Code Online (Sandbox Code Playgroud) 正如标题所述,我遇到了臭名昭著的unresolved external symbol链接器问题。
我已经移动了.h文件中的所有内容。模板类不再在.h和.cpp文件之间拆分。
我已将它们包含在主文件中。我已经三重四重检查了定义是否存在(显然)。
我的文件如下:
Utils.h:
只是一个漂亮的 coutvector和valarray。测试和工作。
#pragma once
#include <vector>
#include <valarray>
#include <ostream>
using std::vector;
using std::valarray;
using std::ostream;
template <typename ValueType>
ostream& operator<< (ostream&, vector<ValueType>);
template <typename ValueType>
ostream& operator<< (ostream&, valarray<ValueType>);
template <typename ValueType>
ostream& operator<< (ostream& out, vector<ValueType> v)
{
out << "Vec[";
for (int i = 0; i < v.size() - 1; i++)
out << v[i] << ", "; …Run Code Online (Sandbox Code Playgroud) 考虑一些使用未初始化堆栈变量的非常简单的代码(或更复杂的代码,请参见下文1),例如:
int main() { int x; return 17 / x; }
Run Code Online (Sandbox Code Playgroud)
这是 GCC 发出的 ( -O3):
mov eax, 17
xor ecx, ecx
cdq
idiv ecx
ret
Run Code Online (Sandbox Code Playgroud)
以下是 MSVC 发出的内容 ( -O2):
mov eax, 17
cdq
idiv DWORD PTR [rsp]
ret 0
Run Code Online (Sandbox Code Playgroud)
作为参考,以下是 Clang 发出的内容 ( -O3):
ret
Run Code Online (Sandbox Code Playgroud)
问题是,所有三个编译器都检测到这是一个未初始化的变量就好了 ( -Wall),但实际上只有一个编译器基于它执行任何优化。
这让我有点难堪……我认为几十年来为未定义行为而争论的所有内容都是为了允许编译器优化,但我看到只有一个编译器关心优化甚至是最基本的 UB 情况。
为什么是这样?如果我想要Clang 以外的编译器优化 UB 的这种情况,我该怎么办?有什么方法可以让我真正获得 UB 的好处,而不仅仅是任何一个编译器的缺点?
1显然,对于某些人来说,这对于SSCCE来说太过分了,无法理解实际问题。如果你想要一个更复杂的这个问题的例子,在程序的每次执行中都不是未定义的,只需稍微按摩一下。例如:
int main(int argc, char …Run Code Online (Sandbox Code Playgroud) 我有这个代码的问题.
我在代码中寻找的是随机获得"first"和"second"的结果并将结果放入文件中.
它的伟大工程,如果我不使用该文件运行它,我得到的所有正确的结果,但是当我尝试将结果保存在文件中,我只得到其中包含的第一个节点(第一,secnd).
这是代码:
#include<iostream>
#include <fstream>
#include<cmath>
using namespace std;
void main()
{
int first[100],secnd[100];
for (int i=0; i<100 ;i++)
{
first[i]=rand()%500; //random number from to 499
secnd[i]=rand()%500; //random number from to 499
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile <<first[i]<<" "<<secnd[i];
myfile.close();
}
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main()
{
char* p=new char[10];
p="sudheer";
char* q=new char[10];
q=p;
delete []p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的程序时,它崩溃与删除.任何意见?
我编译我的代码没有任何错误.但是,当我尝试运行该程序时,会弹出一个包含以下内容的窗口:
Program: C:\Windows\SYSTEM32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring
Line: 1168
Expression: invalid null pointer
Run Code Online (Sandbox Code Playgroud)
我不确定问题是什么或它可能是什么.我绝对肯定地知道标题非常好,因为它是由教授提供的,我们不会触及它.
意图
代码的目的是从用户获取二进制输入并显示十进制等效值.然后取十进制输入并将其打印为二进制数.
码
主要
#include "stdafx.h"
#include "LinkedStack.h"
#include <string>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string binaryNum = "0"; // Holds the entered binary value
string decResult = "0"; // Holds the decimal result
int binResult = 0, pow = 1; // Holds the binary result and the power of 1 for conversion
int decimal = 0; …Run Code Online (Sandbox Code Playgroud) c++ ×10
visual-c++ ×10
c++11 ×2
optimization ×2
assembly ×1
c++builder ×1
createfile ×1
gcc ×1
linker ×1
performance ×1
readfile ×1
string ×1
templates ×1
winapi ×1