Thread: A pointer-string question

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

    A pointer-string question

    Hello everyone ,
    I ' d like to ask a little question which still confuses me sometimes. :

    -------
    char *ptr;
    fgets(ptr,10,stdin);


    When I compile these codes my compiler ( Pellec C) will give an error which tells me that I have never assigned a value to ptr.
    I cant tell that is not right if I think that ptr is a pointer. It needs an adress of a string maybe or another char variable. But I heard that some compilers dont give this error. Is this really depending on my compiler? I am going mad becasue of this question everytime I sit in front of my computer. Yes I can use the features of c easily and I make sense with them. But I need to understand everything trully complete to feel comfortable when using them. If you helped , I would be glad..

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It will warn you that you never assigned anything to ptr, ptr is uninitialized and points to a random memory location (warning will depend on the warning level).
    It's also a good idea to initialize pointers to NULL,
    Code:
    char * ptr = NULL;
    ptr is only a pointer, it points to a memory location, memory which you possibly don't own.

    Either, allocate memory on the stack or the heap.

    stack:
    Code:
    char myArray[10];
    fgets(myArray, sizeof(myArray), stdin);
    or, the heap:
    Code:
    char * ptr;
    if((ptr = malloc(10 * sizeof(char))) != NULL)
    {
        fgets(ptr, 10, stdin);
        free(ptr);
    }else{
        /* failed to allocate memory */
    }
    Last edited by zacs7; 09-20-2007 at 03:24 AM.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    What does that "free" ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The memory allocated with malloc().
    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

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Now , I gotta ask . After we allocate the memory , does the pointer become just like a string with the size of that memory?

    And if we free the memory after we assigned character to the string from stdin , wouldnt we lose them? Or I got the intention of free wrong. Coz I mainly asked why do we use free?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    Now , I gotta ask . After we allocate the memory , does the pointer become just like a string with the size of that memory?

    And if we free the memory after we assigned character to the string from stdin , wouldnt we lose them? Or I got the intention of free wrong. Coz I mainly asked why do we use free?
    Yes, malloc will give you a "chunk" of memory that is the size you asked for (if there is memory available, obviously, if you ask for a HUGE amount of memory, there may not be enough to do that).

    And you need to free the memory to "give it back". And yes, you "loose" the stuff that the pointer was pointing to [scarily enough, you can actually use pointers some time after free, and the content will still be valid, then at some RANDOM point later it will be overwritten by some other data - so it is best to make sure you know what you are doing, because those bugs are very hard to figure out].

    --
    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.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    are we using free to gain some more space and the reduce the spent of memory?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    are we using free to gain some more space and the reduce the spent of memory?

    Yes, if you don't, you have what's called a "memory leak".

    Whilst short-running applications such as a little play application, it may not be necessary to ever free memory [1], it is a good habit to learn to do it properly. If you ever write an application that "never exits" (such as a server application), then leaking memory is a real problem, because eventually the system will fail to give you more memory.

    For the same reason, it is a good idea to check if the pointer that is given back from malloc is not NULL. If it is NULL, there isn't enough memory available to do what you want.


    [1] All memory is freed when the application exits.

    --
    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. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 12:33 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM