Thread: Pointers basics (C and other Programming Lamguages)

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Unhappy Pointers basics (C and other Programming Lamguages)

    Why will we use pointers? Where will we use pointers? Please give me some website where I can learn technical Stuff about pointers and use. MY friend who studied engineering said that RAM memory processing is faster than CPU Processing.
    My other friend said to refer books on Microprocessors because he said the logic of pointes are in those books. What shall I do?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why will we use pointers? Where will we use pointers? Please give me some website where I can learn technical Stuff about pointers and use.
    cprogramming.com itself has an introduction to pointers.

    MY friend who studied engineering said that RAM memory processing is faster than CPU Processing.
    I am not too sure what exactly is "RAM memory processing", but I am certain that accessing the registers on the CPU is faster than accessing cache memory which is faster than accessing main memory (i.e, the RAM).

    My other friend said to refer books on Microprocessors because he said the logic of pointes are in those books. What shall I do?
    Those books may help you understand where pointers come into the picture at machine level, but they will not help you understand the basics of using pointers in C. In fact, they will probably assume that you already have some understanding of pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from what Laserlight said and the links in that post:

    Pointers are a useful way to refer to a block of memory - the other option is to copy the entire block of memory - a pointer is 4 or 8 bytes and can fit in a processor register without problem. If you have a block of data that is (say) 256 bytes, there aren't enough registers on any modern processor to hold that data, never mind a JPEG image of 4MB or some such. Copying data from one place to another requires extra processing steps, when all you really want is "to know where the data is in the first place".

    We use "pointers" in daily life all the time - an invoice number, bill reference numbers, quote reference numbers, police crime reference, etc, etc. They are all a way to refer to a bigger "object" (the actual invoice, bill, quote, crime). In a similar way, we can in the computer, use a "reference" to a block of memory holding some data of some sort.

    I agree with Laserlight about the "memory processing" - there's no such thing as "processing" in the memory of a computer. And a modern processor operates around 2GHz, whilst the latest "standard" memory technology can output data around 800MHz, so a factor 2.5 difference in speed. [And the processor can run at this speed all day long, whilst the memory needs a "breather" to figure out what to do next every now and again - after each burst and more so on a new Row (Tras-to-cas for those in memory technology)]. This is why caches are so important to modern processors.

    But it's definitely faster to process some big item of data "where it is" and just pass around the location (pointer) in memory, rather than copying 4MB of data around when you need to pass the JPG image from one place to another in the code.

    A pointer is really just "the location in memory" for something. Since all data being processed by a computer is ultimately stored in memory somewhere, all data has an address where it's stored. Whether the software (and programmer) knows about this directly or not is of course a didfferent matter - some languages don't have a concept of pointers as such - there probably are pointers inside the implementation of the language, but the user never gets to see them. Java is one of those languages.

    --
    Mats

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    ^^ How do people get so smart???

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    ^^ How do people get so smart???
    By working many years on all sorts of programming projects? :-)

    --
    Mats

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Another pointer analogy is to think about URL links on a website as pointers to other websites. When you click on the link, your browser "de-references" the pointer and displays the website that it was pointing to.

Popular pages Recent additions subscribe to a feed