Why I can't initialize a variable in switch case block

This question already has an answer here: Why can't variables be declared in a switch statement? 23 answers The case statements in a switch() have the same semantics as goto : When you dispatch to a label in a switch - case , you effectively goto the case label. The reason for this is that cases in a switch - case aren't self-contained. You can fall-through from one case to another

为什么我无法在开关盒区块中初始化一个变量

这个问题在这里已经有了答案: 为什么不能在switch语句中声明变量? 23个答案 该case在语句switch()具有相同的语义goto :当你派遣一个标签switch - case ,您可以有效地goto的case标签。 原因是switch案件中的case不是独立的。 你可以从一个案件到另一个案件。 事实上, switch case goto ,你甚至可以写一个怪物,比如Duff's Device。 研究,直到你被吓坏了。 在C ++中,本地定义的对象在它们的定义点处进入作

Understanding stack frame of function call in C/C++?

I am new to C/C++ and assembly lang as well. This could also be very basic question. I am trying to understand how stack frames are built and which variables(params) are pushed to stack in what order?. Some search results showed that....compiler of C/C++ decides based on operations performed within a function. for eg if the function was suppose to just increment value by 1 of the passed int p

在C / C ++中理解函数调用的堆栈框架?

我是C / C ++和汇编语言的新手。 这也可能是非常基本的问题。 我想了解如何构建堆栈帧以及按什么顺序将哪些变量(参数)推入堆栈? 一些搜索结果显示.... C / C ++的编译器根据在一个函数内执行的操作来决定。 例如,如果函数假设只是将传递的int param和return的值递增(类似于++运算符),它会将函数的所有参数和本地变量放入函数中的寄存器并执行加法。 ...想知道哪个寄存器用于返回/按值传递?....如何返回引用? .....

C++ Function Call vs. New Blocks for Push/Popping on the Stack

I was reading about variable scope in C++ and encountered an interesting block structure: int main(int argc, char **argv) { int local; { // New level of scope int more_local; } return 0; } I understand that variables are popped out of the stack at the end of each block denoted by the closing curly brace } . I've also read that function calls also push their varia

C ++函数调用与新建模块在堆栈上进行推送/弹出

我正在阅读有关C ++中的变量作用域,并遇到一个有趣的块结构: int main(int argc, char **argv) { int local; { // New level of scope int more_local; } return 0; } 我知道变量会在每个块的结尾处由弹出式大括号弹出堆栈} 。 我也读过函数调用也将它们的变量推入堆栈,并在通过关闭大括号}表示的调用结束时终止: void foo() { int more_local; } int main(int argc, char **argv) {

OpenCV VideoCapture can't read from my webcam at all

I'm using OpenCV 2.4.6 on Ubuntu 13.04 (on an Acer C7 Chromebook), and I'm using a simple test program to see if my webcam will work with OpenCV. It works fine with Cheese and Skype, so I know that the webcam itself isn't the issue. Here is my code (which compiles without any errors): #include "opencv2/opencv.hpp" #include <stdio.h> #include <stdlib.h> using namespace

OpenCV VideoCapture根本无法从我的网络摄像头读取

我在Ubuntu 13.04上使用OpenCV 2.4.6(在Acer C7 Chromebook上),我正在使用一个简单的测试程序来查看我的摄像头是否可以与OpenCV一起使用。 它适用于奶酪和Skype,所以我知道摄像头本身不是问题。 这是我的代码(编译没有任何错误): #include "opencv2/opencv.hpp" #include <stdio.h> #include <stdlib.h> using namespace std; using namespace cv; int main(int argc, char *argv[]) { cv::VideoCaptu

questions regarding the design of std::initializer

I have some questions regarding the design of std::initializer_list . I didn't find answers in [support.initlist] . Why does it have an explicitly defined default constructor? Why this constructor is not constexpr ? Why the method size() is not constexpr ? Why there's no traits giving the size of initializer_list (like specializing std::tuple_size )? Why it's not possible t

有关std :: initializer设计的问题

我有一些关于std::initializer_list设计的问题。 我在[support.initlist]中找不到答案。 为什么它有一个明确定义的默认构造函数? 为什么这个构造函数不是constexpr ? 为什么方法size()不是constexpr ? 为什么没有特性给出initializer_list的大小(如专门化std::tuple_size )? 为什么不可能静态访问它的元素(如专门化std::get )? sizeof应用于initializer_list时会发生什么? 从C ++标准的第18.9节: 类

C++ delayed tasks

I want to implement a way to schedule a task to be executed at a later time. The interface would be similar to JavaScript's setTimeout(function, milliseconds) . In my application certain resources are owned by a thread. To avoid race conditions they must always be accessed from that same thread. If other threads want to access the resource they must dispatch a task object to the resource

C ++延迟任务

我想实现一种方法来安排稍后执行的任务。 该接口将类似于JavaScript的setTimeout(function, milliseconds) 。 在我的应用程序中,某个线程拥有某些资源。 为了避免竞争条件,他们必须始终从同一个线程访问。 如果其他线程想要访问资源,他们必须将任务对象分派给资源线程。 所以我需要解决的两个问题是: 将任务派遣到一个线程 延迟调用 第一个问题可以通过使用在消费端具有资源线程的无锁队列来快速解决。 (我使

Member function memory allocation stack or heap?

I'm trying to allocate an array as follows: class foo{ public: void func(){double arr[13][64][64][64];} }; int main() { foo* inst = new foo(); inst->func(); return 0; } I was under the impression from answer such as: Does this type of memory get allocated on the heap or the stack? that the array arr would be placed on the heap (as the instance of the class is on

成员函数内存分配堆栈还是堆?

我想分配一个数组,如下所示: class foo{ public: void func(){double arr[13][64][64][64];} }; int main() { foo* inst = new foo(); inst->func(); return 0; } 我在回答的印象中,例如:这种类型的内存是否分配在堆或栈上? 数组arr会放在堆上(因为类的实例在堆上)。 这似乎并不是这样,因为我得到了分段错误。 如果我将声明更改为:double * arr = new double [13 * 64 * 64 * 64]; (

Does delete call the destructor?

I have an class (A) which uses a heap memory allocation for one of it's fields. Class A is instantiated and stored as a pointer field in another class (B). When I'm done with object B, I call delete, which I assume calls the destructor... But does this call the destructor in class A as well? Edit: From the answers, I take that (please edit if incorrect): delete instance of B call

删除调用析构函数吗?

我有一个类(A)为其中一个字段使用堆内存分配。 类A被实例化并作为指针字段存储在另一个类(B)中。 当我完成对象B时,我打电话给删除,我认为这是调用析构函数......但是这是否也调用类A中的析构函数? 编辑: 从答案中可以看出(如果不正确,请修改): delete B实例B调用B ::〜B(); 它调用A::~A(); and A::~A应该明确地delete and A::~A所有堆分配成员变量; 最后将存储所述B实例的内存块返回给堆 - 当使用n

Does this type of memory get allocated on the heap or the stack?

In the context of C++ (not that it matters): class Foo{ private: int x[100]; public: Foo(); } What I've learnt tells me that if you create an instance of Foo like so: Foo bar = new Foo(); Then the array x is allocated on the heap, but if you created an instance of Foo like so: Foo bar; Then it's created on the stack. I can't find resources online to con

这种类型的内存是否被分配到堆或栈上?

在C ++的上下文中(并不重要): class Foo{ private: int x[100]; public: Foo(); } 我学到的东西告诉我,如果你像这样创建Foo的实例: Foo bar = new Foo(); 然后数组x被分配到堆上,但是如果您创建了Foo的实例,如下所示: Foo bar; 然后它在堆栈上创建。 我无法在网上找到资源来确认这一点。 鉴于您的示例稍作修改: class Foo{ private: int x[100]; int *y; pu

Difference between using the Stack or the Heap

This question already has an answer here: What and where are the stack and heap? 25 answers First of all - heap and stack are not c++ terms. They are implementation details. Implementations using stack and heap (which probably most - if not all - do) usually have an upper limit of the stack size. So placing huge variables on the stack can cause stack overflow (which typically leads to un

使用堆栈或堆之间的区别

这个问题在这里已经有了答案: 什么和堆栈和堆在哪里? 25个答案 首先 - 堆和堆栈不是c++术语。 他们是实施细节。 使用堆栈和堆的实现(可能大多数 - 如果不是全部的话)通常具有堆栈大小的上限。 因此,将大量变量放在堆栈上会导致堆栈溢出(这通常会导致不可预知的错误)。 然而,堆栈对堆有好处,所以只要你可以使用它 - 只要你不把巨大的变量放在堆栈上。 注意 - 大多数c ++容器,例如vector,list,deque,都