CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2009
    Location
    Midlands, United Kingdom
    Posts
    2

    Pointers / Destructor etc

    Hi Guys,

    Just wandered if there was anyway through Visual Studio or a third part app that will let me see what variables are currently still in memort when i am writing an apllication??

    I'm trying to get my head around memory allocation and pointers - in particular char* and const char* pointers!!

    basically what i'd like to see is;

    char* var[] = "Hello";
    var = "Hello again";

    Is "Hello" still in memory and now var points to "Hello Again" or have i modified the original "Hello" string and only one portion of memorty allocated??

    any help much appreciated.

    Dale

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Pointers / Destructor etc

    In the snippet you have given, both "Hello" and "Hello again" are called string literals, they are both const strings and cannot be modified (attempting to modify them will yeild undefined behaviour).

    Also, this line:
    Code:
    char* var[] = "Hello";
    is not valid C++, I expect you meant:
    Code:
    char* var = "Hello";
    That said, although the above is valid, it is better to write:
    Code:
    const char* var = "Hello";
    since the const better describes the state of the data to which the pointer is being assigned.

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Pointers / Destructor etc

    By the way, string literals have static storage duration (they will exist for the lifetime of the application or loaded dll in which they are contained).

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointers / Destructor etc

    Quote Originally Posted by PredicateNormative View Post
    Also, this line:
    Code:
    char* var[] = "Hello";
    is not valid C++, I expect you meant:
    Code:
    char* var = "Hello";
    Another alternative (with a different semantic meaning) would be
    Code:
    char var[] = "Hello";
    This will create a local, modifiable array just big enough to hold the contents of the string literal, which will be copied in. Note that although this array is modifiable, you can never put any string into it longer than 5 letters.

  5. #5
    Join Date
    Jul 2009
    Location
    Midlands, United Kingdom
    Posts
    2

    Re: Pointers / Destructor etc

    Thanks for the replies.

    It seems as though I've been getting muddled up with char arrays and pointers

    The book I'm learning from starts off using char arrays as appose to strings to try and show how to use pointers etc.

    Come to think of it i don't suppose any of you know of a good article about passing pointers and references to functions and class member functions / constructors??

    The ones i have found seem very over-complicated examples.

    Thanks

  6. #6
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Pointers / Destructor etc

    There's a book list on the forum somewhere, perhaps someone else will have more to suggest, my days of reaching introductory material were decades ago, so I have nothing to offer on your recent question.

    I wanted to go back an an unanswered portion of your first post.

    You want a debugger....

    You'll be able to see the locations and nature of all the objects running.

    I assume you're in Linux, so the command line oriented GDB is the "base line" option. An IDE would have a GUI front end to GDB, to make it easier to use.

    It's hard to beat the Visual Studio debugger, even the free "Express" edition (obviously that's going to mean Windows)
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Pointers / Destructor etc

    In addition to what has been pointed out above, you don't do this to change the values of a character array:
    Code:
    var = "Hello again";
    You will have to use strcpy or strncpy. According to the C++ standards [2.13.4/2]:
    Code:
    A string literal that does not begin with u, U, or L is an ordinary string literal, 
    also referred to as a narrow string literal. An ordinary string literal has type 
    “array of n const char”, where n is the size of the string as defined below; it has
    static storage duration (3.7) and is initialized with the given characters.
    So, the following would have been invalid in C++:
    Code:
    char* var = "Hello";
    "Hello" is an array of 6 constant characters. var is the pointer to the first element of the array and since that element is const, the pointer cannot be declared as non-const char*. The pointer has to be of the type 'pointer to a const char'. But to have backward compatibility with C where the above works an implicit conversion happens for array to pointer conversion where a string literal would be converted to an r-value of type "pointer to a char". This is however deprecated as per Annexure D. Section [4.2/2] says:
    Code:
    A string literal (2.13.4) with no prefix, with a u prefix, with a U prefix, or 
    with an L prefix can be converted to an rvalue of type “pointer to char”, 
    “pointer to char16_t”, “pointer to char32_t”, or “pointer to wchar_t”, respectively. 
    In any case, the result is a pointer to the first element of the array. This conversion
     is considered only when there is an explicit appropriate pointer target type, 
    and not when there is a general need to convert from an lvalue to an rvalue. 
    [ Note: this conversion is deprecated. See Annex D. —end note ]
    And annexure D.4/1 mentions: (also mentioned in note section above)
    Code:
    The implicit conversion from const to non-const qualification for string 
    literals (4.2) is deprecated.
    On a side note that might be related, when you declare a pointer, it is just a pointer to "something". That "something" might be already allocated or already "there" and declaring a pointer to point to it does not cause any allocation of memory for that "something". Pointer just points to the already existing memory/object. It is when you try to "copy" that object, new memory would be allocated for the copy apart from the existing one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured