Thread: String literal

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    String literal

    If you take the following function call:

    Value = GetVal("Please enter a value in the range 1 to 5: ");

    then the definition:

    int GetVal(const char *const Msg)

    When you are passing a string literal as shown in the function call, does the preprocessor allocate memory for this string so the local pointer of the function will hold the address of the string of the first element of the string?

    Thanks.
    Be a leader and not a follower.

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Donīt really sure about the memory thing but I do know that the compiler allocates memory for yours Msg char-pointer. That pointer will point at the first character element.

    Well lets look at a similair function

    int doSomeThing(int number)

    the argument that is send to doSomeThing doesnīt allocate new memory(Of course has it to be stores somewhere in the computer internally), but the parameter does. I would guess that the same principle would apply to a const char * const argument.

    My guess is that all rvalue doesnīt allocate memory but lvalues does.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    The local pointer will hold the address of the first element of the string regardless.

    The difference that you may be looking at is between char[] and a char* variable.

    char[] is, by definition, constant and, therefore, cannot be changed. A char* variable is mutable, i.e. can be incremented, for example.

    In this particular case, the char* variable is 'const' and is rendered the same as char[].

    As for the preprocessor, I don't know that it has anything to do with the allocation of memory here at all, but someone else may be able to elaborate on this point.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Well it would seem logical to me that the string "Please enter a value in the range 1 to 5: " is held in memory, else the char pointer would not be able to evaluate to the address of the string. The question is, does the preprocessor allocate this memory, or some other process?
    Be a leader and not a follower.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    The preprocessor is invoked with the # symbol, i.e. #include, #define.

    Since your string literal is passed as an argument from main() - supposedly - I would have to assume that memory allocation is a compiler directive and, therefore, memory is allocated by a process other than through the preprocessor.

    (I can negate one (the preprocessor), but can't give you anything more specific on the other, i.e. what the other process is.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Ah, thanks for the reply. I knew something must of been allocating the string memory, ta.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM