c

What's the equivalent of SHGetFolderPath on OSX?

I am porting a Windows application to OSX and need to get user's home, documents and music directories. It is done on Windows using a SHGetFolderPath WinAPI call. How can I do it in OSX, preferably without resorting to Objective C? If I can't do it without Objective C, how can I do it at all? ...

Getting final number of bytes of last member

Greetings, I'm able to find the size in bytes of a field after padding using the offsetof() macro. Something like struct a { char r ; char s[ 255 ] ; } ; // the size in bytes of r after compiler padding is going to be int lenR = offsetof( a, s ) - offsetof( a, r ) ; But how can I find the size in bytes of the last field of a...

Struct initialization problem?

Hi, I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment; I want to initialize the struct, and using the malloc() method return a dynamic memory like this Fragment *frag=malloc(10*sizeof(Fragment)); Then I would using the frag pointer like this: frag->seq="01001"; Then the problem occurs...

How do I encode video in Visual C?

I have a video decrypter library that can decode an obsolete video format, and returns video frames in BMP and audio in WAV through callback functions. Now I need to encode a new video in any standard format under windows. What might be the standard way to do this? I am using Win32/C. I guess Windows has its own encoding library and dr...

Malloc, string pointers, and Valgrind

My program is like this (main.c): #include <stdlib.h> #include <stdio.h> void main(){ char *first="hello "; char *second="world!"; char *seq=(char *)malloc((strlen(first)+1)*sizeof(char)); strcat(strcpy(seq,first),second); printf("%s\n",seq); free(seq); } and I debug with the tool valgrind, it said that($:valgrind --tool=m...

how to get the size of a dir programatically in linux ?

I want to get the exact size of a particular dir in linux through a C program. I tried using statfs(path,struct statfs &) but it doesn't give exact size. I also tried with stat() but it returns size as 4096 for any dir ! Please suggest me the way through which I can get the exact size of dir just like we get after "du -sh dirPath" comma...

Trying to return input from a function in C.

#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]); void shuffleDeck (int deck[]); int getBet (); main() { int deck[52]; int playerBet; char z; initDeck(deck); shuffleDeck(deck); showDeck(deck); playerBet = getBet(); //scanf ("%d\n", &playe...

Fixed Length Float in C/C++?

Hi all, I was wondering whether it is possible to limit the number of characters we enter in a float. I couldn't seem to find any method. I have to read in data from an external interface which sends float data of the form xx.xx. As of now I am using conversion to char and vice-versa, which is a messy work-around. Can someone suggest in...

C Structs and arrays

I can't get the code below to compile (see errors). Advice on correction would be appreciated. #include <stdio.h> typedef struct { char *fldName; unsigned fldLen; } Field; typedef struct { char *fldPrompt; unsigned startRow; unsigned startCol; } Prompt; typedef struct { Field *fields[]; Prompt *prompts[]; uns...

UNIX Portable Atomic Operations

Is there a (POSIX-)portable way in C for atomic variable operations similar to a portable threading with pthread? Atomic operations are operations like "increment and get" that are executed atomically that means that no context switch can interfere with the operation. In Linux kernel space, we have to atomic_t type, in Java we have the ...

C coding practices for performance or code size - beyond what a compiler does

I'm looking to see what can a programmer do in C, that can determine the performance and/or the size of the generated object file. For e.g, 1. Declaring simple get/set functions as inline may increase performance (at the cost of a larger footprint) 2. For loops that do not use the value of the loop variable itself, count down to zero in...

Commented lines in macros

Please help a macro beginner... I created a simple macro for loading images and split it up into several lines so that I can log every time code generated from the macro is executed (for debugging). It looks like this: #define LOAD_PNG(L_I_IMAGE_NAME) ({ \ PngImageClass* __tmp; \ printf("Loading png: %s", L_I_IMAGE_NAME);\ __tmp = [imag...

List File In C (.LST)

After compiling some code, the compiler generates a bunch of files. I have statistics, symbols, call tree, errors, list, debug and exe. I have figured out what each means, except for the list file. What is the function of the list file. Is it for the user or the computer/embedded system itself? ...

Is it possible to have a linked list of different data types?

Hi All, This is just another interview question------- Can we have a linked list of different data types i.e each elements in a linked list can have different structure or union elements.If its possible can u please explain me with an example. Thanks and regards Maddy ...

How do you create a UTC time in C for a specific day, month and year ?

How do I create a UTC time in C for the following date: 1st July 2038 using standard ANSI C function calls (given that the tm_year element of the tm structure cannot be greater than 137) ? ...

C XML library for Embedded Systems

I'm working on a project for an embedded system that's using XML for getting data into and out of the system. I don't want the XML handling to devolve into a bunch of bits that build XML strings using snprintf()/strcat() and friends or parse XML by counting "<" and ">" characters. I've found several XML libraries, a couple of which migh...

How can I generate unique values in the C preprocessor?

I'm writing a bunch of related preprocessor macros, one of which generates labels which the other one jumps to. I use them in this fashion: MAKE_FUNNY_JUMPING_LOOP( MAKE_LABEL(); MAKE_LABEL(); ) I need some way to generate unique labels, one for each inner MAKE_LABEL call, with the preprocessor. I've tried using __LINE__, but sinc...

What are a few time-consuming operations in C?

I'm looking to write a quick benchmark program that can be compiled and run on various machines. Rather than using commercially/open-sourceally available options, I'd rather have my own to play around with threading and algorithm optimization techniques. I have a couple that I use already, which include recursively calculating the nth n...

clearing a small integer array: memset vs. for loop

There are two ways to zero out an integer/float array: memset(array, 0, sizeof(int)*arraysize); or: for (int i=0; i <arraysize; ++i) array[i]=0; obviously, memset is faster for large arraysize. However, at what point is the overhead of memset actually larger than the overhead of the for loop? For example, for an array of size 5...

C-based console app crashes when run from cmd.exe, runs fine in VS2008 debugger?

Not sure what's going on here. I have an Windows console app written in C. When I run it from within VS2008, it runs fine. If I run it from the cmd.exe prompt, it crashes, usually in malloc(). I am guessing it is a race condition due to a mismatched CRT library. The app is simple. It calls into the WinHttp layer to send a GET reque...