我正在学习计算机工程,我有一些电子课程.我听到了,从我的两个教授(这些课程),有可能避免使用的free()
功能(后malloc()
,calloc()
等),因为分配可能不会被再次使用的存储空间分配等内存.也就是说,例如,如果您分配4个字节然后释放它们,您将有4个字节的空间可能不会再次分配:您将有一个漏洞.
我认为这很疯狂:你不能拥有一个非玩具程序,你可以在堆上分配内存而不释放它.但是我没有足够的知识来解释为什么它如此重要以至于每一个都malloc()
必须有一个free()
.
那么:在没有使用的情况下,是否有可能适合malloc()
使用free()
?如果没有,我该如何向我的教授解释这一点?
可能重复:
main()是否真的启动了C++程序?
可以在程序启动前调用我的函数吗?我该怎么做这项工作C++
还是C
?
Windows 7,Intel CORE i3,64位,RAM 4Gb,2.27 GHz
.NET Framework 4.0
我有以下代码:
static void Main(string[] args)
{
var timer = new Stopwatch();
timer.Start();
for (int i = 0; i < 0xFFF; ++i)
{
// I use one of the following line at time
Task.Factory.StartNew(() => { });
new Thread(() => { }).Start();
}
timer.Stop();
Console.WriteLine(timer.Elapsed.TotalSeconds);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
如果我使用Task,输出总是小于0.01秒,但如果我使用Thread,输出总是大于40秒!
这怎么可能?为什么这么大的差异?
在尝试理解右值引用的工作原理时,我最终得到了这段代码:
int* iptr = nullptr;
int*&& irr = iptr;
Run Code Online (Sandbox Code Playgroud)
编译上面的代码会出现以下错误:
错误:对'int*'类型的rvalue引用不能绑定到'int*'类型的左值
我理解这是正确的,但为什么下面的代码,我使用a void*
而不是int*
编译绑定,编译没有任何问题?运行时行为是正确的还是我应该期望未定义的行为?
int* iptr = nullptr;
void*&& irr = iptr;
Run Code Online (Sandbox Code Playgroud) 我需要在Windows窗体控件中托管Win32窗口.我遇到了与WPF相同的问题,我通过使用HwndHost
控件解决了这个问题.
我按照本教程:
Windows窗体中是否有任何等效控件?
我有一个Panel
和它的Handle
属性,我使用此句柄作为我的Direct2D渲染目标窗口的父级:
// Register the window class.
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
// Redraws the entire window if a movement or size adjustment changes the height
// or the width of the client area.
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = Core::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr;
wcex.hCursor = LoadCursor(nullptr, IDI_APPLICATION);
wcex.lpszClassName = L"SVGCoreClassName";
RegisterClassEx(&wcex);
hwnd = CreateWindow(
L"SVGCoreClassName", // …
Run Code Online (Sandbox Code Playgroud) 我有一个Eigen::MatrixXd
,我想通过应用组件功能来修改其所有元素.例如:
MatrixXd m = ...;
for each m[i][j]:
m[i][j] = exp(m[i][j]);
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个结果?
我有以下代码:
// vector of elements
vector<Graphic> graphics;
// vector of indexes of the selected graphic elements
vector<int> selected_indexes;
// vector according to which the graphic elements have to be "sorted" and parsed
vector<short> order;
for (auto o : order)
{
for (auto i : selected_indexes)
{
const auto& g = graphics[i];
if (g.position() == o)
{
// parse g
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个自定义元素的向量以及已经选择要解析的元素的索引,但是这些元素必须被解析的顺序取决于它们position()
根据第三个向量的值.
有没有办法改进这些嵌套循环,避免迭代迭代将被跳过的元素,因为它们的位置不等于当前的顺序?
我知道我的类方法有三种不同类型的实现"位置":
1)在我的类(.h文件)中定义方法并在我的.cpp文件中实现它
//.h
class Foo
{
int getVal() const;
};
//.cpp
int Foo::getVal() const
{ return 0; }
Run Code Online (Sandbox Code Playgroud)
2)在我的类(.h文件)中定义并实现该方法.
//.h
class Foo
{
int getVal() const
{ return 0; }
};
Run Code Online (Sandbox Code Playgroud)
3)在我的类中定义方法并在类外但在我的头文件中实现它.
//.h
class Foo
{
int getVal() const;
};
int Foo::getVal() const
{ return 0; }
Run Code Online (Sandbox Code Playgroud)
这三种方法有哪些主要区别?
我有这个类的列表:
public class Data
{
public string name {get; set;}
public int width {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个只返回name属性列表的方法.像这样:
public List<string> GetAllNames()
{ return MyDataList<name>.ToList(); }
Run Code Online (Sandbox Code Playgroud)
所以,如果我有这个清单:
name
= Jon - width
= 10name
杰克 - width
= 25我想要以下列表:
name
=乔恩name
杰克可能吗?
我正在开发的绘图应用程序Visual C++
通过以下方式Direct2D
.我有一个演示应用程序,其中:
// create the ID2D1Factory
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
// create the main window
HWND m_hwnd = CreateWindow(...);
// set the render target of type ID2D1HwndRenderTarget
m_pDirect2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_hwnd, size),
&m_pRenderTarget
);
Run Code Online (Sandbox Code Playgroud)
当我收到WM_PAINT
消息时,我会画出我的形状.
现在我需要开发一个代表我的新渲染目标的WPF控件(一种Panel)(因此它将替换主窗口m_hwnd
),这样我就可以创建一个新的(C#)WPF项目,其主窗口具有子窗口我的自定义面板,而渲染部分保留在本机C++/CLI DLL项目中.
我怎样才能做到这一点?我必须设置为新的渲染目标?
我想用我的WPF窗口的句柄:
IntPtr windowHandle = new WindowInteropHelper(MyMainWindow).Handle;
Run Code Online (Sandbox Code Playgroud)
但我需要在我的面板上画画,而不是在我的窗户上.
请注意,我不想将任何WPF类用于渲染部分(Shapes
,DrawingVisuals
...)