以下是C++中单例模式的众所周知的实现.
但是,我不完全确定它是否是线程安全的.
基于此前问过的类似问题的答案,它似乎是线程安全的.
是这样吗?
//Curiously Recurring Template Pattern
//Separates a class from its Singleton-ness (almost).
#include <iostream>
using namespace std;
template<class T> class Singleton {
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
protected:
Singleton() {}
virtual ~Singleton() {}
public:
static T& instance() {
static T theInstance;
return theInstance;
}
};
// A sample class to be made into a Singleton
class MyClass : public Singleton<MyClass> {
int x;
protected:
friend class Singleton<MyClass>;
MyClass() { x = 0; }
public:
void setValue(int n) …Run Code Online (Sandbox Code Playgroud) 以下代码在C++编译器上编译.
#include<cstdio>
int main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;
};
struct yy *q;
};
Run Code Online (Sandbox Code Playgroud)
使用C编译器编译时,行为会有什么不同吗?
即会有任何编译器错误?
我知道动态分配/解除分配2D数组的算法,但是对于3D数组我也不太清楚.
利用这些知识和一点对称性,我提出了以下代码.
(我在编码期间很难在3D中进行可视化).
请评论正确性并建议任何更好的替代方案(效率方面或直观方面),如果有的话.
另外,我觉得这两个2D和3D阵列可被访问像正常一样arr2D静态数组[2] [3]和
arr3D [2] [3] [2].对?
二维码
//allocate a 2D array
int** allocate2D(int rows,int cols)
{
int **arr2D;
int i;
arr2D = (int**)malloc(rows*sizeof(int*));
for(i=0;i<rows;i++)
{
arr2D[i] = (int*)malloc(cols*sizeof(int));
}
}
//deallocate a 2D array
void deallocate2D(int** arr2D,int rows)
{
int i;
for(i=0;i<rows;i++)
{
free(arr2D[i]);
}
free(arr2D);
}
Run Code Online (Sandbox Code Playgroud)
3D代码
//allocate a 3D array
int*** allocate3D(int l,int m,int n)
{
int ***arr3D;
int i,j,k;
arr3D = (int***)malloc(l * sizeof(int **));
for(i=0;i<l;i++)
{
arr3D[i] = (int**)malloc(m * sizeof(int*));
for(j=0;j<m;j++) …Run Code Online (Sandbox Code Playgroud) c memory memory-management dynamic dynamic-memory-allocation
如果已知应用程序泄漏内存(执行时),那么在应用程序的源代码中找到此类内存泄漏错误的各种方法有哪些.
我知道某些解析器/工具(可能对代码进行静态分析)可以在这里使用但是有没有其他方法/技术可以做到这一点,特定于语言(C/C++)/平台?
我最近在接受采访时询问了有关虚拟功能和多重继承的对象布局.
我在没有涉及多重继承的情况下如何实现它的上下文中解释了它(即编译器如何生成虚拟表,在每个对象中插入指向虚拟表的秘密指针等等).
在我看来,我的解释中缺少一些东西.
所以这里有问题(见下面的例子)
示例代码:
class A {
public:
virtual int funA();
private:
int a;
};
class B {
public:
virtual int funB();
private:
int b;
};
class C : public A, public B {
private:
int c;
};
Run Code Online (Sandbox Code Playgroud)
谢谢!
c++ multiple-inheritance virtual-inheritance vtable memory-layout
fork用于创建从中调用的进程的副本.通常会调用exec系列函数.除此之外还有叉子的用法吗?我能想到一个.用管道功能做IPC.
在最近的一次采访中,我被要求在Linux机器上用C++实现一个线程安全的通用(基于ietemplate)堆栈.
我很快想出了以下内容(它可能有编译错误).
我完成了.面试官可能喜欢这个实现中的一些东西.也许是设计部分:)
以下是此实现可能具有的一些问题: -
1.表示溢出/下溢的实现不正确.因为我使用STL向量作为底层数据结构,所以没有溢出处理.应该有这样的处理吗?此外,下溢(在Pop()中)产生false作为返回值.应该通过抛出异常来完成吗?
2. PopElem例程的实现.以下实施是否正确?
3.没有真正使用顶级元素.
4.编写器和读者线程启动之间的更好时间.
请提出任何意见/建议/改进.
谢谢.
//实现线程安全的通用堆栈.
#include<pthread.h>
#include<iostream>
#include<vector>
using namespace std;
template<typename T>
class MyStack
{
public:
//interface
bool Push(T elem);
bool Pop(T& elem);
bool IsEmpty();
//constructor
MyStack() {
pthread_mutex_init(&lock);
top = 0;
}
//destructor
~MyStack() {
pthread_mutex_destroy(&lock);
}
private:
pthread_mutex_t lock;
int top;
vector<T> stack;
bool MyStack::Push(T elem);
bool MyStack::PopElem(T& elem);
}; //end of MyStack
template<typename T>
bool MyStack<T>::Push(T elem)
{
pthread_mutex_lock(&lock);
PushElem(elem);
pthread_mutex_unlock(&lock);
}
template<typename T>
bool MyStack<T>::Pop(T& elem)
{ …Run Code Online (Sandbox Code Playgroud) 在多线程编程上经历许多资源时,通常会出现对volatile说明符的引用.很明显,使用此关键字并不是在C/C++和Java(版本1.4及更早版本)中实现多个线程之间同步的可靠方法.以下是维基百科列出(不解释如何)作为此说明符的典型用法: -
我可以开始在上面列出的用法中看到这个说明符的作用,但由于我还没有完全理解这些区域中的每一个,我无法弄清楚这个说明符在每个用法中的行为.
有人能解释一下吗
我看过一本关于C++的书,提到使用静态转换导航继承层次结构比使用动态转换更有效.
例:
#include <iostream>
#include <typeinfo>
using namespace std;
class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};
int main() {
Circle c;
Shape* s = &c; // Upcast: normal and OK
// More explicit but unnecessary:
s = static_cast<Shape*>(&c);
// (Since upcasting is such a safe and common
// operation, the cast becomes cluttering)
Circle* cp = 0;
Square* sp = 0;
// Static Navigation …Run Code Online (Sandbox Code Playgroud)