可能重复:
为什么不能在switch语句中声明变量?
我的代码中有一个奇怪的错误:
char choice=Getchar();
switch(choice)
{
case 's':
cout<<" display tree ";
thetree->displaytree();
break;
case 'i':
cout<<" enter value to insert "<<endl;
cin>>value;
thetree->insert(value);
break;
case 'f' :
cout<< "enter value to find ";
cin>>value;
int found=thetree->find(value);
if(found!=-1)
cout<<" found = "<<value<<endl;
else
cout<< " not found " <<value <<endl;
break;
default:
cout <<" invalid entry "<<endl;;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010编译器说:
1>c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(317): error C2361: initialization of 'found' is skipped by 'default' label
1> c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(308) : …Run Code Online (Sandbox Code Playgroud) 我偶然发现了令我惊讶的行为:
写作时:
int x = x+1;
Run Code Online (Sandbox Code Playgroud)
在C/C++程序中(或者涉及新创建的变量x的更复杂的表达式),我的gcc/g ++编译没有错误.在上述情况下,X之后为1.请注意,前一个声明的范围中没有变量x.
所以我想知道这是否是正确的行为(甚至可能在某些情况下有用)或者只是我的gcc版本或gcc的解析器pecularity.
顺便说一句:以下不起作用:
int x++;
Run Code Online (Sandbox Code Playgroud) 假设我们有以下问题 - 我们想要读取一组(x,y)坐标和一个名称,然后按顺序对它们进行排序,方法是增加与原点(0,0)的距离.这是一个使用最简单的冒泡排序的算法:
#include<iostream>
#include <algorithm>
using namespace std;
struct point{
float x;
float y;
char name[20];
};
float dist(point p){
return p.x*p.x+p.y*p.y;
}
void sorting(point pt[],int n){
bool doMore = true;
while (doMore) {
doMore = false; // Assume no more passes unless exchange made.
for (int i=0; i<n-1; i++) {
if (dist(pt[i]) > dist(pt[i+1])) {
// Exchange elements
point temp = pt[i]; pt[i] = pt[i+1]; pt[i+1] = temp;
doMore = true; // Exchange requires another pass.
}
} …Run Code Online (Sandbox Code Playgroud) 在调试会话期间,我发现了一个问题,我能够减少到这个C++ 11代码:
#include <thread>
#include <vector>
#include <unordered_map>
class MyClass
{
public:
MyClass(){
printf("%p\n", this);
}
~MyClass(){
printf("~%p\n", this);
}
std::unordered_map<int, int> member;
};
thread_local MyClass threadLocalObject;
int main(){
std::vector<std::thread> threads;
for (int i = 0; i < 40; ++i){
threads.emplace_back([&](){
printf("%ld\n", threadLocalObject.member.size());
});
}
for (auto &thread : threads)
thread.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用的编译器是g++-6 (Homebrew GCC 6.4.0) 6.4.0打开的MacOS 10.12.6.问题是它似乎在析构函数中崩溃了member.
但它并不总是崩溃,这让我想知道它是否是线程之间的某种竞争条件.
任何帮助表示赞赏,这让我感到疯狂.
我在Windows上与MinGW有类似的崩溃,所以我真的希望从了解这里发生的事情的人那里获得一些知识.
这是此代码中更激烈的崩溃日志之一:
0x7f9b2ba00228
0x7f9b2ac02728
0x7f9b2ba004e8
0x7f9b2ae00128
0x7f9b2b800128
0x7f9b2ad00128
0x7f9b2ac025f8
0x7f9b2ad00178
0x7f9b2b900138
0x7f9b2b800288 …Run Code Online (Sandbox Code Playgroud) void update_memblock(MEMBLOCK *mb)
{
static unsigned char tempbuf[128 * 1024];
SIZE_T bytes_left;
SIZE_T total_read;
SIZE_T bytes_to_read;
SIZE_T bytes_read;
bytes_left = mb->size;
total_read = 0;
while (bytes_left)
{
bytes_to_read = (bytes_left > sizeof(tempbuf)) ?
sizeof(tempbuf) : bytes_left;
ReadProcessMemory(mb->hProc, mb->addr + total_read,
tempbuf, bytes_to_read, &bytes_read);
if (bytes_read != bytes_to_read) break;
memcpy(mb->buffer + total_read, tempbuf, bytes_read);
bytes_left -= bytes_read;
total_read += bytes_read;
}
mb->size = total_read;
}
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码,我最初正在阅读另一个进程'内存使用ReadProcessMemory.现在我存储了临时数据tempbuf.我能够以tempbuf十六进制形式输出数据.但是我打算如图所示显示它,另外我在这里遇到的另一个复杂因素是bytes_left> sizeof(tempbuf)我只读取相当于tempbuf大小的足够数据.如何读取更多数据,因为我定义的数组只能支持尽可能多的数据?
我有以下问题; 如果a是一个带有10个元素的int数组,我可以定义指针
int*b=&a[3];
int*c=&[2];
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用这些指针进行算术运算,int d=a-c;这将返回b和c之间数组中int值的数量.所以我的问题是,如果我也被允许对任何可能不在数组中的变量进行这样的指针算术运算.例如:
int a=10;
int b=20;
int*c=&a;
int* d=&b;
Run Code Online (Sandbox Code Playgroud)
然后做int e=d-c;或int*e=c+1;
我问的原因是我收到了有关这是否会导致未定义行为的相互矛盾的信息,