Realworld example of when struct can be used [closed] - struct

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
Can anyone provide a realworld example of when a struct can be used?

A struct can be used when you have a complex return type for a method. i.e. you have to return several values, and they don't really warrant a full class's overhead.

A struct is notion of a record, a datatype that aggregates a fixed set of labelled objects, possibly of different types, into a single object. Structs are often used to group and relate objects in some manner.

If you mean a C struct, a great example is fixed scalar types in compilers. For example:
struct myScalar {
void *payload;
size_t psz;
unsigned int refs;
enum {
S_STR,
S_INT,
S_FLOAT,
S_OBJECT_INSTANCE
}type;
};
Or a union could be used. Not a robust example, but you get the idea. You can then do
switch(aVar.type){ ... }

Structs are great for helping you parse data that has been compressed to bits for sending over "The wire". You might have a bunch of bitfields to fill out a single byte, and a struct is a way to lay a template over this scrambled pile of variables and, without any real effort, change it into a collection of usable, easily referenced variables.

Related

Delphi 7 Function - String or ShortString? Integer or Word? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am a "performance" enthusiast programmer, and have a basic theoretical question: Does Delphi 7 has some "way" to identify the kind of string used and automatically define the correct type?
For example, when you know the result string length from a function will always be shorter than 255, would you use:
function SomeFunction: String; or function SomeFunction: ShortString;?
Same thought with:
function SomeFunction(Num: Integer): Integer; or function SomeFunciont(Num: Byte): Word;?
Never use shortstring with functions, even in Delphi 7.
When used as a function result, a shortstring full content (i.e. all characters) is always copied.
So it will be slower than using a plain string variable, which will only increment the internal reference count of the string, without copying the chars.
Since most of the Delphi RTL/VCL libraries use string, using shortstring in your code will add a lot of conversions for each call of those methods, whereas it will not be the case when using plain string. And when calling the Windows API, the delphi string type is already zero-ended, so will avoid any allocation and conversion, which is necessary when using shortstring.
Therefore, you have to know that shortstring is slower in all versions of Delphi, especially if you use FastMM4 as memory manager: when using the shortstring for any RTL/VCL/API calls, the Delphi compiler will in fact allocate some hidden temporary string, therefore allocate a buffer from the heap... far from efficient: it is preferred to use a string right away!
If you think that string is slow (due to the atomic reference counting or memory allocation which are not so multithread-friendly), do not use shortstring, but other structures - see my answer https://stackoverflow.com/a/6076407/458259 And never optimize without profiling with a tool like http://delphitools.info/samplingprofiler/
For functions using integer values, byte/word/cardinal/integer does not make a speed difference. My habit is to use integer whenever possible, which will fit the CPU register size. Only Int64 kind of value will be slower, under 32 bit, since it will use 2 registers or a temporary variable on stack.
Update: with newer versions of Delphi, you will have the "inline" keyword, which may help a lot about performance of small functions returning such types. And since Delphi 2009, string is Unicode and shortstring is deprecated, since it is fixed to the System Non-Unicode applications code page. Use shortstring only if you know exactly what you are doing, and are able to use Alt-F2 and see the generated asm code, guessing which hidden RTL functions are called.
Does Delphi 7 have some way to identify the kind of string used and automatically define the correct type?
Even if it improved performance, the compiler never makes such transformations. In order to do so it would need to perform what is known as data flow analysis. In other words it would need to analyse how the data flows through the program and reason about the possible content that could be held in variables.
But the compiler does not perform that sort of analysis and so cannot make optimisations of the
type you describe.

What are these ? ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
These "characters" seem interesting. What are they?I would like to know more technical information about them. Thanks!
ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉ ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉ ه҈҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉҉
It's just a mass of combining marks. That alone is like 170 Combining Marks. You can do all sorts of weird rendering effects just by using a ton of combining marks together. But in this case just U+0489 COMBINING CYRILLIC MILLIONS SIGN is used.
Combining marks are not stand alone characters by themselves, but modify the previous base letter. For example a, a combining diaeresis ¨ renders as ä instead of a¨. Put more combining marks in there, and they all have to somehow render with the base character a, causing interesting rendering effect.
Code in the jsfiddle (SO didn't allow me to post otherwise):
var l = 1000;
var str = ":"
while(l--) {
str += String.fromCharCode(0x300 + Math.floor((Math.random() * 0x20)))
}
document.write(str);
Those are unicode characters that belong to another character set/language (in this case, Cyrillic). This information can be seen (on Windows) by using the Character Map tool, which is found in the Programs -> Accessories folder of the Start Menu.

Is there any case where you must use ++var? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
The question is whether there is a situation where you can not use var++ and must use ++var (or the other way around).
That is, as far as I know, the only reason to use ++var over var++ (or the other way around) is to save space.
The question is, can we just have only the var++ abillity and not lose anything in the language's power?
You have to use ++var if you're using the expression within a bigger expression, and you definitely want the value of the expression to be the incremented one. For example, in C#:
int foo = 0;
foreach (var x in someCollection)
{
Console.WriteLine("{0}: {1}", x, ++foo);
}
Of course you could change this to use foo++ in a separate statement, but that isn't always feasible:
int foo = 0;
Expression<Func<int>> expression = () => ++foo;
That will return 1 the first time you call it, and you can't convert statement lambdas to expression trees.

what is logic programming? and what is different from other [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
today i have talk with other friend ,he said he has logic programming skill , so I am very curious about that.
The wikipedia entry explains it well: while on the surface it seems a redundant terms since all programming uses logic, in practice it's term for a well-defined paradigm, like, say, "functional programming" and "object-oriented programming". Specifically,
logic programming, in the narrower
sense in which it is more commonly
understood, is the use of logic as
both a declarative and procedural
representation language. It is based
upon the fact that a backwards
reasoning theorem-prover applied to
declarative sentences in the form of
implications:
If B1 and … and Bn then H
treats the implications as
goal-reduction procedures:
to show/solve H, show/solve B1 and … and Bn.
The language Prolog (in some variant or other) is probably still the most popular logic programming language.
I'd usually understand it to mean using prolog. Prolog allows you to define predicates, and truth values. The prolog interpreter can then derive further "truths", using standard logic rules. For example, each of the following lines establish a father_child and mother_children relationship between the first and the second parameter (the people mentioned are from the Simpsons).
member(X, [X|_]).
member(X, [_|T]) :- member(X,T).
mother_children(marge, [bart, lisa, maggie]).
mother_children(mona, [homer, jay]).
mother_child(X, Y) :- mother_children(X, C), member(Y, C).
father_child(homer, bart).
father_child(homer, lisa).
father_child(homer, maggie).
father_child(abe, homer).
father_child(abe, herb).
father_child(abe, abbie).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
If you fire this program into a prolog interpreter, and then ask it sibling(X,Y), it will return to you all the pairs of siblings that exist. What's interesting is that we never explicit say that say, Bart is a sibling to Lisa. We just define father and mother relationships, but by defining further rules, prolog uses normal rules to derive what names fullfill the sibling rule.
Prolog was more popular in the 80s, in various AI systems and the like. It's a bit out of fashion these days (not that you'd know in universities, where it's still hot shit).
I would take that statement to mean, "I can program if/then/else statements". Or in other words, I would take that statement to mean, "I can't program with any real technology". I would not be impressed.

What is a Pointer? [duplicate]

This question already has answers here:
Closed 14 years ago.
See: Understanding Pointers
In many C flavoured languages, and some older languages like Fortran, one can use Pointers.
As someone who has only really programmed in basic, javascript, and actionscript, can you explain to me what a Pointer is, and what it is most useful for?
Thanks!
This wikipedia article will give you detailed information on what a pointer is:
In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining or requesting the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). Pointers to data improve performance for repetitive operations such as traversing string and tree structures, and pointers to functions are used for binding methods in Object-oriented programming and run-time linking to dynamic link libraries (DLLs).
A pointer is a variable that contains the address of another variable. This allows you to reference another variable indirectly. For example, in C:
// x is an integer variable
int x = 5;
// xpointer is a variable that references (points to) integer variables
int *xpointer;
// We store the address (& operator) of x into xpointer.
xpointer = &x;
// We use the dereferencing operator (*) to say that we want to work with
// the variable that xpointer references
*xpointer = 7;
if (5 == x) {
// Not true
} else if (7 == x) {
// True since we used xpointer to modify x
}
Pointers are not as hard as they sound. As others have said already, they are variables that hold the address of some other variable. Suppose I wanted to give you directions to my house. I wouldn't give you a picture of my house, or a scale model of my house; I'd just give you the address. You could infer whatever you needed from that.
In the same way, a lot of languages make the distinction between passing by value and passing by reference. Essentially it means will i pass an entire object around every time I need to refer to it? Or, will I just give out it's address so that others can infer what they need?
Most modern languages hide this complexity by figuring out when pointers are useful and optimizing that for you. However, if you know what you're doing, manual pointer management can still be useful in some situations.
There have been several discussions in SO about this topic. You can find information about the topic with the links below. There are several other relevant SO discussions on the subject, but I think that these were the most relevant. Search for 'pointers [C++]' in the search window (or 'pointers [c]') and you will get more information as well.
In C++ I Cannot Grasp Pointers and Classes
What is the difference between modern ‘References’ and traditional ‘Pointers’?
As someone already mention, a pointer is a variable that contains the address of another variable.
It's mostly used when creating new objects (in run-time).

Resources