
Programming Language
文章平均质量分 50
CaspianSea
这个作者很懒,什么都没留下…
展开
-
Memory Layout of C++ Object in Different Scenarios
n this article, we will see the memory layout of different C++ Object. And how different storage & access specifiers affect this memory footprint. I am not going to discuss compiler augmented code, name mangling & working of any C++ mechanism relat转载 2021-10-21 22:19:59 · 220 阅读 · 0 评论 -
(C++)upcast的时候一定要用 dynamic_cast 吗?
如果是多继承,并且 upcast到不同的接口的时候,需要用dynamic_cast比如,下面的代码,第37行一定要用 dynamic_cast,因为它是从一个 基类接口转到另外一个基类借口。 1 class A 2 { 3 public: 4 virtual bool IsA(void) = 0; 5 protected: 6 int t_a; 7 }; 8 9 class B 10 { 11原创 2021-10-18 23:05:50 · 248 阅读 · 0 评论 -
Leetcode 错误:store to misaligned address
今天遇到了一个 leetcode报的错:Line 48: Char 21: runtime error: store to misaligned address 0x61400000009e for type 'char *', which requires 8 byte alignment [solution.c]0x61400000009e: note: pointer points here61 00 62 00 be be be be be be be be be be be be b...原创 2021-01-15 11:23:54 · 3664 阅读 · 0 评论 -
今天发现的两处代码问题
1. 插入排序void insert_sort(int *nums, int s, int e){ int i, j; for(i = s+1; i <= e; ++i) { j = i-1; int k = nums[i]; while( j >= s && nums[j] > nums[i])原创 2020-06-16 09:17:41 · 225 阅读 · 0 评论 -
POJ 1064: Cable master
Cable masterTime Limit: 1000MS Memory Limit: 10000K Total Submissions: 83304 Accepted: 16926 DescriptionInhabitants of the Wonderland have decided to hold a regional programmin...原创 2020-02-23 10:14:15 · 233 阅读 · 0 评论 -
format specifier floating point values in printf and scanf..
"%f" is the (or at least one) correct format for a double in printf. There is no format for a float, because if you attempt to pass a float to printf, it'll be promoted to double before printf rece...转载 2020-02-22 19:29:49 · 411 阅读 · 0 评论 -
free(): invalid pointer
代码如下:#include #include #include #define SIZE_BYTE 10int main( void){ int i; char *buff; buff = (char *)malloc( SIZE_BYTE); (char *)memset(buff,'a',SIZE_BYTE); for(i=0;i<SIZE_BYTE;i原创 2015-10-13 08:14:05 · 22173 阅读 · 1 评论 -
shared_ptr Analysis
Smart pointer uses RAII(Resource Acquisition is Initialization) idiom to manage resource. The C++ 11 standards accept it as first class library member, and it resides in namespace std, instead of n转载 2015-09-24 07:33:21 · 2776 阅读 · 0 评论 -
Dumping a C++ object's memory layout with Clang
http://eli.thegreenplace.net/2012/12/17/dumping-a-c-objects-memory-layout-with-clangWhen one wants to understand the memory layout of structures and classes, the C/C++ operatorssizeof and offset转载 2015-12-13 09:26:25 · 955 阅读 · 0 评论 -
Name Mangling and Function Overloading
The C++ provides a feature called function overloading. This enables you to write many functions with same name, provided each function takes parameters of different types. The lower level languages (转载 2015-12-17 01:55:28 · 607 阅读 · 0 评论 -
编译器对C函数的名字修饰
假设有个文件如下:$ cat test.c int foo(int a){ return 1;}编译如下:$ g++ test.c -c $ nm test.o 00000000 T _Z3fooi可见, c++ 编译器对名字做了修饰。再编译如下:$ gcc -c test.c $ nm test.o 00000000 T foo可见,原创 2015-12-17 04:28:21 · 1378 阅读 · 0 评论 -
Ubuntu 14.04 上安装 C Sharp 开发环境。
1. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EFecho "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /e原创 2016-01-26 08:46:43 · 3816 阅读 · 0 评论 -
C++对象 内存分布
代码如下: class T { public : T(); virtual int f(void); private : int d; };T::T(void){ d = 0x12345678;}int T::f(void){ return 12;}void x(T& t) {原创 2015-12-27 11:16:34 · 1034 阅读 · 0 评论 -
python获取帮助
1.help()>>> help()Welcome to Python 3.5's help utility!If this is your first time using Python, you should definitely check outthe tutorial on the Internet at http://docs.python.org/3.5/...原创 2018-03-09 01:21:28 · 613 阅读 · 0 评论 -
负数右移位
默认的情况下,右移的结果是向下取整的。比如,-3>>1, 移位的时候结果是 -2,而不是 -1;合理的结果应该是-1(取靠近0的整数值)。下面的代码展示了获得合理的结果的一个技巧:#include <stdio.h>int main(void){ int x = -12340; double y = (double)(x) / 16; in...原创 2018-09-20 07:53:24 · 864 阅读 · 0 评论 -
理解 extern "C"
extern "C"是为了让代码按照C的方式编译,链接。$ cat 1.h#ifdef __cplusplusextern "C" {#endifvoid foo(void);#ifdef __cplusplus};#endif$ cat 1.cpp #include "1.h"void foo(void){}$ g++ 1.cpp -c$ objd...原创 2019-06-14 01:29:48 · 353 阅读 · 0 评论 -
自己犯的一个错误。。
在做一道题目的时候,写了下面一行代码:ret = ret << 8 + data[i];本意是把 ret的值左移8位,然后加上 data[i].但是,实际上,因为 +的优先级高于 <<, 实际的结果是 把 ret 左移 ret+data[i]位。。查了半天才发现。。代码应该写成:ret = (ret << 8) + data[i];...原创 2019-06-14 01:36:30 · 198 阅读 · 1 评论 -
ARM assembler in Raspberry Pi – Chapter 1
In my opinion, it is much more beneficial learning a high level language than a specific architecture assembler. But I fancied learning some ARM assembler just for fun since I know some 386 assembler.转载 2015-10-13 00:48:07 · 823 阅读 · 0 评论 -
ARM assembler in Raspberry Pi – Chapter 3
We saw in chapter 1 and chapter 2 that we can move values to registers (using mov instruction) and add two registers (usingadd instruction). If our processor were only able to work on registers转载 2015-10-13 07:59:21 · 968 阅读 · 0 评论 -
保存不同类型的函数到map
就不解释了,直接上代码:/** * \file callback3.cc * \author Michael Egli * \date 08-Mar-2015 * \copyright 2015 wisol technologie GmbH * * Overview * ======== * * Shows how to store callbacks with differ转载 2015-06-24 07:13:15 · 2090 阅读 · 0 评论 -
C++ and The Perils of Double-Checked Locking: Part I
C++ and The Perils of Double-Checked Locking: Part I In this two-part article, Scott and Andrei examine Double-Checked Locking.Google the newsgroups or Web for the names of design patterns, and you’re转载 2015-06-27 09:57:02 · 955 阅读 · 0 评论 -
Memory Ordering at Compile Time
Between the time you type in some C/C++ source code and the time it executes on a CPU, the memory interactions of that code may be reordered according to certain rules. Changes to memory ordering are转载 2015-06-28 11:52:06 · 721 阅读 · 0 评论 -
Double-Checked Locking is Fixed In C++11
The double-checked locking pattern (DCLP) is a bit of a notorious case study inlock-free programming. Up until 2004, there was no safe way to implement it in Java. Before C++11, there was no safe转载 2015-06-28 12:15:19 · 937 阅读 · 0 评论 -
C++ and the Perils of Double-Checked Locking: Part II
In this installment, Scott and Andrei examine the relationship between thread safety and thevolatile keyword.In the first installment of this two-part article, we examined why the Singleton patt转载 2015-06-28 11:45:52 · 1033 阅读 · 0 评论 -
std::string crash的问题
今天遇到一个 std::string 字符串crash的问题。问题代码如下面:#include int main(void){ std::string str; char *str2 = 0x00; str = str + "hello" + str2; return 0;}crash的时候,调用栈如下:(gdb) bt full#0 strlen ()原创 2015-07-13 00:53:21 · 8032 阅读 · 0 评论 -
The virtual table
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. Thevirtual table is a lookup table of functions used to resolve function calls in a dynamic/late b转载 2015-07-11 16:12:12 · 726 阅读 · 0 评论 -
c++ 11 thread 初试
最新的 c++11标准整合进了 线程支持,下面写一个小程序测试一下。测试代码:#include #include void hello(void){ std::cout << "Hello concurrent world" << std::endl;}int main(void){ std::thread t(hello); t.join();}编译方法:原创 2015-07-15 01:31:55 · 1588 阅读 · 0 评论 -
查看C++类的虚表(vtable)结构
假定源文件如下:#include #include class IA{public:virtual ~IA() {}virtual void PrintA() = 0;virtual void PrintB() = 0;virtual void PrintC() = 0;};class A : public IA{public:A();~A();void Pr原创 2015-07-22 09:01:17 · 3053 阅读 · 0 评论 -
安装 Ruby的三种方法
1. 下载ruby源代码,编译,安装2. 使用发行版自带的安装包,安装3. 使用 rvm安装。这种方法如下:3.1curl -L https://get.rvm.io | bash -s stable这一步主要是准备 rvm 环境,并且下载 ruby最新版本。安装目录是 $HOME/.rvm3.2 source ~/.rvm/scripts/rvmrvm原创 2015-08-21 01:40:16 · 11337 阅读 · 0 评论 -
安装RVM失败: public key not found
错误是这样的:$ curl -L get.rvm.io | bash -s stable --ruby % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent原创 2015-08-20 07:37:52 · 9120 阅读 · 4 评论 -
RubyGems 镜像
为什么有这个?由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败。所以你会与遇到 gem install rack 或 bundle install的时候半天没有响应,具体可以用 gem install rails -V 来查看执行过程。这是一个完整 rubygems.org 镜像,你可以转载 2015-08-21 01:24:34 · 944 阅读 · 0 评论 -
安装 redmine遇到的两个问题
执行bundle install --without development test时,走到Installing mysql2 0.3.19 with native extensions这一步后就一直停在这里。后来发现是没有安装 mysql的dev文件。执行 sudo aptitude install libmysqlclient-dev后,就能继续往下原创 2015-08-20 09:03:42 · 3169 阅读 · 0 评论 -
编译器自动创建的析构函数
下面摘自 http://en.cppreference.com/w/cpp/language/destructorTrivial destructorThe destructor for class T is trivial if all of the following is true:The destructor is not user-provided (meanin原创 2015-09-26 12:25:45 · 942 阅读 · 0 评论 -
Compile with ARM Thumb2 to Reduce Memory Footprint and Improve Performance
ARM claims that Thumb-2 instructions (for ARM Cortex cores and all ARMv7 processors) provides performance improvements and code size optimization:Thumb-2 technology is the instruction set underlyi转载 2015-10-17 14:28:04 · 791 阅读 · 0 评论 -
C++11 Tutorial: Introducing the Move Constructor and the Move Assignment Operator
C++11 Tutorial: Introducing the Move Constructor and the Move Assignment OperatorAugust 6, 2012 byDanny Kalev 9 CommentsCopy constructors sounds like a topic for an article f转载 2015-06-21 16:32:07 · 1029 阅读 · 0 评论