Showing posts with label c language. Show all posts
Showing posts with label c language. Show all posts

2010-09-29

2's complement

真是糟糕,這些基本的都遺忘了。

http://www.programmer-club.com.tw/ShowSameTitleN/c/40792.html


在十進位的寫法裡, 「負十二」的寫法是 -12, 用二進位來寫, 是 -1100. 這是數學上的概念.

問題是, 當數學上的概念要應用到電腦上時, 我們要用什麼方法來表示「負」這個概念.


2010-09-14

i++ , ++i

i=i+1的過程相當:
temp=i+1; i=temp;
i++的過程相當:
temp=i; i=temp+1; return temp;
++i的過程最簡單:
i增1然後return i 的值,一步完成,沒有給任何temp變量賦值:)

==========================================================
i++;是一個右值.而++i是一個左值就行了.而且盡量去用++i,因為對於類類型或大型的數據類型來做相關操作時會提高效率.

==========================================================

i++是指在執行該語句後才加1;++i是指加1以後再執行該語句;
==========================================================
最終的結果都是使i增加1。
i=i+1是最常見的也最簡單。
i++如果單獨使用與i=i+1相同,但是他可以作為表達式的變量。先引用i的值,然後i的值增加1;
++i如果單獨使用與i=i+1相同,但是他可以作為表達式的變量。在引用i的值之前,i的值增加1。

2008-03-25

指標的運算

Ref:http://caterpillar.onlyfun.net/Gossip/CppGossip/CppGossip.html

指標的加法與減法與一般數值的加減法不同,
在指標運算上加 1 ,是表示前進一個資料型態的記憶體長度,
例如在int型態的指標上加1,是表示在記憶體位址上前進4個位元組的長度

--
齁 明明知道的阿
究竟在想什麼 Orz

2008-03-13

define , const , enum , inline

應用常數最常見的問題有三個:第一個是到底要用 #define 或是 const?
第二個是 const string (literal) 的設定,第三個是若是函數參數為常數
型態的指標又如何?

我們先來解決第一個問題:到底要用 #define 或是 const?

答案是用 const。 為什麼?理由是因為使用 const 的話,compiler 才能
防止錯誤地使用常數。我們說過 #define 是一種編譯指示,而編譯指示是
以下圖的方式被處理的:

原始碼 (source code)

+ - - - - - - - - - - - - - - +
| 前置處理程式 (preprocessor) |
| ↓ 擴張後的碼 |
| 編譯程式 (compiler) |
| ↓ |
| 最佳化程式 (optimizer) |
+ - - - - - - - - - - - - - - +

中間碼 (intermediate code)

原始碼由前置處理程式處理之後,變成擴張後的碼 (expanded source),
然後才丟給 compiler 去編譯。前置處理程式是 cpp (C PreProcessor),
它也是由 gcc 偷偷地去呼叫的。前置處理程式會把所有的 #define 所產生
的常數直接代換進數值,也就是說像

printf ("%f\n", Pi);

這一行敘述,經過 cpp 處理之後就會變成

printf ("%f\n", 3.1415926);

這有什麼缺點呢?第一,cpp 並不會替你檢查型態是否正確。第二,compiler
根本看不到 Pi 這個符號 (因為被代換掉了),所以 compiler 產生出來的除
錯資料也沒有 Pi 這個符號,因此你若使用除錯器來替你的程式偵錯,也看不
到 Pi 這個符號,那意味著除錯變的非常地麻煩。所以盡量使用 const 來代
替 #define。

2008-02-26

Calling Convention 呼叫慣例

1. 因為函式呼叫牽涉到參數的傳遞, 所以並是只是單純跳到那個Address執行程式碼再跳回來這麼簡單, 呼叫副程式(函式)的主程式, 需要知道怎麼填參數,副程式(函式)才能接到參數後進行處理, 再將結果, 傳給主程式, 所以這段協定, 稱之為Calling Convention(呼叫慣例)

但因為程式類型的不同(assambly, c/c++ , passcal ,fortran, vc .....), 並因為平台不同(Windows,Linux,MacOS,Unix...), 最主要的是CPU的不同(x86,PowerPC,Sparc.....), 所以這種協定就有很多方式


# What Are Calling Conventions?
C++ provides a way of defining a function and a way of calling that function with arguments that soon become second nature to use. When the function and calls to it are compiled there are quite a few different ways in which the process of getting those arguments to where the function’s code can “see” them before executing the code can be performed. There are pros and cons to each, and some compilers have extensions which allow you do choose which is used.

Most of the time there is no need to do anything other than use the defaults or, in a few cases where you are required to match a binary specification, do what you are told. But you might want to make use of this in some cases, or simply understand what it is going on with calling conventions used by code you interface with.

#
Normally you don't have to think about a function's calling convention: The compiler assumes __cdecl as default if you don't specify another convention. However if you want to know more, keep on reading ... The calling convention tells the compiler things like how to pass the arguments or how to generate the name of a function. Some examples for other calling conventions are __stdcall, __pascal and __fastcall. The calling convention belongs to a function's signature: Thus functions and function pointers with different calling convention are incompatible with each other! For Borland and Microsoft compilers you specify a specific calling convention between the return type and the function's or function pointer's name. For the GNU GCC you use the __attribute__ keyword: Write the function definition followed by the keyword __attribute__ and then state the calling convention in double parentheses. If someone knows more: Let me know;-) And if you want to know how function calls work under the hood you should take a look at the chapter Subprograms in Paul Carter's PC Assembly Tutorial.

Ref:
http://daydreamman.blogspot.com/2007/01/calling-convention.html
http://www.hackcraft.net/cpp/MSCallingConventions/
http://www.newty.de/fpt/fpt.html
http://tw.myblog.yahoo.com/hyper0672/article?mid=11&prev=12&next=10

--
怪~不太懂

callback function

# What Is a Callback Function?
a callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made.

# Why Should You Use Callback Functions?
Because they uncouple the caller from the callee. The caller doesn't care who the callee is; all it knows is that there is a callee with a certain prototype and probably some restriction (for instance, the returned value can be int, but certain values have certain meanings).

If you are wondering how is that useful in practice, imagine that you want to write a library that provides implementation for sorting algorithms (yes, that is pretty classic), such as bubble sort, shell short, shake sort, quick sort, and others. The catch is that you don't want to embed the sorting logic (which of two elements goes first in an array) into your functions, making your library more general to use. You want the client to be responsible to that kind of logic. Or, you want it to be used for various data types (ints, floats, strings, and so on). So, how do you do it? You use function pointers and make callbacks.

A callback can be used for notifications. For instance, you need to set a timer in your application. Each time the timer expires, your application must be notified. But, the implementer of the time'rs mechanism doesn't know anything about your application. It only wants a pointer to a function with a given prototype, and in using that pointer it makes a callback, notifying your application about the event that has occurred. Indeed, the SetTimer() WinAPI uses a callback function to notify that the timer has expired (and, in case there is no callback function provided, it posts a message to the application's queue).

Another example from WinAPI functions that use callback mechanism is EnumWindow(), which enumerates all the top-level windows on the screen. EnumWindow() iterates over the top-level windows, calling an application-provided function for each window, passing the handler of the window. If the callee returns a value, the iteration continues; otherwise, it stops. EnumWindows() just doesn't care where the callee is and what it does with the handler it passes over. It is only interested in the return value, because based on that it continues its execution or not.

However, callback functions are inherited from C. Thus, in C++, they should be only used for interfacing C code and existing callback interfaces. Except for these situations, you should use virtual methods or functors, not callback functions.



[ref: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/#more]

2006-08-22

C++

  • a > b ? c : d




  • for
    {
    if()
    {
    if()
    {
    break;
    }
    }

    }



  • vector

  • v.push_back( );
    v.erase( v.begin() + i );
    v.pop_back( );
    v.clear( );

    2006-08-18

    Linking problem in VC

    Key word :
    #VC build linking hang
    #VC build linking stop
    #VC build linking finish

    Build not finished
    寄件人: "Ranga Narasimhan"
    日期: 2000年3月8日(星期三) 上午12時00分
    Temporary disable any antivirus if running.


    cannot stop a build :
    寄件人: Graham F
    日期: 2004年6月3日(星期四) 下午12時23分
    I have seen problems similar to this with VC++ 6 due to antivirus
    software. See http://support.microsoft.com/default.aspx?scid=kb;en-us;250670.


    trying to do a build freezes Visual C++
    寄件人: Gary Chang
    日期: 2004年6月12日(星期六) 下午4時21分

    Hi,

    Do you have any antivirus software installed on your system?
    Some time it will cause the problem:

    PRB: Visual C++ IDE May Appear to "Hang" During a Build Due to Anti-Virus
    Software
    http://support.microsoft.com/?id=250670

    Addtionally, another possible reason is that your system have some hardware
    conflicts, such as a device driver conflict, disabling drivers one by one or
    going to standard windows drivers will determine which driver is at fault.

    One work around to your scenario could be use a .BAT file and copy all the
    compile and link command/options to it. Then it would have to be run
    manually(command line) separate from the IDE process.

    Thanks!
    Best regards,

    Gary Chang
    Microsoft Online Partner Support

    MSDN 中文

    http://msdn2.microsoft.com/zh-tw/library/default.aspx

    MSDN Taiwan

    MSDN forum

    2006-08-17

    note

    1. An unhandled win32 exception occurred in xxxxx.EXE [xxxxx]. Just-In-Time debugging this exception failed.

    solution : VC setting
    Tools - Options - Debug - Just-in-time debugging enable

    result :
    "cannot execute program" error


    2. "cannot execute program" error

    solution :
    VC++ shows this message when it cannot find executable file. See Project - Settings - Debug - Executable for debug session.
    (The "Project - Settings - Debug - Executable for debug session")

    copy from http://www.experts-exchange.com/Programming

    result:
    My setting is correct. So back to problem 1 , there is still an error.

    3. An unhandled win32 exception occurred in xxxxx.EXE [xxxxx]. Just-In-Time debugging this exception failed.

    solution 1:
    disable Just-int-time debugging

    result 1:
    fatal error , response or not

    solution 2:
    enable Just-int-time debugging

    result 2:
    "cannot execute program" error


    +++++++++++


    Oh my god!

    Just-In-Time debugging


    An unhandled win32 exception in (Program) Just-In-Time debugging this exception failed with the following error; No installed debugger Just-In-Time debugging enabled. In Visual Studio, Just-In-Time debugging can be enabled from Tools/Options/Debugging/Just-In-Time.

    Check the documentation index for 'Just-in-time debugging, errors' for more information

    之前我因為運作program,visual studio經常想彈出debugger令我非常困擾,之後我刪掉了visual studio, 不過問題沒有改善,佢還是彈出debugger的東西...

    請問如何解決?





    您好:

    您的錯誤訊息主要是因為系統的設定還是會去找VS.NET成為預設的偵錯工具,但是您目前系統中已經沒有VS.NET了,所以才會出現這個訊息。

    上一篇沒能讓您了解,不好意思,請您進行以下步驟:

    點選[開始]>[執行],在"開啟"對話框中鍵入以下指令:

    drwtsn32 -i

    點選[確定],這個指令將會把Dr.Watson設定成預設的Debugger工具,這樣就不會讓您每次都會跳出這樣的錯誤訊息了。



    copy from Microsoft forum

    2006-08-16

    warning LINK 4098

    1. 如警告訊息所提示的,於 Linker 的 Command Line 中,加入 /NODEFAULTLIB:LIBC ,即可將警告訊息關掉。
    根據 MSDN 上之說法 ,吾人可以把 /VERBOSE:LIB 加入 Linker 的 Command Line 中,如此可以知道, Linker 到底搜尋了哪些 libaries ,把不要的 library 利用 /NODEFAULTLIB:library 關掉,可以讓 Linker 不要產生這個警告。

    NODEFAULTLIB:msvcrt.lib NODEFAULTLIB:libcmtd.lib NODEFAULTLIB:msvcrtd.lib 。

    此解決方案可說是一種鴕鳥心態的作法,因為我們只知道如何將警告訊息關掉,並不知道產生此訊息的真正原因為何?

    2.重新檢視專案中指定使用的 library ,是否有混用的情形。
    Linker 不允許將 debug 與 non-debug 版本混用,同時它亦不允許將 single-threaded 與 multithreaded 版本混用。

    以 shortie 的例子,發生問題的專案,於 debug configuration 中使用了 CUnit 的 release 版本之 library ,這就是問題的所在。

    shortie 只要重新編譯 CUnit ,令其產生 debug 版本的 library ,再使用於發生問題的專案, Linker 不需要加入任何 /NODEFAULTLIB 的選項,就不會產生這個警告訊息。

    copy fromCine


    +++++++++++++++++++++++++

    #Question:

    I get these link errors when linking with the Chilkat VC++ library:

    LINK : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification


    #Answer:

    There's a README.html file included with the distribution, which tells what additional Microsoft libraries need to be linked in. You should add these:

    wininet.lib, rpcrt4.lib, crypt32.lib, ws2_32.lib

    to your list of libs in your VC++ linker options.


    copy from chilkasoft

    2006-08-14

    assert

    Evaluates an expression and when the result is FALSE, prints a diagnostic message and aborts the program.


    void assert( int expression );

    Routine Required Header Compatibility
    assert ANSI, Win 95, Win NT

    Return Value

    None

    Parameter
    expression
    Expression (including pointers) that evaluates to nonzero or 0

    Remarks

    The ANSI assert macro is typically used to identify logic errors during program development, by implementing the expression argument to evaluate to false only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG. NDEBUG can be defined with a /D command-line option or with a #define directive. If NDEBUG is defined with #define, the directive must appear before ASSERT.H is included.

    assert prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero). The diagnostic message includes the failed expression and the name of the source file and line number where the assertion failed.

    The destination of the diagnostic message depends on the type of application that called the routine. Console applications always receive the message via stderr. In a single- or multithreaded Windows application, assert calls the Windows MessageBox API to create a message box to display the message along with an OK button. When the user chooses OK, the program aborts immediately.

    When the application is linked with a debug version of the run-time libraries, assert creates a message box with three buttons: Abort, Retry, and Ignore. If the user selects Abort, the program aborts immediately. If the user selects Retry, the debugger is called and the user can debug the program if Just-In-Time (JIT) debugging is enabled. If the user selects Ignore, assert continues with its normal execution: creating the message box with the OK button. Note that choosing Ignore when an error condition exists can result in “undefined behavior.” For more information, see Using C Run-Time Library Debugging Support.

    The assert routine is available in both the release and debug versions of the C run-time libraries. Two other assertion macros, _ASSERT and _ASSERTE, are also available, but they only evaluate the expressiosn passed to them when the _DEBUG flag has been defined.