Thread: A couple of Basic questions

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    A couple of Basic questions

    Hello,
    I would like to ask a few basic questions :

    1- When we first define a pointer , is it automatically NULL? I am asking this because if it was not NULL it would have to point somewhere , and this would mean another opened memory without our permission.I could try this by writing a small program , but I need an explanation which is detailed enough.
    2-Why is calloc bad? Yes I have read. I could not figure out why would it be problem for us if calloc sets the pointer to 0 ( NULL ? YES ?)?
    3-I still can not figure out the need for pointer when we write linked list or trees? ( The cruiosity may be because I have just started to read about data structres)

    I would be glad if you mentioned my faults , show me someway. It would make me move faster.Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    Hello,
    I would like to ask a few basic questions :

    1- When we first define a pointer , is it automatically NULL? I am asking this because if it was not NULL it would have to point somewhere , and this would mean another opened memory without our permission.I could try this by writing a small program , but I need an explanation which is detailed enough.
    The contents of a pointer is not necessarily NULL if it's not assigned to a value - but it doesn't mean that the contents is pointing to a valid (or useful) memory location. You can assign a pointer any value you like using typecasts too - e.g.
    Code:
    int *p = (int *) 1234;
    I now have a pointer than "points to" memory at address 1234. This doesn't mean that this memory locaiton is available to your application to use. It just means that the pointer contains 1234. Just like I can look out the window and read a number-plate on a car - doesn't mean that I own that car, right? So, like with the car registration, you have to "ask nicely" if you want a pointer to point to a memory location. Such as using malloc().

    2-Why is calloc bad? Yes I have read. I could not figure out why would it be problem for us if calloc sets the pointer to 0 ( NULL ? YES ?)?
    calloc isn't bad if what you want is "p = malloc(...); memset(p, 0 ...);". But if you allocate memory that will be filled in with other data than zero, you are wasting time filling the memory with zero only to overwrite it with some other data soon after.
    3-I still can not figure out the need for pointer when we write linked list or trees? ( The cruiosity may be because I have just started to read about data structres)

    I would be glad if you mentioned my faults , show me someway. It would make me move faster.Thank you.
    If you want to use a linked list, how are you going to refer to the next oject in the list, if you don't use pointers?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1- When we first define a pointer , is it automatically NULL?
    It depends. A good rule of thumb is never to rely on default initialization. Always set your pointers to NULL or another predictable location.

    >I could try this by writing a small program
    You could, and that's a good reaction. However, this is a case where the result can be implementation-defined, which is the premise of the guideline above.

    >2-Why is calloc bad?
    calloc is bad? That's news to me.

    >I could not figure out why would it be problem for us if calloc sets the pointer to 0 ( NULL ? YES ?)?
    calloc doesn't set the pointer to NULL, it zero initializes the memory that the pointer points to. The problem with calloc is that zero initialization doesn't necessarily mean 0 for the type being initialized. For example:
    Code:
    int **p = calloc ( 10, sizeof *p );
    
    if ( p[0] == NULL )
      printf ( "Life is good!\n" );
    This snippet is not required to print "Life is good!". You have to be careful with assumptions like that, and I imagine that's one of the reasons why you read that calloc is bad. The other is that the zero initialization can take extra time in an operation that's already slow.

    >3-I still can not figure out the need for pointer when we write linked list or trees?
    You don't need pointers for linked data structures. That's just the most common implementation. Here's a quickie linked list that doesn't use any pointers:
    Code:
    #include <stdio.h>
    
    struct node {
      int data;
      int next;
    };
    
    int main ( void )
    {
      struct node list[] = {
        { 0, 5 },
        { 5, -1 },
        { 2, 4 },
        { 4, 1 },
        { 3, 3 },
        { 1, 2 }
      };
    
      int link = 0;
    
      while ( link != -1 ) {
        printf ( "%d -> ", list[link].data );
        link = list[link].next;
      }
    
      printf ( "~\n" );
    
      return 0;
    }
    My best code is written with the delete key.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://c-faq.com/malloc/calloc.html
    Do not rely on all-bits-zero being valid representations of say pointers (NULL) or floats (0.0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    What I dont understand is , if it is not NULL when we first define a pointer , it would mean that another memory is used without our permission. Pointer already spends some memory , and the memory that it points to will spend another.??? I think I made my point.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1- When we first define a pointer , is it automatically NULL? I am asking this because if it was not NULL it would have to point somewhere , and this would mean another opened memory without our permission.I could try this by writing a small program , but I need an explanation which is detailed enough.
    Quote Originally Posted by ozumsafa View Post
    What I dont understand is , if it is not NULL when we first define a pointer , it would mean that another memory is used without our permission. Pointer already spends some memory , and the memory that it points to will spend another.??? I think I made my point.
    I think you're confused as to what a pointer is. A pointer is a variable, whose contents is an address. Initially, this pointer is not set to anything, so it will contain a "random" value. The address is invalid. This doesn't mean that an invalid address is taking up any memory. When you assign a pointer to point to something else, it's still taking up the same amount of space: the amount of space required to store an address.

    It's like a sign on the side of the road pointing in a random direction, saying that where it is pointing is the direction to a city. Pointing the sign so that it's actually pointing towards the city doesn't effect how far away the city is, or the road, or anything. It only effects the sign itself. And if you were using the sign to navigate, well . . .

    So maybe that's not the best example, but I hope you understand what I mean.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I knew everything you said. I am not confused about what a pointer is. I have just wondered.When I define a pointer , it does not m... Oh wait, I got it. It does not mean that the adress that pointer points will spend memory. Maybe it points to an empty memory. Not maybe , definitely. Okey I got it.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ozumsafa View Post
    When I define a pointer , it does not m... Oh wait, I got it. It does not mean that the adress that pointer points will spend memory. Maybe it points to an empty memory. Not maybe , definitely. Okey I got it.
    When I told you that pointers take up small parts of memory I meant that very literally. Whatever cluster of bits you got from a section in RAM by the time the execution of your program rolls around to
    Code:
    int main( void ) {
      /* ~> */  foo *p;
      print( p );
      return 0;
    }
    is exactly what you needed to store an address. When using uninitialized pointers you take a chance that an unknown pattern of bits equates to some address that your program has priveliges to and chose to house the pointed to object you wanted.

    Since this is never the case, point p at something specific before you use it.

    But no you can't necessarily type

    foo *p;

    and watch some sort of memory sinkhole.
    Last edited by whiteflags; 09-26-2007 at 04:07 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right. First of all, as has been explained before, a pointer is LITERALLY just an address to something.

    If you think of memory as a set of safety deposit boxes in a bank. Each has a number. Memory cells are the same, each memory cell has a unique number that reflecte ONE small "box" in memory.

    A variable is stored in a safety deposit box. That means that the variable has an ADDRESS - that is, which box it is stored in. Say we have a variable X, and it's stored at address 36. Safety box 36. Inside, we store the number 7. in C that would be "int x = 7;"

    Now, we'd like to, somehow, store the address of X. So we pick another safety box, say number 48. Inside 48, we store 36 inside this box. In C this would be "int *p = &x;",

    Then we give our friend the number of box 48. In C that would be "friend(p);".

    Our friend function can now get to box 48, find that it refers to box 36, and stick another number in there - say number 14. That is in C: "*p = 14;".

    Box 48 is still the same - it still contains 36. But box 36 now contains 14. But "friend" didn't know that we called our variable X.

    I hope this helps.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 02:50 PM
  2. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  3. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 02:48 PM
  4. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  5. couple questions for newb
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2001, 03:32 AM