c

Java JNI - associating resources allocated in C with java objects?

I want to allocate some memory in C and keep it associated with a java object instance, like this: void configure(JNIEnv *object, jobject obj, ....) { char *buf = new char[1024]; // associated <buf> with <obj> somehow } And then later free the memory when the java object gets garbage collected - I could do this by calling a JNI fu...

Macro resolution address

#define PLAINTEXT_TARGET "plaintext" if( strstr(PLAINTEXT_TARGET, optarg) == PLAINTEXT_TARGET ) /* ... */ Does the C language guarantee that PLAINTEXT_TARGET above compiles into a single instance? If the compiler may produce two instances of the macro string then the conditional above is misleading and can be false. ...

C preprocessor: removing quotes from an argument

I'm abusing the C preprocessor for my build system to produce a "readme" plain-text file and a web page from the same source file. The construction is something like this: The actual definitions are in data.h: #define WEBSITE "http://example.com" Note that the // in the URL must be quoted, or else it will be treated as the start of a...

Invoke PostgreSQL Stored Procedure using C

I am referring to http://www.postgresql.org/docs/8.1/static/libpq.html I try to find an example of C/C++ to call PostgreSQL stored procedure. However, I cannot find one. Can anyone point me the right direction? ...

In solaris how to fix 2038 problem for time functions

Is there any compiler option to make time_t 64-bit in Solaris 5.8 in forte compiler. I need to develop library in 32-bit and I cannot change it to 64-bit as it effects existing client applications. ...

Which one to use - memset() or value initialization to zero out a struct?

In Win32 API programming it's typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zeroed out. This can be achieved in either of the two ways: STRUCT theStruct; memset( &theStruct, 0, sizeof( STRUCT ) ); or STRUCT theStruct = {}; The second variant looks clean...

How to measure the power consumed by a C algorithm while running on a Pentium 4 processor?

How can I measure the power consumed by a C algorithm while running on a Pentium 4 processor (and any other processor will also do)? ...

Why assignment of double to int does not trigger gcc warnings?

int i = 3.1 / 2 does not cause any warnings,even with -Wall option.Sometimes,I would like to know where precision lose.Why gcc does not support this warning,while msvc support this one? thanks. EDIT: my gcc -v shows Configured with: ../../gcc-4.4.1/configure --prefix=/mingw --build=mingw32 --enable-languages=c,ada,c++,fortran,objc,o...

Continue running program until user type Ctrl + a

Hi, I'm practicing C programming for Linux for an exam. I don't know how to exit the program when user press Ctrl + a ( not Ctrl+c ) For example, looping something until user press Ctrl+a Could anyone tell me how to check Ctrl+a input? Notes: I'm using 'gcc' and run output with './a.out' Thanks in advance for everyone! ...

How can I tell if a library was compiled with -g?

I have some compiled libraries on x86 Linux and I want to quickly determine whether they were compiled with debugging symbols. ...

Screen Pixel Resolution in mm

Is it possible to get the screen pixel resolution in mm using Win32 APIs ? I have an application which is showing 0.3472222222222222 as the value for this on my 1280x1024 monitor with 96 dpi . But I am not able to find out how it got this value. Any clues will be helpful. I can use MFC also if required. EDIT Sorry for the confusion, the...

Segmentation fault

I have come across a problem while writing a fairly simple program. I have a statically allocated vector as a global variable and in a function I'm trying to change the values of the elements and that is when the program stops and says segmentation fault. The code is something like this: int a[10] = {0,0,0,0,0,0,0,0,0,0}; ... int bla(...

Software design practice in C

For OOP languages, there are many books describing how to design software, and design patterns are mainly for OOP languages. I am wondering whether there are any books/good articles teaching how to use C in a big project, like it is a good practice to use static functions when this function is only used in a single file. ...

Odd socket() error -- returns -1, but errno=ERROR_SUCCESS

I'm developing a dedicated game server on a linux machine, in C/C++ (mixed). I have the following snippet of code: int sockfd=socket(AI_INET, SOCK_DGRAM, 0); if(sockfd==-1) { int err=errno; fprintf(stderr,"%s",strerror(err)); exit(1); } My problem here, is that socket is returning -1 (implying a failure) and the error stri...

Loose-coupling patterns for embedded systems programming

Where can I find some good, proven guidelines or examples on writing extensible, modular, loosely-coupled code in C (if possible)? Background of our problem is that we are maintaining large plain C, legacy code project for a low-cost microcontroller with limited computing and memory resources. Due to the fact that the system must be ex...

What does this condition test?

Came across this conditional in some uncommented Objective-C code: if (w & (w - 1)) { i = 1; while (i < w) { i *= 2; } w = i; } Where w is a size_t greater than 1. Update: Added the code contained by the conditional for context. ...

PostgreSQL notice processing

Hi, I'm working on a C application that is suppose to talk to PostgreSQL. Right now I need to handle notices and warnings sent by the server but I'm at a loss on how to make it work. The (very unclear) documentation says we should use PQsetNoticeReceiver to set a method as the receiver of notifications, as the default receiver just for...

How to resolve fatal error LNK1000: Internal error during IncrBuildImage?

I am trying to recompile solution file for memcached project on Windows 7 64 bit with Visual Studio 2008 and got the following error: 1>LINK : fatal error LNK1000: Internal error during IncrBuildImage 1> Version 9.00.21022.08 1> ExceptionCode = C0000005 1> ExceptionFlags = 00000000 1> ExceptionAddress = ...

Display something in every 3 seconds

Hi everyone, I can't figure out how to display something (like hello world) in every 3 seconds. I'm writing in only C programming with gcc compiler in linux. We can stop it by Ctrl+c. I just want simplest and easiest way to manipulate thet code with my project. Thank you so much in advance! ...

Which way to reserve memory for a string?

I have created a macro to make reserve memory for my strings in C. It looks like this: #define newString(size) (char*)malloc(sizeof(char) + size) So is there any reason I shouldn't use this macro in my own personal projects? I know I shouldn't do this in production code because it would require everyone to have that header file and ev...