c

What techniques to avoid conditional branching do you know?

Sometimes a loop where the CPU spends most of the time has some branch prediction miss (misprediction) very often (near .5 probability.) I've seen a few techniques on very isolated threads but never a list. The ones I know already fix situations where the condition can be turned to a bool and that 0/1 is used in some way to change. Are t...

Can I mix C++ and C in a single project in Visual Studio?

I have a a Win32 DLL project in VS2008, it is written in a handful of C modules. Because I also want to be able to build outside VS2008, with no dependency on VS2008, I have produced a custom makefile, that does all the build and link steps. All this is set up just fine. Now I'd like to add a couple C++ modules to this DLL. I have mod...

Is DbgHelp.dll built-in to Windows? Can I rely on it being there?

I use Jochen Kalmbach's StackWalker class from CodeProject, to produce a stacktrace when an exception occurs in my DLL. It relies on DbgHelp.dll Is DbgHelp.dll built-in to Windows Vista, WS2008, Windows 7? I know about The Debugging Tools for Windows from Microsoft, and I'm aware that DbgHelp.dll ships in that package. But I also f...

Running external command with user input in C

I want to show the output of the Linux command dialog --menu from my C program so that the user can select an option from the menu. Also, the last line of output from the program is the option the user selected, so I need to capture that. I've tried using popen() and system() to accomplish this, and looked on the web, but couldn't find...

Confused on const correctness with static array of pointers to const objects

I'm still not sure I totally get how this particular case should work out. So if I want to declare an array of NSStrings that won't change, is this correct? static NSString * const strings[] = {@"String 1", @"String 2", ...}; Is the static necessary? (what does it do?) Am I missing an extra const somewhere? There's just too many pl...

Choosing C struct name for doxygen?

How can I tell Doxygen to use the first declaration in this code: typedef struct _decor_extents { int left; int right; int top; int bottom; } decor_extents_t; Cheers, Kris ...

How can I set the time zone before calling strftime?

I represent dates using seconds (and microseconds) since 1970 as well as a time zone and dst flag. I want to print a representation of the date using strftime, but it uses the global value for timezone (extern long int timezone) that is picked up from the environment. How can I get strftime to print the zone of my choice? ...

Reading long ASCII-files in C.

In C, if I read a line from the file by function fgetc, there might be a stack overflow. Suppose that some has written a program which outputs a a huge line of ASCII data in one line on a particular file. How can I read that data in my C program to screen? I know that I can read only, say 100 character at once but I don't know how can I ...

Recommended approach to typedefs for standard types in C?

What's the recommended approach to typedefs for standard types in C? For example at the start of my project I created typedefs to use the smallest types possible for their purpose. The main intention was to allow for easy modification of the types in the case where the numerical ranges used became too large for the type without having t...

Problem when using C object file (.o) in different computers

I'm trying to create an object file which can be useable in any computer. So what I did was create bunch of functions and placed them in a .c file. Used gcc -c myfile.c which produces myfile.o. (I'm running Windows XP and got gcc from DevC++) I then create another program which uses my object file. All seems fine except when I try copyi...

why do include lines not end with a semicolon ?

when including libraries c, the line does not end with a semicolon, while other statements do. what is the reason behind this ? ...

How to find largest triangle in convex hull aside from brute force search

Given a convex polygon, how do I find the 3 points that define a triangle with the greatest area. Related: Is it true that the circumcircle of that triangle would also define the minimum bounding circle of the polygon? ...

How to prevent scanf causing a buffer overflow in C?

I use this code: while ( scanf("%s", buf) == 1 ){ What would be the best way to prevent possible buffer overflow so that it can be passed strings of random lengths? I know I can limit the input string by calling for example: while ( scanf("%20s", buf) == 1 ){ But I'd prefer to be able to process whatever the user inputs. Or can't...

Problem with EOF in C

I'm writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I'm using EOF (Ctrl-Z or Ctrl-D) to end the string. This works fine with the first variable, but with the second variable, however, this seems to be problematic as apparently something is stuck in the input b...

malloc zeroing out memory?

Given this C code compiled with gcc 4.3.3 #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { int * i; i = (int *) malloc(sizeof(int)); printf("%d\n", *i); return 0; } I would expect the output to be whatever was in the memory that malloc() returns, but instead the output is 0. Is malloc ze...

Why is 49, 50, 51, 52 stored in the array when I declare testArray[] = {'1','2','3','4','5'}? (C programming)

Why is 49, 50, 51, 52 stored in the array when I declare testArray[] = {'1','2','3','4','5'}? How should i initialize a string array? Thanks ...

How to allow spaces in string when searching for position of substring in C?

I'm stuck on part of my homework, I had to find the rightmost occurrence of a substring inside of a string. I have the first part done (can find substring in single word strings), but now I am having trouble with the second part. I have to use a modified version of getline in order to allow multi-word strings (aka with spaces). Here's...

GLib Convert gchar* to gint

I have a string that I want to convert to an int. I am aware that there are other methods for doing this such as atoi; however, I'd really like to use a glib function if one exists. Does such a function exist? Thanks! ...

* is illegal for a struct?

I tried to compile the following code, but the compiler wouldn't doing because " * is illegal for a struct" is that true? struct String { int length; int capacity; unsigned check; char ptr[0]; } String; void main(){ char *s; String *new_string = malloc(sizeof(String) + 10 + 1); } ...

create strdup in C

I've been instructed to created a model strdup char* modelstrdup(char* source); -- This function is a mirror of the standard C library function called strdup. The parameter is a string that we wish to duplicate. The returned pointer will point onto the heap. When you write this function, create a String struct on the hea...