Sponsored Content
Top Forums Programming char constants vs. hard-coding Post 302227826 by otheus on Friday 22nd of August 2008 04:23:01 AM
Old 08-22-2008
Quote:
Originally Posted by redoubtable
Despite what everyone said I think speed differences between both cases are not mensurable.
It IS measurable. But even after 1/2 million invocations, it made almost no difference on a very slow (10-year old) machine.

Quote:
For one, memory is stored in the data segment in both cases thus it's accessed in the same way/speed.
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.

Quote:
Furthermore, this is highly platform/architecture/implementation dependent.
Architecture and processor, yes. For instance, while practically all processors have both an 'immediate' addressing and a 'direct' addressing mode, the difference in the number of clock cycles to process such an argument varies across architectures (surely), processor manufacturers (AMD vs Intel), and processor families (Pentium vs Celeron). There almost always IS a difference, but in very-large-pipelined architectures and efficient caching, that difference is statistically erased.

However, there's more dependency on the compiler. Whether the compiler chooses immediate mode or direct mode for literals, whether it uses direct or stack-indexed addressing addressing for constants, weather it passes the first argument in using a register or the last, etc, etc. The OS can come into play, too, especially with my program of 100000 lines of code. This likely meant there were page-traps during the execution. For this reason (and others), I took an average of several runs.

Quote:
We should call a meta-programmer to enlighten us with accurate specifications on the matter at hand.
WTF do you mean by a metaprogrammer??
 

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
SIGRETURN(2)                                                 Linux Programmer's Manual                                                SIGRETURN(2)

NAME
sigreturn, rt_sigreturn - return from signal handler and cleanup stack frame SYNOPSIS
int sigreturn(...); DESCRIPTION
If the Linux kernel determines that an unblocked signal is pending for a process, then, at the next transition back to user mode in that process (e.g., upon return from a system call or when the process is rescheduled onto the CPU), it creates a new frame on the user-space stack where it saves various pieces of process context (processor status word, registers, signal mask, and signal stack settings). The kernel also arranges that, during the transition back to user mode, the signal handler is called, and that, upon return from the han- dler, control passes to a piece of user-space code commonly called the "signal trampoline". The signal trampoline code in turn calls sigreturn(). This sigreturn() call undoes everything that was done--changing the process's signal mask, switching signal stacks (see sigaltstack(2))--in order to invoke the signal handler. Using the information that was earlier saved on the user-space stack sigreturn() restores the process's signal mask, switches stacks, and restores the process's context (processor flags and registers, including the stack pointer and instruction pointer), so that the process resumes execution at the point where it was interrupted by the signal. RETURN VALUE
sigreturn() never returns. CONFORMING TO
Many UNIX-type systems have a sigreturn() system call or near equivalent. However, this call is not specified in POSIX, and details of its behavior vary across systems. NOTES
sigreturn() exists only to allow the implementation of signal handlers. It should never be called directly. (Indeed, a simple sigreturn() wrapper in the GNU C library simply returns -1, with errno set to ENOSYS.) Details of the arguments (if any) passed to sigreturn() vary depending on the architecture. (On some architectures, such as x86-64, sigreturn() takes no arguments, since all of the information that it requires is available in the stack frame that was previously created by the kernel on the user-space stack.) Once upon a time, UNIX systems placed the signal trampoline code onto the user stack. Nowadays, pages of the user stack are protected so as to disallow code execution. Thus, on contemporary Linux systems, depending on the architecture, the signal trampoline code lives either in the vdso(7) or in the C library. In the latter case, the C library's sigaction(2) wrapper function informs the kernel of the location of the trampoline code by placing its address in the sa_restorer field of the sigaction structure, and sets the SA_RESTORER flag in the sa_flags field. The saved process context information is placed in a ucontext_t structure (see <sys/ucontext.h>). That structure is visible within the signal handler as the third argument of a handler established via sigaction(2) with the SA_SIGINFO flag. On some other UNIX systems, the operation of the signal trampoline differs a little. In particular, on some systems, upon transitioning back to user mode, the kernel passes control to the trampoline (rather than the signal handler), and the trampoline code calls the signal handler (and then calls sigreturn() once the handler returns). C library/kernel differences The original Linux system call was named sigreturn(). However, with the addition of real-time signals in Linux 2.2, a new system call, rt_sigreturn() was added to support an enlarged sigset_t type. The GNU C library hides these details from us, transparently employing rt_sigreturn() when the kernel provides it. SEE ALSO
kill(2), restart_syscall(2), sigaltstack(2), signal(2), getcontext(3), signal(7), vdso(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-09-15 SIGRETURN(2)
All times are GMT -4. The time now is 05:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy