Search

3/17/2014

7. Memory : Stack vs Heap

7. Memory : Stack vs Heap

Stack vs Heap So far we have seen how to declare basic type variables such as int, double, etc, and complex types such as arrays and structs. The way we have been declaring them so far, with a syntax that is like other languages such as MATLAB, Python, etc, puts these variables on the stack in C. The Stack What is the stack? It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function). The stack is a "FILO" (first in, last out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables. The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more. What's more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast. A key to understanding the stack is the notion that when a function exits, all of its variables are popped off of the stack (and hence lost forever). Thus stack variables are local in nature. This is related to a concept we saw earlier known as variable scope, or local vs global variables. A common bug in C programming is attempting to access a variable that was created on the stack inside some function, from a place in your program outside of that function (i.e. after that function has exited). Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be store on the stack. This is not the case for variables allocated on the heap. To summarize the stack: the stack grows and shrinks as functions push and pop local variables there is no need to manage the memory yourself, variables are allocated and freed automatically the stack has size limits stack variables only exist while the function that created them, is running The Heap The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks. Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly. Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope. Stack vs Heap Pros and Cons Stack very fast access don't have to explicitly de-allocate variables space is managed efficiently by CPU, memory will not become fragmented local variables only limit on stack size (OS-dependent) variables cannot be resized Heap variables can be accessed globally no limit on memory size (relatively) slower access no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed you must manage memory (you're in charge of allocating and freeing variables) variables can be resized using realloc()

Threes! 原來算數學可以讓人上癮!熱門數學遊戲登陸 Android -電腦玩物

Threes! 原來算數學可以讓人上癮!熱門數學遊戲登陸 Android -電腦玩物

3/16/2014

.bash_profile vs .bashrc

.bash_profile vs .bashrc - .bash_profile 只有在 login 的時候執行一次, .bash_rc 每開一個 new terminal 都會執行一次

WHEN working with Linux, Unix, and Mac OS X, I always forget which bash config file to edit when I want to set my PATH and other environmental variables for my shell. Should you edit .bash_profile or .bashrc in your home directory? You can put configurations in either file, and you can create either if it doesn’t exist. But why two different files? What is the difference? According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells. What is a login or non-login shell? When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt. But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal. Why two different files? Say, you’d like to print some lengthy diagnostic information about your machine each time you login (load average, memory usage, current users, etc). You only want to see it on login, so you only want to place this in your .bash_profile. If you put it in your .bashrc, you’d see it every time you open a new terminal window. Mac OS X — an exception An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling .bash_profile instead of .bashrc. Other GUI terminal emulators may do the same, but most tend not to. Recommendation Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a PATH, you want it to apply to both. You can fix this by sourcing .bashrc from your .bash_profile file, then putting PATH and common settings in .bashrc. To do this, add the following lines to .bash_profile: if [ -f ~/.bashrc ]; then source ~/.bashrc fi Now when you login to your machine from a console .bashrc will be called.

http://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export

http://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export

export makes the variable available to sub-processes. That is, export name=value means that the variable name is available to any process you run from that shell process. If you want a process to make use of this variable, use export, and run the process from that shell. name=value means the variable scope is restricted to the shell, and is not available to any other process. You would use this for (say) loop variables, temporary variables etc. It's important to note that exporting a variable doesn't make it available to parent processes. That is, specifying and exporting a variable in a spawned process doesn't make it available in the process that launched it.
To illustrate what the other answers are saying:
al$ foo="Hello, World"
al$ echo $foo
Hello, World
al$ bar="Goodbye"
al$ export foo
al$ bash
bash-3.2$ echo $foo
Hello, World
bash-3.2$ echo $bar

bash-3.2$

node environment

add to ~/.profile

export NODE_ENV=production   

then in node
> process.env.NODE_ENV
'production'

then in express
> app.get('env') //will be 'production'

or:
> NODE_ENV=production node server.js

3/07/2014

同工不同酬?年薪 50 萬美金的工程師到底作哪些工作啊? | Winston Chen

同工不同酬?年薪 50 萬美金的工程師到底作哪些工作啊? | Winston Chen

他們究竟是作哪些事情,或是擁有哪些技術,讓他們如此值錢?這些東西有辦法用『學』的嗎? 首先,這篇是翻譯文章,原文在此:What kind of jobs do the software engineers who earn $500K a year do? 。我偶而會讀到幾篇我想要全文翻譯分享的文章,比如說上一篇祕技 - 沒有人教過你的應徵技巧跟這篇,翻譯分享之前,我都會直接聯絡原文作者,徵求同意,並作交換連結。一般而言(至少這兩篇拉),原文作者都會很啊薩力地大方同意,還會在來往的信件中不經意地露出他暗爽所受的內傷,總之我想說的是,不要亂去翻譯沒有版權的文章放到自己的平台上面,那內容不是你的,要轉載,要翻譯好歹也問一下原作者吧 XD 背景資訊 之前 Business Insider 出了一篇某個 Google 工程師拒絕年薪 50 萬工作,因為 Google 每年附他 300 萬美金的文章,有個對這數字很有興趣的人就上 Quora 問了,我要怎麼作才會跟他一樣?我也要到 Google 上班然後年薪 300 萬美金這樣(誰不想啊?) 這位叫 Amin Ariana 的創業家就上 Quora 寫了一則被讚到破表的回答,我自己非常同意,也受到很多啟發,因此跟 Amin 聯絡獲得允許,分享他的文章如下。 以下正文開始 聲明:我之前也是 Google 的員工,但是我的回答不代表 Google 的觀點。 首先,這問題問得有點奇怪,有點誤導人,好像只要工程師做了哪幾點,或是獲得哪些技能以後,就可以掛到年薪 50 萬的保證。其實 Business Insider 那邊說得很清楚了,50 萬美金其實是薪水跟股票的總和。 一類跟二類勞工 要了解高昂報酬背後的條件,讓我先先來打個比方。 假設你是村子裡面非常重要的,負責水源供給的勞工好了。這裡有兩種勞工類型:一類勞工,與二類勞工。 一類勞工會拎起一到兩個水桶,衝到水源旁邊,裝滿它們,把他們兩個挑回來,大概夠 20 個人喝吧,如此一來有水喝的村民就皆大歡喜了。這個勞工挑水的過程可能會喝掉一些水,然後回到村中,他可能可以分一些水回家作他的報酬。 二類勞工不太理所謂『公平分水』的概念,他會拿起一把鏟子,帶上一止水杯,然後忽然間就消失了。他跑到水源處,挖起一條可以通到村莊的小溪,希望可以把水源引過來。每當他拖著疲憊的身軀,拎著空杯子回到村莊的時候,總會引起一陣失望,但是不知道為什麼那村中的長老相信他,相信他在做的事情(還丟根骨頭給他啃,讓他不致餓肚子)。 某天,他直挺挺的站在村莊前面,他深後白涔涔地流躺著一條飲用水的小溪。這條小溪立刻把一類專門經營『水快遞』的勞工趕出市場,他們只好轉行,加入別的團隊。這個二類勞工呢,看他對這條小溪擁有多少的控制權,一般而言,他有小溪很大部分的擁有權。 後來村莊決定要把小溪整個買下來,整進整個村莊的供水系統,於是村莊拿了他們一部分的財產去換,比如說土地啊什麼的,這個二類勞工於是瞬間升級變成地主了。 村子裡面的媒體注意到村子給這個二類勞工的薪水奇高,別村的人根本挖不動他(他應該是有跟村子簽訂協議,比如要在村子裡留兩年,才能領完全額的報酬之類的),於是出了一篇報導,寫得好像別村出高價挖角,卻因為村子給的薪水太好,以致於這個二類勞工根本不會考慮。 這時候,一類勞工看了媒體報導,覺得村子虧待他們,同工不同酬(請看下面這個兩隻猴子同工不同酬的影片),心生不滿。 沙灘上的掘渠者 來說說一個真實的故事吧。 今年跨年的時候,我跑去 Monterey Bay 玩,沙灘上有個年輕人在挖洞,我饒有興致地站在高處看,我太太欣賞著沙灘美景,其他人根本不在意這個年輕人的舉動,沒有人理他。我指著他轉頭跟太太說:『你等著看,30分鐘以後,周遭的所有人都會加入,幫這個年輕人挖』 30 分鐘以後,他挖出了一條小渠道,從他沙灘上的城堡直挺挺的延伸到海邊有水的地方,希望把海水引入渠道,注入他的護城河。那渠道還不夠深,海水還進不來,於是年輕人忙著加深河道。又過了 5 分鐘,原本站在旁邊看熱鬧的小孩們開始加入,動手幫忙。10 分鐘以後,周遭的機個大人也開始挖掘。15 分鐘以後,一個靦腆的,拎著相機的外國人也投入幫忙。60 分鐘之內,這位二類勞工影響了 15 個一類勞工自願投入,一起把海水引入護城河。 文章開頭的照片就是我當時照的,永久地紀念我對個人力量的賭注。那個拿著紫色桶子的傢伙就是渠道的創始者,不過照片上看不出來就是了。 新聞報導總是很喜歡忽略很多真實的細節,這篇年薪 50 萬的報導就忽略掉『汗水並不等價』這個部分。二類勞工願意突破現狀,孤獨地,有時候可能還要挨餓一小段時間來引入村子賴以維生的水源,一類勞工則是用自己完成的工作與技能去交換薪水,兩者最主要的差異是冒險,而且不保證一定會回收。 村子裡有遠見的那群人可以說都是二類勞工(在 Goolge 裡面領高新的那群),他們篳路藍縷,以啟山林,連結了村莊的水源。這些拿很多股票的傢伙大概是下列其中一種: 在 Goolge 當初創立時,就已經負責創造其核心價值的那群人 自己業餘的時候玩玩自己的專案(side project),然後公司覺得超級有用,很有價值的那群人。[譯注:Gmaill 其實就是這樣從 Side Project 長成現在 Goolge 核心產品的。] 自己開新創公司,被 Google 買進來的 (比較少拉)不知道為什麼有辦法成為某種核心科技或是技能的唯一提供者 除此之外,這種待遇大都是憑空想像出來的,用來賣很多很多 Business Insider 文章的這樣(以台灣的例子來說,就是商周,還有今週刊那些 XD)。 價值 190 億美金的不錄取通知 每顆心都會歌唱,唱得不完整,直到另一顆心跟著附和。 - 柏拉圖 多謝大家的熱情支持,本文在 Quora 上面已經累積了 12 萬則瀏覽,Quora 真是太屌了。 我收到很多評論,有一部分跟我說上列的故事很難應用到他們的生活中,另一部分的評論問到跟公司談判股權的技巧,希望要到 50 萬美金收入的方式,其他的評論則說我這篇文章根本沒有回答到他的問題。大部分的評論者都是一類勞工,還在想怎麼作才能炒高自己的市場價值,獲得更高的『收入』。 那我再來說個故事吧,一個發生在上列文章出來一個禮拜之內的故事,希望這次會具體一點,比較好懂。 2009 年 5 月,有個一類勞工應徵 Twitter ,但是他被拒絕了,於是 2009 年 8 月,他又跑去 Facebook 應徵,他也被拒絕了,怎麼辦?他決定自己出來試試看,挑起二類勞工的大樑,從『增進人類溝通』的水源處,挖起那條之前拒絕他一類服務的那兩間公司都非常需要的小溪。 一路走來,他與跟他一起挖小溪的朋友影響了 55 人加入團隊,一起努力,村中的長老也丟了些骨頭給他,一開始只有 25 萬美元,接下來 8 百萬美元,眼看著小溪越來越成功,紅杉創投到後來注入 5000 萬美元的資金。 我寫這篇文章的 3 小時前, CNN 剛剛報導他們二類勞工的作品被 Facebook 以 190 億美金收購(你沒聽錯, 190 億美元)。 Facebook 買了 Whatsapp。而幫 Facebook 挖了五年小溪的 Brian Acton ,正式成為 Facebook 的股東,正是那個當初拒絕他工作申請的 Facebook 。 在他開挖之前(開 Whatsapp 公司之前),他曾經寫下了這兩條推特訊息: 推特總部不要我。沒有關係,反正從我家過去很遠。 Facebook 也不要我。不過那是個很厲害人們社交的好機會。我很期待接下來的人生旅程。 你覺得那 55 個人有需要去跟 Facebook 談判,要 50 萬美金的薪水嗎?還是你覺得當那些人獲利了結,要離 Facebook 出走時, Facebook 會砸下重金,並拿出股票來留人? 二類勞工不會去比較,或是談判薪水,因為他們不是在出賣他們的勞力給村莊(公司),他們賣的是被低估的財富,那些出價的村莊沒有別的選擇,只能拿出相對於這些財富的數字,這些二類勞工端出來的財富,可以讓村莊與自己雙雙受益(你看看 Facebook 往上衝的股價就知道)。 你可以想想,要賣掉你挖通的水源時,有沒有哪個村莊願意坐在談判桌的另一邊。當村莊決定買水的時候,薪水條中多出來的幾個零,都不過是基本條件罷了。 Amin Ariana 現駐矽谷,是個軟體創業家。