Click here to Skip to main content
12,952,273 members (65,751 online)
Click here to Skip to main content


2 bookmarked
Posted 3 Jun 2012

Ideas from a smart pointer(part 2). size == sizeof(std::shared_ptr)/2

, 3 Jun 2012 CPOL
embedded ref-counted resource management
#include "stdafx.h"

struct base

struct der: base
	virtual void fff()

int main()
	using namespace std;


	////pointer value can be changed
	der obj;
	base* p = &obj;

	if(p == &obj)
		printf("pointer is equal\n");

	if(unsigned(p) != unsigned(&obj))
		printf("value is not equal\n");
	//ref-counted smart pointer for build-in types
	RefNative<int>::Normal n = NativeAlloc<int>(2); //two int
	n[0] = 1;
	n[1] = 2;
	int* pn = n; //smart pointer to raw pointer

	printf("Current ref-count before push_back = %d\n", n.GetRefCount());

	std::vector<RefNative<int>::Normal> vec;
	vec.push_back(n); //auto InterlockedIncrement to make thread-safe. ref-counted pointer can be securely duplicated 

	printf("Current ref-count after push_back = %d\n", n.GetRefCount());

	printf("sizeof(RefNative<int>::Normal) = %d\n", sizeof(RefNative<int>::Normal));

	//alignment support
	RefNative<double, 64>::Aligned na = NativeAllocAligned<double, 64>(1); //allocate one 64 bytes aligned double 
	double* pna = na;
	pna[0] = 3.5;

	printf("sizeof(RefNative<int>::Aligned) = %d\n", sizeof(RefNative<int>::Aligned));

	if(unsigned(pna)%64 == 0)
		puts("it is properly 64 bytes aligned\n");

	return 0;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

China China
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 3 Jun 2012
Article Copyright 2012 by weibing
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid