Sponsored Content
Top Forums Programming char constants vs. hard-coding Post 302227926 by redoubtable on Friday 22nd of August 2008 08:35:57 AM
Old 08-22-2008
Quote:
Originally Posted by otheus
It IS measurable. But even after 1/2 million invocations, it made almost no difference on a very slow (10-year old) machine.
I also did some testing and results were not conclusive that's why I said that. After ~0 calls to strrchr() in x86 with no forced preemption (kernel) and -20 nice level, 950 clock ticks happen in both cases (times()). I did the test 10 times, and results looked always the same.

Quote:
mostly wrong. The '@' literal is embedded in the machine instructions itself (for x86 architectures), so that's in the code segment. The "const" designation for a variable means the compiler can optimize that variable, for instance, by also "hard coding" the value inside instructions. However, I did not turn on optimizations. In my code, I defined the const char to be inside the main() call, meaning it would go on the stack. Do nothing is on the data segment. Finally, the call to strchr places both arguments on the stack. So the price of having a constant in an immediate instruction type is practically nullified by this.
I disagree, you're mixing things up. In elf32-i386 file format both cases store data in .rodata section (sections simplify my explanation). Although everything gets push()'d to the stack when a function is called, the places where data is push()'d FROM is the same. Let me demonstrate:
Code:
int
main ()
{
        write (1, "MOMMA", 5);
        return 0;
}
"MOMMA" string is stored in .rodata section, let's disassemble (objdump -D):
Disassembly of section .text:
...
080483b0 <main>:
...
 80483c1:       c7 44 24 08 05 00 00    movl   $0x5,0x8(%esp)
 80483c8:       00 
 80483c9:       c7 44 24 04 bc 84 04    movl   $0x80484bc,0x4(%esp)
 80483d0:       08 
 80483d1:       c7 04 24 01 00 00 00    movl   $0x1,(%esp)
 80483d8:       e8 13 ff ff ff          call   80482f0 <write@plt>
...
(items are push()'d in reverse order $0x5, $0x80484bc, $0x1)
Anyway, let's search for 0x80484bc :

Disassembly of section .rodata:
...
080484b8 <_IO_stdin_used>:
 80484b8:       01 00                   add    %eax,(%eax)
 80484ba:       02 00                   add    (%eax),%al
 80484bc:       4d                      dec    %ebp
 80484bd:       4f                      dec    %edi
 80484be:       4d                      dec    %ebp
 80484bf:       4d                      dec    %ebp
 80484c0:       41                      inc    %ecx
"\x4d\x4f\x4d\x4d\x41" = "MOMMA"

Thus demonstrating that despite "MOMMA" is used in main() (.text section), it is retrieved from .rodata section

Now, let's store "MOMMA" in a const var and call it from write():
# objdump -s test|grep -A 1 rodata
Contents of section .rodata:
 80484c4 03000000 01000200 4d4f4d4d 4100      ........MOMMA. 

So, the string is used in the .text section from .rodata section in both cases.

Quote:
WTF do you mean by a metaprogrammer??
I meant compiler programmers eheh

I agree with everything else you said.
 

8 More Discussions You Might Find Interesting

1. Programming

constants in C/C++

Hi all My question is related to following sample code which tries to change consant value by pointers.(I know it is wrong practice but i am surprised by mis-behaviour) The code: #include <stdio.h> int main() { const int x = 10; int *y; const int * const z = &x; y = (int *)&x;... (2 Replies)
Discussion started by: Shobhit
2 Replies

2. Shell Programming and Scripting

perl: eval and constants

is it possible to use eval to create constants in perl? i cannot seem to get anything to work, and my searches are turning up little to nothing. an example of what i am trying to do is this: 2 arrays: array 1: 'FOOD','NUMBER','OS' array 2: 'pizza','two','unix' loop through the arrays and... (5 Replies)
Discussion started by: effigy
5 Replies

3. Shell Programming and Scripting

I wanted to update a script, more dynamic (just say no to hard coding)...

currently it has the following: bdumpN=`ll /home/apps/oracle/admin/DBprod/bdump/DBprod_j* | grep "$Cdate" | wc -l` If I pass the DBname, I would not have to hardcode it in the script... I can capture the database name by adding the following: DBname=$1 The problem is, I have been unable... (2 Replies)
Discussion started by: mr_manny
2 Replies

4. UNIX for Dummies Questions & Answers

Sending mails to various users without hard coding the email IDS

Hi Can any one help me out ? I am trying to send an autogenerated mail with an attachment to bulk of users using 'MAILX' and 'UNENCODE' . I have used it as follows X " ( cat /sastemp/body.txt; uuencode Test.xls.gz Test.xls.gz ) | mailx -s 'Testing' ' abcd@yahoo.com , efgh@gmail.com ' " ... (9 Replies)
Discussion started by: manas6
9 Replies

5. UNIX for Dummies Questions & Answers

Operation with real variables and constants

Hello there, I'd like to define a variable b equal to 0.5/a where a=0.001, so I wrote something like that: a=0.001; let 'b=0.5/$a'; but it doesn't work... maybe because the variable a has a real value??? Any help will be appreciated!!!:D (1 Reply)
Discussion started by: Giordano Bruno
1 Replies

6. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

7. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

8. Web Development

Using LWP without hard coding password

Hi, I would like to read content from a https site. I have decided to use LWP module in perl. but it throwed 401 Authorization required error. i dont want to hard code the password in my perl code. Is there any way to achieve the authentication without hardcoding the password. Thanks,... (1 Reply)
Discussion started by: pandeesh
1 Replies
All times are GMT -4. The time now is 12:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy