GithubHelp home page GithubHelp logo

blog's People

Contributors

shuang-shuang avatar

Watchers

 avatar  avatar  avatar

blog's Issues

Java String

Java的传参都是值传递,对于引用类型会传递引用的值,但是子函数中得到的是独立的复制的值。
String str = "abc";
change(str);
public static void change(String s){
s = null;
}
上述代码s也指向abc,但是s和str 是独立的,改变s只会让s指向空,而不会改变str以及str的内容。另外String, Integer, StringBuffer等等都是final,他们指向的内容初始化后不会再改变,但是可以使指向他们的引用指向其他内容。

Java 的变量存储:
Stack: 一般变量及对象的引用。
Heap: new 生成的对象 (Includes static variables and method in PermGen section in HEAP, things changed in Java8)
Constant Pool: independent from stack & heap. store string constant per class file

C/C++ Language

Memory Layout for C:

Address: High Command Line & Environment Variables
Stack

                        Heap

                        Data Segment (global and static variables):
                                                 BSS (Uninitialized Segment, would be initialized to 0 by loader)

                                                 Initialized Segment (includes read only constant strings)

                        Text Segment (binary code)

Static & Extern
Static is used before local, global variables and functions to make them only visible in the same source file. static local variable also stores in data segments other than stack and life cycle is longer than function. Extern declare the variable is defined in another compile unit. function is by default decorated by extern;

inline & define
inline function tells the compiler to replace the function body with function calling other than switching context to save statck. inline is similar to normal function calling, it will check the parameters and easy
to debug. While macro does not check parameters and is potential unsafe, unable to debug. Thus,
not recommend to define complex functions.

Volatile
in C, this keyword tells compiler that the variable could be modified by other threads anytime, do not optimize its access (e.g save it in cache or register). Volatile has similar function in Java, however it does not guarantee atom operations, You should use synchronized keyword.

attribute
attribute((function/variable/type)) 放在函数申明的分号;之前,让编译器检查优化函数。
例如add(int a, int b)attribute((const)) 多次调用同一参数时会优化只调用一次。
int a__attribute__((aligned 8)) = 10; 8字节对齐分配

Function Pointers
int (_pointer_name) (int a, int b); a pointer point to function address just like variable or array.
int (_test) (int a, int b) = add;
test (2, 3) is equal to add(2, 3);
typedef int (myfunc) (int, int) is define a type myfunc as int () (int, int)
myfunc f = func1; use it as f(2, 3) = func1(2, 3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.