Thread: Usage of string literals assigned by character pointer as a arguments.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Usage of string literals assigned by character pointer as a arguments.

    Hi Team,

    Want to the merits/demerits/risks in using the string literals which are being passed as argument to other routines.

    This string lietrals are directly assigned to the character pointer variable (with out any explicit memory allocation).

    Example code is as below
    Code:
    char *option = "-1 -i 1200 -f";
    file_write(option);
    In above code snippet , can the memory be intact in calling functions. As I believe we have not allocated explicitly any overide in the memory location pointing towards option can be a possibility.

    Thanks
    RK

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    When the compiler sees a string literal, what happens is something like this.
    Code:
    static const char __compiler_makes_up_a_unique_name__[] = "-1 -i 1200 -f";
    This is what actually allocates the space for your string, and is initialised with your string.

    Which transforms your code to
    Code:
    char *option = __compiler_makes_up_a_unique_name__;
    file_write(option);
    By way of an actual example
    Code:
    $ cat foo.c
    void foo () {
        char *option = "-1 -i 1200 -f";
        file_write(option);
    }
    $ gcc -S foo.c
    $ cat foo.s
    	.file	"foo.c"
    	.text
    	.section	.rodata
    .LC0:
    	.string	"-1 -i 1200 -f"
    	.text
    	.globl	foo
    	.type	foo, @function
    foo:
    .LFB0:
    	.cfi_startproc
    	endbr64
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	.cfi_offset 6, -16
    	movq	%rsp, %rbp
    	.cfi_def_cfa_register 6
    	leaq	.LC0(%rip), %rax
    	movq	%rax, -8(%rbp)
            // snipped for brevity
    At line 10, the compiler made up the name ".LC0", and allocated and initialised it's contents with your string (using the assembler .string directive).

    Line 24 loads the address of your string (aka the address of the symbol it generated for it), and line 25 is storing that address in your local variable 'option'.

    > can the memory be intact in calling functions.
    String constants exist for the life of the program (they have global scope).
    Even when say the 'option' variable has gone out of scope, the actual bytes representing your string still exist.
    So it's safe for a called function to make a copy of that pointer and it would still work.

    This on the other hand is NOT the same thing.
    Code:
    void foo ( ) {
        const char option[] = "-1 -i 1200 -f";
        file_write(option);
    }
    file_write needs to completely process the string before it returns.

    This would be a BAD file_write
    Code:
    const char *buffer[10];
    int index = 0;
    // save strings until the buffer is full, then write them all.
    void file_write(const char *s) {
        buffer[index++] = s;
    }
    Whilst it could work with the former, it definitely won't work with the latter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why can a string be assigned to a 'char' pointer?
    By Ronerote in forum C Programming
    Replies: 5
    Last Post: 08-14-2018, 03:41 AM
  2. hexadecimal and character string usage?
    By vlrk in forum C Programming
    Replies: 3
    Last Post: 01-03-2012, 11:08 PM
  3. Character literals incorrectly interpreted
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 04-05-2009, 05:35 PM
  4. Character literals in scanf
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 08-09-2008, 02:36 PM
  5. Edit a character string with an assigned variable
    By jeffdavis_99 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2005, 10:54 AM

Tags for this Thread