Thread: Challenging query regarding pointer

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Challenging query regarding pointer

    In the below code...

    Code:
     
    
    void main()
    {
         char *p="Hello";
         scanf("%s",p);
         printf("%s",p);
    }
    It is confirm that the first statment, the String Literal "Hello" goes in to read only data segment(.rodata).

    My doubt is when we enter a string in second statementi.e scanf, the entered string will store in which segment? is it stack or data.....???

    Please reply asap....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That results in undefined behaviour. The code attempts to modify a string literal. To help avoid such mistakes, p should be declared const char* not char* since it points to a string literal.

    By the way, use int main instead of void main, and return 0 if you want to be compatible with the 1990 edition of the C standard.
    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
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Hi laserlight.....I didn't asked you what happend to pointer p....I am asking you if we enter a string through scanf...where the entered string goes?.......(in stack or data segment)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it goes into the read-only arey causing the seg fault
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthik537
    I didn't asked you what happend to pointer p
    I did not tell you what happened to p either. I just told you that undefined behaviour is involved. Effectively, you asked a wrong question.

    Quote Originally Posted by karthik537
    I am asking you if we enter a string through scanf...where the entered string goes?.......(in stack or data segment)
    It goes to wherever the destination string has space allocated... but since there is undefined behaviour in this case, it depends on the implementation.
    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

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ganesh bala View Post
    All local variables and parameters of a function are declared in a part of memory called
    the stack......

    On the other hand, global variables and dynamically allocated memory comes from a
    different part of memory called the heap.

    Hope u understood this
    I hope not - as it's wrong - and not the question .

    Global variables are in the data section. Local variables are indeed in the stack section. Heap section is used when malloc() is called - that is, when dynamically allocated memory is used.

    As to the original question: Laserlight is right - it is undefined. The C standard doesn't clearly say how string literals are stored - as long as they are stored somewhere and work as the standard describes, it is entirely possible for the compiler to store them in any memory it likes - it can be writable, and it can be read-only memory.

    The reason the C standard doesn't specify how the string literal should be stored is that it would prevent a C compiler from being implemented on a machine that can't provide read-only memory for string literals - such as many small embedded systems or DOS-based PC's.

    In a modern OS, using a modern compiler, such as Windows or Linux with gcc(-mingw), the string literal will be stored in read-only memory, and an attemp to actually enter something into that memory will cause the application to have a access violation/segmentation fault.

    Of course, it is BETTER if the compiler can provide protection. If we assume the above code doesn't crash, what happens if we do:
    Code:
    void main()
    {
         char *p="Hello";
         char *q = "World\n");
         scanf("%s",p);
         printf("%s %s",p, q);
    }
    and then enter "LongWord"?
    Most likely, part of "World" would be overwritten! That's really not good, right?

    --
    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
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by karthik537 View Post
    In the below code...

    Code:
     
    
    void main()
    {
         char *p="Hello";
         scanf("%s",p);
         printf("%s",p);
    }
    It is confirm that the first statment, the String Literal "Hello" goes in to read only data segment(.rodata)...
    fact that a "string literal" is aka a "string constant" may have something to do with it.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    one way to 'fix' this problem is to tell the compiler to declare string litterals as 'const char*' (-Wwrite-strings with gcc, plus -Werror) which will prevent a statement like char*p="blah".

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You know it is interesting that if I declare p like so:

    Code:
    const char *p="Hello";
    And do

    Code:
    gcc -Wwrite-strings -Werror -o seg seg.c
    I get no warnings at all. If I use -Wall, I get a warning. From the gcc man page:

    Code:
    -Wwrite-strings
    When compiling C, give string constants the type const char[length] so
    that copying the address of one into a non-const char * pointer will get a
    warning; when compiling , warn about the deprecated conversion from string
    constants to char *. These warnings will help you find at compile time
    code that can try to write into a string constant, but only if you have
    been very careful about using const in declarations and prototypes.
    Otherwise, it will just be a nuisance; this is why we did not make -Wall
    request these warnings.
    Last edited by kermit; 01-30-2009 at 05:04 PM.

  10. #10
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Quote Originally Posted by laserlight View Post
    By the way, use int main instead of void main, and return 0 if you want to be compatible with the 1990 edition of the C standard.
    I see this code just works fine with most C compiler

    Code:
    func() {
      //...
    }
    
    main() {
      //...
    }
    Just GET it OFF out my mind!!

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I get no warnings at all. If I use -Wall, I get a warning
    If you add 'const', before, you get no warning from -Wwrite-strings, as expected (you get one if there is no const). With -Wall, what warning do you get?

    I see this code just works fine with most C compiler
    You shoud pay more attention when someone speaks of undefined/unspecified or implementation-defined behavior in this board.
    Last edited by root4; 01-31-2009 at 02:37 AM.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by root4 View Post
    If you add 'const', before, you get no warning from -Wwrite-strings, as expected (you get one if there is no const). With -Wall, what warning do you get?
    With -Wall, I get this:

    Code:
    seg.c: In function ‘main’:
    seg.c:6: warning: writing into constant object (argument 2)
    and the code looks like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
         const char *p="Hello";
         scanf("%s",p);
         printf("%s",p);
    
         return 0;
    }
    I removed the const part of the declaration, and got this:

    Code:
    kermit@fastbox ~/cprogs/board $ gcc -Wwrite-strings -o seg seg.c
    seg.c: In function ‘main’:
    seg.c:5: warning: initialization discards qualifiers from pointer target type
    However, when I use -Wall with the const removed, I get no warning at all..

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    the warning you get with -Wall concerns the scanf line (you're trying to modify a const-qualified object), the warning you get with -Wwrite-strings concerns the assignment (like char* p="hello" which is invalid with -Wwrite-strings and should be const char*), these are two differents things and the compiler behavior is the one expected (at least the one I expect).

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Oh I get it now. Thanks

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by audinue
    I see this code just works fine with most C compiler
    That relies on the deprecated feature of int being the default return type.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM