Like Tree2Likes
  • 2 Post By grumpy

Recommend a text that explains the physical implementation of C

This is a discussion on Recommend a text that explains the physical implementation of C within the C Programming forums, part of the General Programming Boards category; I saw some truths in the popular thread below "why new programming students aren't fairing well." I am currently in ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    36

    Recommend a text that explains the physical implementation of C

    I saw some truths in the popular thread below "why new programming students aren't fairing well." I am currently in an online MSCS program, but my undergrad is in Aero Eng and not CS. Because of my different BS, in order to get into the MS program, I had to illustrate a very basic understanding of C, Java, and data structures. Of the 3 of those, I am definitely the weakest on C. Most of the rest of my MS program focuses on much higher level things (Databases, AI, etc), and although I passed the entrance requirements for C, I certainly do not feel like I understand it well enough for my own satisfaction. I feel as though I will never get a better education on the lower level stuff unless I chase it down on my own (since my MS just keeps focusing "higher"). I have had friends who are software developers say I can build a whole career on high level stuff only, and even if that is true, I am somebody who likes to understand things from the ground up, and right now my questions about C make me feel inadequate as a software-developer-to-be.

    I'm almost finished with my 2nd run through the K&R and am getting very good at the syntax for even the typical "difficult issues" such as pointers. However, one thing I've learned in all my frustration is that understanding pointers and the other tough parts of C requires understanding the physical side of what the code is actually doing. Memory allocation, stack, heap, memory leaks, etc... to me they all seem very important, and I only have a very vague understanding of how it all works... and K&R doesn't really touch on any of that. One question that always sticks out in my mind is why, historically, did programming evolve to use stack and heap in the way that it does? Surely it's not the only implementation that would work, but it is the one that has stuck. What little I do know about these issues, I've picked up from forum threads here, stackoverflow, and random tutorial websites. I'd like to read some kind of text though that presents a much more comprehensive look at the physical side of C, if that makes any sense. Can anybody point me in a good direction?

    Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For your reading request ... CLICK ... be warned there is a LOT of information there.

    As for some of the basic questions...
    Why the stack instead of the heap ... at one time it was a performance issue as most CPUs have registers built in for handling the stack directly. It's a lot faster to push a register to the stack than it is to use OS APIs to allocate memory for it. It's also a lot easier, for the same reasons.

    Memory allocation is actually a system API call... For Windows, check THIS It is definately slower than stack allocations but considerably more flexible.

    Yes there were other ways it could have been done. Pascal for example reserved heap space for variables but used it's own mapping scheme to make it almost as fast as stack allocations.

    Also keep in mind that C started out on single threading, single user systems and has stalwartly maintained reverse compatibility, for reasons I'll never understand. If it was up to me C would have gotten a string type on it's first revision; its such an obvious oversight.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by CommonTater View Post
    Also keep in mind that C started out on single threading, single user systems and has stalwartly maintained reverse compatibility, for reasons I'll never understand. If it was up to me C would have gotten a string type on it's first revision; its such an obvious oversight.
    Err.. Do you really think that Unix was single threading, single user system ? ..

    Also.. Character arrays and streams are somewhat like an Universal interface...and having a separate string type would only serve to add an extra layer of abstraction over that (that was the purpose why C++ was created ...but I won't mention that you again!).
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Err.. Do you really think that Unix was single threading, single user system ? ..

    Also.. Character arrays and streams are somewhat like an Universal interface...and having a separate string type would only serve to add an extra layer of abstraction over that (that was the purpose why C++ was created ...but I won't mention that you again!).
    No I don't think unix was single anything... DOS was... as were early versions of Windows... and that's where C really got it's toe hold in programming.

    Pascal had strings... real strings you could manipulate and test using = := < > etc. and I found it a whole lot more programmer friendly than C... It may be an extra abstration, but it's one I would happily welcome.

    And FWIW... C++ doesn't have a native string type either. It's still a library function using operator overloading to produce strings that require conversion to use in C based API calls.
    Last edited by CommonTater; 12-03-2011 at 05:36 PM.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    DOS was... as were early versions of Windows...and that's where C really got it's toe hold in programming.
    Are you out of your mind ? C was already nearly used for everything before DOS started walking !
    Quote Originally Posted by wiki
    In 1972, Unix was rewritten in the C programming language
    Dos wasn't even written in C ! ...and didn't have any sort of (AFAIK) facilities for C programming at least a decade or so.
    (Also..AFAIFbSTFW.. msdos was written in assembly..)


    And FWIW... C++ doesn't have a native string type either. It's still a library function using operator overloading to produce strings that require conversion to use in C based API calls.
    Define native.
    In OOP (And yes..I know your head does not work in that way..!), Class==Type ..and allow much more flexibility than if they were hard coded into the core language. (Also..even my debugger displays it like a native char array when queried during breaks .)
    And of course it'd require conversions to be used...in fact you can't avoid that when switching between strings of two different languages.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, I can see I've hit some kind of bizarre nerve here...

    Conversation over.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by CommonTater View Post
    Ok, I can see I've hit some kind of bizarre nerve here...
    Yes..the most bizarre one..
    It is called logic..btw!
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C was made a commercially successful and widespread language outside of Unix, that's true. It's creation and first success however, was in and for Unix.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by The111 View Post
    One question that always sticks out in my mind is why, historically, did programming evolve to use stack and heap in the way that it does? Surely it's not the only implementation that would work, but it is the one that has stuck.
    Stacks are used for function calls, where you want the program to do go off and do something and then come back to where it was and continue on. Any mechanism that will allow you to arbitraryily deep into function calls will do, e.g. linked-list. But no matter what the implementation, it is still a stack, because anything that is LIFO is a stack. A contiguous block of memory is the simplest and most efficient thing to use for a stack implementation. Stacks on some systems grow downwards and on other systems they grow upwards, either is fine.

    A heap is at its core something where memory can be allocated in any order and deallocated in any order. Various heaps on different platforms work differently, but they all do essentially the same job.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    CommonTater, let's just say the history you describe might have happened in a different universe, but not the one this forum exists in.

    As to the distinction between heap and stack.... they predated C by a long way, but have hung around.

    Historically, early computers have very limited memory resources, and (in software and hardware) the interaction with memory was colloquially via this thing referred to the stack (it was managed using a LIFO stack data structure with the help of some machine registers). It was not anticipated, at that time, there would ever be a need to build computers with more than a megabyte or so of memory. As hardware became more capable, and more memory installed, programmers wanted or needed to access that memory. Physically, the additional memory was distinct from what associated with the stack, so early operating systems managed this additional memory differently. So that additional memory had a range of names (depending on memory chips and how it was accessed via device drivers) including "high memory", "extended memory", "heap" and a few other names. The name "heap" eventually stuck.

    Later on, with more advanced memory management units, the distinction between heap and stack became a bit more academic, at least from a hardware perspective. However the names stack and heap stuck around. From an operating system and application perspective, the stack is now basically a small chunk of memory used as scratch space for a thread of execution - when a function is called, some data is allocated at the "top" of the stack (which literally implements a LIFO stack, so all that is necessary to allocate or deallocate memory from the stack is changing value of a pointer) to hold state, and popped off again when the function returns, in order to restore caller context and other things. Conventionally, it happens that local variables in a function also make use of the stack. The heap is now the memory set aside for dynamic memory allocation (malloc(), etc in C). There are no constraints on when memory is dynamically allocated, or when it is released, so dynamic memory allocation tends to take more work than an allocation on the stack (an allocation or deallocation involves more than setting a pointer).

    In modern operating systems, each application typically has its own stack, and the upper size limit is usually some relative small quota (usually allocated on a per-user or per-system basis by an administrator). The operating system allocates this memory while starting up the application and (hopefully) reclaims it when the application is terminated. With multi-threaded programs, each thread in an application is allocated its own stack when the thread is started (this often includes thread local storage). The heap, however, is generally directly managed by the operating system - applications dynamically request memory as they need it, and release it when done. Modern operating systems can reclaim heap when a program exits, but that takes a bit of work (and more primitive operating systems don't do it) which is why programmers are still encouraged to deallocate memory before terminating their program.
    Adak and The111 like this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good discussion on the heap and stack, Grumpy.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    36
    Thanks Grumpy, glad I got a couple good answers out of all the arguments I caused inadvertently!

    Still no books recommended, but on my own searching today I found this one... looks like it might be a good place to start.
    Amazon.com: Expert C Programming (9780131774292): Peter van der Linden: Books

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FAT and Physical Disk Information
    By siavoshkc in forum Tech Board
    Replies: 33
    Last Post: 09-02-2009, 12:57 AM
  2. physical address or logical ??
    By agarwaga in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2006, 02:36 PM
  3. Editing of physical memory
    By Sang-drax in forum Windows Programming
    Replies: 0
    Last Post: 11-15-2003, 09:09 AM
  4. menu physical height
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 11-24-2002, 08:29 PM
  5. Accessing physical memory
    By Greg in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2002, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21