c

C : static array

I need to have a static void* array[1024]; in a library and i need to have it set to NULL for every entries. My question is about the best way to set the entire array to NULL, is a memset (array, NULL, sizeof (void*) * 1024) the best solution? ...

Is this really a buffer overflow?

The static analysis tool we use is flagging C code similar to the following as a critical buffer overflow. #define size 64 char buf [size + 1] = ""; memset (buf, 0, size + 1); The tool's error message is: Buffer Overflow (Array Index Out of Bounds): The array 'buf' size is 1. Array 'buf' may use the 0..64 index. Is this legitimat...

Catching segfaults in C

I have a program that segfaults from pointer arithmetic sometimes. I know this happens, but I can't easily check ahead of time to see whether it segfaults or not - either I can "pre-scan" input data to see if it will cause a segfault (which can be impossible to determine), or I can refit it to not use pointer arithmetic, which would requ...

Thread communication theory

What is the common theory behind thread communication? I have some primitive idea about how it should work but something doesn't settle well with me. Is there a way of doing it with interrupts? ...

Reconciling classes, inheritance, and C callbacks

In my C++ project, I've chosen to use a C library. In my zeal to have a well-abstracted and simple design, I've ended up doing a bit of a kludge. Part of my design requirement is that I can easily support multiple APIs and libraries for a given task (due, primarily, to my requirement for cross-platform support). So, I chose to create an ...

Simple USB host stack

I am trying to connect to a single USB device using the USB host port on an Atmel processor. I have an embedded system with limited memory and no OS. I want to implement a simple dedicated host to interface to a single USB slave device. Can anyone guide me to a simple USB host implementation? The processor is the Atmel AT91SAM9261S. ...

Timekeeping in Linux kernel 2.6

I've read chapter 7 in the 'Linux Device Drivers' (which can be found here) that time can be measured in 'jiffies'. The problem with the stock jiffies variable is that it wraps around quite frequently (especially if you have your CONFIG_HZ set to 1000). In my kernel module I'm saving a jiffies value that is set to some time in the futur...

Calling a script from a setuid root C program - script does not run as root.

I need to run a bash script as root (passwordless sudo or su not viable) and since you cannot setuid a script in Linux, I thought about calling it from an executable and making it setuid: $ cat wrapper.c int main(void) { system("/bin/bash ./should_run_as_root.sh"); } $ gcc -o wrapper wrapper.c $ sudo chown root wrapper $ sudo ch...

How do I get the HMODULE for the currently executing code?

I have a static library that may get linked into either a .exe or a .dll. At runtime I want ony of my library functions to get the HMODULE for whatever thing the static library code has been linked into. I currently use the following trick (inspired from this forum): const HMODULE GetCurrentModule() { MEMORY_BASIC_INFORMATION mbi ...

Read number of columns and their type from query result table (in C)

I use PostgreSQL database and C to connect to it. With a help from dyntest.pgc I can access to number of columns and their (SQL3) types from a result table of a query. Problem is that when result table is empty, I can't fetch a row to get this data. Does anyone have a solution for this? Query can be SELECT 1,2,3 - so, I think I can't ...

What open source project to start making experience?

I'd like to improve my programming skills, especially the readability of my source codes, and, why not, to give a little contribution to some open source or Linux-related projects. I'm studying C, and I'm interested in practical development problems and electronics problems, but I need to make a lot of experience. Is there any project yo...

How to write files with (readable) UTF8 characters in C?

I read a file that has utf8 characters like this: FILE *FileIN,*FileOUT; FileIN=fopen("filename","r"); char string[600]; WideChar C[600],S[100]; fgets(string,600,FileIN); wcscpy(C,UTF8Decode(string).c_bstr()); // widechar copy And it reads it perfectly (this is shown in the Editbox when running the program): Edit1->Text=C; Result ==...

ELF file versions

Hi guys, I'm working on a clone of the elfdump command. My program works fine when running 'elfdump -ecps file` on all executables. However, when I run my program on a library file, say libc.so, it prints out an incorrect version number for all symbols in the .symtab section (specifically they are all zero). I've looked over the documen...

ANSI C No-echo keyboard input.

Hello Folks, I've been Googling around with no luck. I'm looking for a way to do getc() or gets() or whatever that does not echo to the terminal. I saw kbhit() but this doesn't appear to be part of ANSI. Ideally, I'd like code that looks like char s[100]; no_echo_gets(s); /* Won't echo to screen while being typed */ printf("%s\n", s...

va_copy -- porting to visual C++?

A previous question showed a nice way of printing to a string. The answer involved va_copy: std::string format (const char *fmt, ...); { va_list ap; va_start (ap, fmt); std::string buf = vformat (fmt, ap); va_end (ap); return buf; } std::string vformat (const char *fmt, va_list ap) { // Allocate a buffer on the stack...

Best way to use a single C++ class in a C program

I have to import/translate the code from one C++ class so that I may use it in a C program. The C program is large and has lots of dependencies on C libraries both open and closed. The C++ Class .cpp file is 650 lines I have no experience mixing C and C++ so even though I have looked at one guide on how to do it, I am not convinced wh...

How do I parse a token from a string in C?

How do i parse tokens from an input string. For example: char *aString = "Hello world". I want the output to be: "Hello" "world" ...

How do I get a thread ID from an arbitrary pthread_t?

I have a pthread_t, and I'd like to change its CPU affinity. The problem is that I'm using glibc 2.3.2, which doesn't have pthread_setaffinity_np(). That's OK, though, because pthread_setaffinity_np() is itself a wrapper of sched_setaffinity(), which can be called by passing a thread ID instead of a process ID to set the affinity for an ...

Readings, tools and libraries for low-level, network related, softwares in C

I'm going to graduate soon in electronics and tlc engineering and I have some decent OO programming experience with PHP and Java. Now I would like to try starting a career as a C programmer. I'm interested in C since this is, I think, the most suited language, without considering Assembly, to develop device drivers, firmwares and other l...

Compiler-Programming: What are the most fundamental ingredients?

I am interested in writing a very minimalistic compiler. I want to write a small piece of software (in C/C++) that fulfills the following criteria: output in ELF format (*nix) input is a single textfile C-like grammar and syntax no linker no preprocessor very small (max. 1-2 KLOC) Language features: native data types: char, int an...