Questions tagged [c]

1099972 questions
22.3k

votes
22

answer
1.3m

Views

Why is it faster to process a sorted array than an unsorted array?

Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code almost six times faster. #include #include #include int main() { // Generate data const unsigned arraySize = 32768; int data[arraySize]; for (unsigned c = 0; c < arraySize;...
GManNickG
8k

votes
21

answer
648.3k

Views

What is the “-->” operator in C++?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. Here's the code: #include int main() { int x = 10; while (x --> 0) // x goes to 0 { printf('%d ', x...
5.6k

votes
30

answer
919.3k

Views

What is the difference between String and string in C#?

Example (note the case): string s = 'Hello world!'; String s = 'Hello world!'; What are the guidelines for the use of each? And what are the differences?
4.2k

votes
1

answer
2.1m

Views

The Definitive C++ Book Guide and List

This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the go from tutorials found on the Internet, few are able to quickly pick up C++ without studying a well-written C++...
3.3k

votes
26

answer
639.9k

Views

How do I enumerate an enum in C#?

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { DoSomething(suit); } } And gives the following compile-time error: 'Suit' is a 'type' but i...
Ian Boyd
2.9k

votes
30

answer
886.6k

Views

What are the differences between a pointer variable and a reference variable in C++?

I know references are syntactic sugar, so code is easier to read and write. But what are the differences? Summary from answers and links below: A pointer can be re-assigned any number of times while a reference cannot be re-assigned after binding. Pointers can point nowhere (NULL), whereas a referen...
prakash
2.8k

votes
30

answer
2.1m

Views

How do I iterate over the words of a string?

I'm trying to iterate over the words of a string. The string can be assumed to be composed of words separated by whitespace. Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer. The...
2.7k

votes
22

answer
1.1m

Views

Cast int to enum in C#

How can an int be cast to an enum in C#?
lomaxx
2.7k

votes
9

answer
355.2k

Views

Improve INSERT-per-second performance of SQLite?

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop application. We have large amounts of configuration data stored in XML files that are parsed and loaded...
2.5k

votes
24

answer
720.3k

Views

Case insensitive 'Contains(string)'

Is there a way to make the following return true? string title = 'ASTRINGTOTEST'; title.Contains('string'); There doesn't seem to be an overload that allows me to set the case sensitivity.. Currently I UPPERCASE them both, but that's just silly (by which I am referring to the i18n issues that come w...
Boris Callens
2.5k

votes
11

answer
711.8k

Views
2.3k

votes
30

answer
727.2k

Views

Why is “using namespace std” considered bad practice?

I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a fu...
akbiggs
2.2k

votes
26

answer
992.8k

Views
2.2k

votes
26

answer
196.9k

Views

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the result of malloc, i.e. int *sieve = malloc(sizeof(int) * length); rather than: int *sieve = (int *) malloc(sizeof(int) * length); Why would this be the case?
Patrick McDonald
2.2k

votes
9

answer
303.3k

Views

What are the correct version numbers for C#?

What are the correct version numbers for C#? What came out when? Why can't I find any answers about C# 3.5? This question is primarily to aid those who are searching for an answer using an incorrect version number, e.g. C# 3.5. The hope is that anyone failing to find an answer with the wrong version...
2.1k

votes
7

answer
464.5k

Views

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases?
e.James
2.1k

votes
25

answer
1.2m

Views

What is the best way to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
Jake Stewart
2.1k

votes
10

answer
221.7k

Views

Why are elementwise additions much faster in separate loops than in a combined loop?

Suppose a1, b1, c1, and d1 point to heap memory and my numerical code has the following core loop. const int n = 100000; for (int j = 0; j < n; j++) { a1[j] += b1[j]; c1[j] += d1[j]; } This loop is executed 10,000 times via another outer for loop. To speed it up, I changed the code to: for (int j =...
Johannes Gerer
2k

votes
30

answer
1.1m

Views

How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding? I'm going to encrypt the string. I can encrypt it without converting, but I'd still like to know why encoding comes to play here. Also, why should encoding be taken into consideration? Can't I simply...
Agnel Kurian
2k

votes
7

answer
861.3k

Views

What are the basic rules and idioms for operator overloading?

Note: The answers were given in a specific order, but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make most sense: The General Syntax of operator overloading in C++ The Three Basic Rules of Operator...
sbi
1.9k

votes
30

answer
708.9k

Views

Deep cloning objects

I want to do something like: MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); And then make changes to the new object that are not reflected in the original object. I don't often need this functionality, so when it's been necessary, I've resorted to creat...
NakedBrunch
1.9k

votes
30

answer
459.4k

Views

What is the difference between #include <filename> and #include “filename”?

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include statement, as follows? #include #include 'filename'
quest49
1.9k

votes
8

answer
235.8k

Views

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?
fredoverflow
1.9k

votes
30

answer
1.1m

Views

What is a NullReferenceException, and how do I fix it?

I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do to fix this error?
1.8k

votes
10

answer
168.4k

Views

Should 'using' directives be inside or outside the namespace?

I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace. Is there a technical reason for putting the using directives inside instead of outside the namespace?
benPearce
1.8k

votes
27

answer
448.5k

Views

Catch multiple exceptions at once?

It is discouraged to simply catch System.Exception. Instead, only the 'known' exceptions should be caught. Now, this sometimes leads to unneccessary repetitive code, for example: try { WebId = new Guid(queryString['web']); } catch (FormatException) { WebId = Guid.Empty; } catch (OverflowException) {...
Michael Stum
1.8k

votes
26

answer
455.4k

Views

What's the difference between the atomic and nonatomic attributes?

What do atomic and nonatomic mean in property declarations? @property(nonatomic, retain) UITextField *userName; @property(atomic, retain) UITextField *userName; @property(retain) UITextField *userName; What is the operational difference between these three?
Alex Wayne
1.7k

votes
30

answer
502.4k

Views

How do I calculate someone's age in C#?

Given a DateTime representing a person's birthday, how do I calculate their age in years?
1.7k

votes
5

answer
293.9k

Views

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it's been mentioned in many places, we didn't have any singular 'what is it' question and answer, so here it is. Here is a partial list of places where it was previously me...
GManNickG
1.7k

votes
4

answer
222.8k

Views

What does the C ??!??! operator do?

I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It compiled correctly and seems to run ok. It seems like it's checking if an error has occurred, and if it has, it handles it. But I'm not really sure what it's actually doing or how it's doing it. It does look like th...
Peter Olson
1.6k

votes
30

answer
948.2k

Views

How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?

How can I create an Excel Spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
1.6k

votes
30

answer
944.9k

Views

Create Excel (.XLS and .XLSX) file from C#

How can I create an Excel Spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
1.6k

votes
6

answer
174.9k

Views

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming? This article (by Gavin Clarke who quotes Herb Sutter) says that, The memory model means that C++ code now has a standardized library to call regardless of who made the compil...
Nawaz
1.6k

votes
30

answer
603.9k

Views

How can I make a UITextField move up when the keyboard is present - on starting to edit?

With the iOS SDK: I have a UIView with UITextFields that bring up a keyboard. I need it to be able to: Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up Automatically 'jump' (by scrolling up) or shortening I know that I need a UIScrollVi...
1.6k

votes
30

answer
596.2k

Views

How can I make a UITextField move up when the keyboard is present?

With the iOS SDK: I have a UIView with UITextFields that bring up a keyboard. I need it to be able to: Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up Automatically 'jump' (by scrolling up) or shortening I know that I need a UIScrollVi...
1.6k

votes
10

answer
213.5k

Views

Why is reading lines from stdin much slower in C++ than Python?

I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is rusty and I'm not yet an expert Pythonista, please tell me if I'm doing something wrong or if I'm mis...
1.6k

votes
11

answer
437.1k

Views

How can I profile C++ code running on Linux?

I have a C++ application, running on Linux, which I'm in the process of optimizing. How can I pinpoint which areas of my code are running slowly?
Gabriel Isenberg
1.5k

votes
16

answer
413.4k

Views

Why can templates only be implemented in the header file?

Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable solution. But they are the most convenient portable...
MainID
1.5k

votes
5

answer
92.1k

Views

Is there a reason for C#'s reuse of the variable in a foreach?

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example: foreach (var s in strings) { query = query.Where(i => i.Prop == s); // access to modified closure ... } Due to the modified closure, the above code will cause all of t...
StriplingWarrior
1.5k

votes
28

answer
1.8m

Views

How do I generate a random int number in C#?

How do I generate a random integer in C#?
Rella