What is an example in which knowing C will make me write better code in any other language?

前端 未结 20 1168
独厮守ぢ
独厮守ぢ 2021-02-05 12:40

In the Stack Overflow podcasts, Joel Spolsky constantly harps on Jeff Atwood about Jeff not knowing how to write code in C. His statement is that \"knowing C helps you write bet

20条回答
  •  旧时难觅i
    2021-02-05 13:26

    Classic examples are things involving lower level memory management, such as the implementation of a linked list class:

    struct Node
    {
        Data *data;
        Node *next;
    }
    

    Understanding how the pointers are used to iterate the list, and what they signify in terms of the machine architecture will allow you to better understand your high level code.

    Another example which Joel was referring to was the implementation of string concatenation, and the right way to create a string from a set of data.

    // this is efficient
    for (int i=0; i< n; i++)
    {
        strcat(str, data(i));
    }
    
    // this could be too, but you'd need to look at the implementation to be sure
    std::string str;
    for (int i=0; i

提交回复
热议问题