473,899 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Increasing efficiency in C

As everybody knows, C uses a zero delimited unbounded
pointer for its representation of strings.

This is extremely inefficient because at each query of the
length of the string, the computer starts an unbounded
memory scan searching for a zero that ends the string.

A more efficient representation is:

struct string {
size_t length;
char data[];
};

The length operation becomes just a memory read.
This would considerably speed the programs. The basic
idea is to use a string type that is length prefixed and
allows run-time checking against UB: undefined
behavior.

Comparing strings is speeded up also because when
testing for equality, the first length comparison tells
maybe the whole story with just a couple of
memory reads.

A string like the one described above is not able to
resize itself. Any pointers to it would cease to be valid
when it is resized if the memory allocator is forced to
move memory around. The block where that string was
allocated is bounded by another blocks in memory, and
it is not possible to resize it.

A pointer ( an indirect representation) costs a sizeof(void *)
but allows to resize strings without invalidating the pointers
to them.

struct string {
size_t length;
char *data;
};

There is no compelling reason to choose one or the other.
It depends on the application. In any case, the standard
library could be complemented by
Strcmp
Strcpy
etc., all using length prefixed strings.

Syntactic sugar.

I have added some sugar to this coffee. I always liked coffee
with a bit of sugar. I feel that is too acid without it.

Current strings are used using the [ ] notation. This strings
could have the same privilege isn't it?

The language extension I propose is that the user has the right to
define the operation [ ] for any data type he/she wishes.

Not a big deal for today's compilers.

Length checked strings can then use:

String s;
....
s[2] = 'a';

I think I am proposing the obvious.

Do you agree?

jacob
Nov 14 '05
100 3672
Groovy hepcat jacob navia was jivin' on Wed, 3 Mar 2004 23:07:25 +0100
in comp.lang.c.
Increasing efficiency in C's a cool scene! Dig it!
As everybody knows, C uses a zero delimited unbounded
pointer for its representation of strings.


Wrong! The term "zero delimited unbounded pointer" is meaningless.
There ain't no sech animal.
As everybody who knows about C knows, C uses contiguous sequences of
characters, each such sequence terminated by a null character, for its
representation of strings.
[Snip rest of the post I didn't bother reading.]

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #91
"Dik T. Winter" wrote:
Mark McIntyre <ma**********@s pamcop.net> writes:
...
In the UK exchanges were known by name eg Oxford, Inverness etc,
and I'm given to believe that the original dialling codes were
based on three of the letters in their names, converted to
digits. This link has long gone.


That link has lasted only a few months (and it was two letters).
OXford would have area code 009x (in the UK the letter O was on
the digit 0), but that was changed very soon, together with other
exchanges that started with letters O or Q. It was changed
before I even became interested in this subject. On the other
hand I remember the local Parisian exchange OPEra. So a local
number starting with 072, which lasted a bit longer.

Letters on telephones have gone out of use in Europe in the early
to mid 60s.


There is the difference. Here in leftpondia we have rabid
conservatives, so the letters never left the dial or the keypads,
although they did leave the telephone directories. So every child
can learn a slightly truncated alphabet from the telephone. This
proves that rabid conservatism is not entirely bad. :-)

Some pads here add the Q to the 7 key, and the z to the 9 key.
All this allows automated systems to ask callers to spell the name
of the party they want, etc., usually with success. Any move to
take letters off the keypads would cause all sorts of havoc.
Those systems are usually programmed in C. And you thought this
was OT :-)

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #92
Jacob, in your proposal what should be the
default behavior if an array bounds is
exceeded? Abort the program?


Depends which compile time option you used when building the library.
By default the program aborts.
If you compile it without exception handling, the called function will fail,
returning false or doing any other sensible action.

In critical applications, this behavior is better than trashing
the return address or making other catastrophical actions.

For instance in a machine where failure is not an option,
this will allow the program to go on, in better conditions
than now. The string operation will fail, but the integrity
of the program is preserved.

Assuming that the String "S" as a capacity of 5, the
operation Strcpy(s,"a very long string") would result
in a truncated copy. This can provoke errors later, but
it is better than destroying data that lies after the
storage reserved by "S"
Nov 14 '05 #93
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ************@un ix44.andrew.cmu .edu...
The problem is you just can't ignore them, because on a project of
any size others will start to use them.
Solution 1: Don't care what the other guy is doing; that's *his*
part of the project, and you only need to know the interfaces. Make
sure the interfaces are all written using standard C types and
passing mechanisms, so that modules can talk to each other with
some sort of reliability.


Yeah sure. No bugs will ever be found, this other guy will always work
there, never be sick and never take a holiday. We can all sit on our own
island and just pass C types to each other.
Solution 2: Tell the other guy up front that he shouldn't use
the complicated parts of C++. Or better, get your boss to tell
him.


Yes, tell the other guy. Go tell! They won't start a discussion of course
and then *you* can start using the features that *you* think are useful.
Don't forget to tell the other guys what they are allowed to use. And make
sure they don't forget so you have to write it all down, or let your boss
write it down. And punish them when they do forget, show them the paper and
cut them with it.

C++ is full of pitfall features and people will use all the features that
they can use.
But C++ also has some good features, but whenever you mention that you want
a C++ feature in C the only response you'll get is: "Go use this other
language that we want no part of". That's not helping C at all.
Nov 14 '05 #94

On Sat, 6 Mar 2004, CBFalconer wrote:

"Dik T. Winter" wrote:
[...]
Letters on telephones have gone out of use in Europe in the early
to mid 60s.


There is the difference. Here in leftpondia we have rabid
conservatives, so the letters never left the dial or the keypads,
although they did leave the telephone directories. So every child
can learn a slightly truncated alphabet from the telephone. This
proves that rabid conservatism is not entirely bad. :-)


While you may be correct about the rabid conservatism, I naturally
assumed it was the rabid commercialism. I was quite surprised when
I was informed by this thread's European participants that most parts
of Europe don't have letters on telephone digits. No 1-800-FLOWERS?
No "dial down the center with 1-800 C-A-L-L A-T-T"? No Moviefone?
They don't know what they're missing! ;)

-Arthur

Nov 14 '05 #95

On Fri, 5 Mar 2004, Dan Pop wrote:

no****@nowhere. com writes:
Arthur J. O'Dwyer <aj*@nospam.and rew.cmu.edu> wrote: [Jacob Navia wrote:]
What algorithm you use for mapping 278487 to "Arthur" ??

Hmm. I would have expected my explanation to be a little opaque
to a Russian native, perhaps, but I honestly expected the French
telephone to follow the American mold [see diagram].<snip>
He asked what ALGORITHM you used, not what an American telephone
looks like.


The ALGORITHM is based on the layout of an American telephone keypad.
It seemed readily obvious (to me, at least) that he wanted to
know how std::string and std::map made your algorithm so much easier.


I agree with Dan, that Jacob undoubtedly just didn't understand the
number-letter correlation. But for the record, I used the C++ equivalent
of a blunt object:

Take the dictionary and add each word's number-representation to a map:
n2w_map["78487"] = "suits";
Run through that map with strstr() and produce a collection of
pairs mapping words to locations within the target telephone number:
"suits" is in "278487" at position 1.
Recurse over that collection, producing all possible sequences of
words that "sum" to the telephone number in question.
"2-suits" is one result; "Arthur" is another.
(I added rules to the recursion so that the last digit in a number
could represent itself, as could the first; hence "2-suits,"
even though "2" isn't a word.)

I then tried to reformulate the program to deal with l33tsp34k like
"B4"->"before" (thus mapping the dictionary word "before" to the
number-representations "24" AND "233673"), but that seemed like more
work than it was worth. This was, after all, just a quick hack for
fun, not a real project.
A couple of notes on Dan's program:

My first impression of the program shows that I may be spending too
much time in international programming groups ;-) I saw identifiers
like "lutini" and "phoneno" and thought, "What, is Dan Pop using Italian
now?" :) ("lutini"->"Look-Up Table INItialization" ; "phoneno"->"PHONE
NO. [number]".)
int match(char *phoneno, char *word)


Dan has the letter-to-number algorithm correct, but he'd have to
be generating a whole lot more "word"s than I did, or a lot more
"phoneno"s, if this function were going to help produce multi-word
mnemonics like "2-rug-up."

-Arthur
Nov 14 '05 #96
In article <Pi************ *************** *******@unix46. andrew.cmu.edu> "Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
....
I was quite surprised when
I was informed by this thread's European participants that most parts
of Europe don't have letters on telephone digits. No 1-800-FLOWERS?
No "dial down the center with 1-800 C-A-L-L A-T-T"? No Moviefone?
They don't know what they're missing! ;)


It is coming, but very slowly (this is due to mobiles, eh cellphones,
with letters). And, indeed, the last time I ever saw a phone with
letters (before the emergence of mobiles), was in the late sixties/early
seventies, in the UK. But we do not have the hassle where the owner of
1-800-FLOWERS appears to think that he/she also owns 1-888-FLOWERS,
1-877-FLOWERS, etc. But none of the three phones I have here at home
has letters on the dials/buttons. BTW, in Australia they would have
difficulty with xxx-FLOWERS, as there are about three standard layouts,
and the letters O, Q and Z switch around a bit among the three layouts.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #97
"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message
news:pa******** *************** *****@spam.for. me.invalid...
Many != all.

Your proposal would impose that cost on all applications. Leaving things
as they are would allow applications to store the length alongside the
string if they choose to. (or link against a library that provides such a
string type and replacement string manipulation functions)


The applications that really can't afford the size_t can of course still use
the zero terminated strings, I don't think those will be removed from the
standard in the near future :-)
Nov 14 '05 #98
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40******** ********@news.i ndividual.net.. .
And frankly, that sums up my reaction to this idea. It would make
programming with strings in C considerably more hassle-ful, to, as far
as I can tell, an entirely ephemeral speed advantage, which is more than
likely to be offset by the more complicated and time-consuming code
needed to handle these strings in any other circumstance than finding
their length - an operation which isn't performed as often as you'd
tihnk, in quality code.


There is no quality code, only average code :-)
Nov 14 '05 #99
"jacob navia" <ja***@jacob.re mcomp.fr> wrote in message
news:c2******** **@news-reader4.wanadoo .fr...
As everybody knows, C uses a zero delimited unbounded
pointer for its representation of strings.

This is extremely inefficient because at each query of the
length of the string, the computer starts an unbounded
memory scan searching for a zero that ends the string.

A more efficient representation is:

struct string {
size_t length;
char data[];
};


Have you considered wchar_t? Very important to include nowadays.
Nov 14 '05 #100

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8668
by: Good Man | last post by:
Hi everyone I'm using the "MySQL Administrator" program to keep tabs on the health of a web system i am developing. I think it's nice to have quick (gui) feedback on the query cache, memory variables, and other status variables. I've noticed that one of the status variables, "Aborted_connects" has been increasing steadily. This is defined by MySQL as "Number of tries to connect to the MySQL server that failed". I googled around a...
6
1843
by: Matik | last post by:
Hello all, I've following problem. Please forgive me not posting script, but I think it won't help anyway. I've a table, which is quite big (over 5 milions records). Now, this table contains one field (varchar), which contains some data in the chain. Now, there is a view on this table, to present the data to user. The
92
4141
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...) method. So now I start to wonder how that method, and others in the math.h library, are implemented. Dave
1
2287
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown. Would "Feed1" or "Feed2" be preferable in the following: #include <iostream>
335
11990
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
19
2939
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
3330
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm not concerned when dealing with user typing, but I am if a procedure is called by a query. Does the VBA compiler generate "in-line" code for some apparent function calls? For example, y = Abs(x) might be compiled as y = x & mask. The string...
4
8277
by: Rahul B | last post by:
Hi, I was getting the error: sqlcode: -911 sqlstate: 40001 , which is "The maximum number of lock requests has been reached for the database." So i increased the locklist size to 200 from the default value of 100. I wanted to know what other effects it will have on the database? Like, will the performance reduce, if the locklist size is 200 and 120 locks are on it as compared to when the locklist size is 130 and 120
4
6362
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
Visual Studio 2005, C# WinForms application: Here’s the question: How can I increase the standard 1 MB stack size of the UI thread in a C# WinForms application? Here’s why I ask: I’ve inherited some code that at the view (User Interface) layer kicks off a background worker thread. At the service layer (think CAB service layer), there’s quite a lot of the following:
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10971
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9666
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7201
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3317
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.