Thread: A basic question

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    A basic question

    consider the following code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const char *a = "abc";
        const char *b = "abc";
        
        if ( a == b )
            printf("Yes, we are the same!");
            
        getchar();
        return 0;
    }
    Is it guaranteed that the two pointers hold the same address? That's, is the address of the two "abc" the same?
    Last edited by Antigloss; 11-13-2005 at 03:48 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No they don't hold the same address. If you're talking about addresses, try this.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const char *a = "abc";
        const char *b = "abc";
        
        if ( &a == &b )
            printf("Yes, we are the same!");
    
        printf("%p \n%p", &a, &b);
            
        getchar();
        return 0;
    }
    Last edited by SlyMaelstrom; 11-13-2005 at 04:23 AM.
    Sent from my iPadŽ

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    No, I didn't mean the addresses of a and b, I meant the address of the two "abc".
    I meant the value that a and b hold.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Oh, Try this then.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const char *a = "abc";  // Reserves memory for 3 characters
        const char *b = "abc";  // Reserves memory for 3 more characters
        const char *x = &a[0];
        const char *y = &b[0];
       
        printf("%p\n%p\n", &x, &y);
            
        getchar();
        return 0;
    }
    Hmm... Let me think this over a bit. I'm all screwed up here.
    Last edited by SlyMaelstrom; 11-13-2005 at 04:54 AM.
    Sent from my iPadŽ

  5. #5
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    From the gcc manual

    `-fmerge-constants'
    Attempt to merge identical constants (string constants and
    floating point constants) across compilation units.

    This option is the default for optimized compilation if the
    assembler and linker support it. Use `-fno-merge-constants' to
    inhibit this behavior.

    Enabled at levels `-O', `-O2', `-O3', `-Os'.
    Because they're the same string, the compiler may choose (or may be told to) merge identical string constants, so writing "abc" twice in the code results in only one "abc" in the executable.
    In this case, comparing the pointers will produce a match, but not for the obvious reason (or intent)

  6. #6
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    So it's up to the compiler to do this, but not guaranteed by the standard. Right?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your first example was correct. Disregard everything Sly said. You are comparing the two values contained in the pointer, which are addresses. The comparison made in your example would be valid.

    Back to the question: The standard doesn't guarintee that two string literals of the same contents be in the same spot in memory. Purely a compiler issue.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM