c

zero size malloc

Very simple question, I made the following program : #include <stdlib.h> int main(int argc, char ** argv) { void * ptr; ptr = malloc(0); free(ptr); } And it does not segfault on my machine. Is it a portable behaviour of stdlib malloc and free, or am I looking for trouble ? Edit : What seems non portable is the value retur...

Writing integer values into binary file

Hi Lets say I have an array numbers that contains the following values: int numbers = [12, 511, 337, 254]; Now, I would like to scale those numbers into single byte values and store them in a char array char numbersscaled; for(i=0; i<4; i++) { numbersscaled[i] = numbers[i]/2; } Finally, I would like to write those valu...

while(1) .. break instead of goto

I found the following code in a C program: while (1) { do_something(); if (was_an_error()) break; do_something_else(); if (was_an_error()) break; [...] break; } [cleanup code] Here while(1) is used as local emulation of "finally". You can also write this using gotos: do_something() if (was_an_error()) g...

Qt Creator source files

Is it possible to set up QtCreator to treat .d files as C sources? ...

Is there a one-line function that generates a triangle wave?

In a similar way that modulo generates a sawtooth wave. It doesn't have to be continuous. here is what i mean: int m = 10; int x = 0; int i = 0; while (i < m*3) { printf("%d ", x); x++; x = x % m; i++; } generates a sequence 0..9, three times which looks like this: note that the slope on the right side of the peak is just a gr...

Getting the cluster size of a hard drive (through code)

I need to find the cluster size of the users hard drive, through C or C++. The hard drive uses NTFS (though I'd appreciate knowing how it's done on other file systems as well). I guess what I need is some combination of win32 API calls, but I don't know which. For instance, typing "fsutil fsinfo ntfsinfo c:" in the windows console give...

What does this line of code do ?

Hi Confused as to what this code does for (L=0; L < levels; L++, N_half>>=1){ func( y, N_half); } // end: levels for loop In particular this " N_half>>=1 " Thanks ...

fork and existing threads ?

On a linux system, does the child process view the existing threads the same way as the parent process ? int main() { //create thread 1 . . . int child_pid = fork(); if ( 0 == child_pid) { .. } else { .. } Since the whole address space is copied for the child process, what happens to the state of the threads. What if the thread 1 in...

Why does SCons VariantDir() not put output in the given directory?

I'm thinking about using SCons for a new project. It looks really good, though I'm finding VariantDir quite confusing. I have a simple project with a handful of C source files in one directory, and I want to build in "normal" and in "profile" mode -- with two different sets of options to gcc. I want the outputs to go in the normal/ and ...

Hard-coded string in the format of scanf

I'm trying to match lines with a format like "point %d %d". So I only need to two those two integers, then the "point" is hard-coded in the format string. As I understand reading Linux man pages of scanf, this should work correctly. The next code, the way I want to use, the first call to scanf works, but the next calls scanf return with...

Two C syntax tidbits: no init for and char *w[4] vs char w[4]

Just studying the C style of Jon Bentley's Programming Pearls and was wondering if someone could please explain to me the following two pieces of C syntax: A for with no init condition (see line 2 in wordncmp in the file above): for ( ; *p == *q; p++, q++) and the semantic difference between char *word[800000]...

I got a file full with hex addresses(char) what's the best way to convert them to unsigned long ?

Hey, I got a file full with hexadecimal addresses, I've managed to parse the file and fetch them yet I must convert them back to unsigned long, what's the best algorithm or fast way to do so considering the file is very long (a few megs) ? Thanks in advance. ...

Problem with realloc of **struct

Hi Mates, I have a problem with realloc function in C. I'm passing you a code bellow: typedef struct _Pool Pool; typedef struct _Item Item; struct _Pool { Item ** items; unsigned int itemsCount; unsigned int poolSize; unsigned int poolStep; }; struct _Item { char * file; unsigned int lenOfFilePath; unsign...

64 bit floating point porting issues

I'm porting my application from 32 bit to 64 bit. Currently, the code compiles under both architectures, but the results are different. For various reasons, I'm using floats instead of doubles. I assume that there is some implicit upconverting from float to double happening on one machine and not the other. Is there a way to control ...

Extending Python with C/C++

Can anyone please give me tips on the tools or sofware to use to extend Python with C/C++? Thanks. ...

Excel document parser/importer?

Can anyone recommend a decent Excel (Binary XLS) document importer written in C? I am looking to write a Ruby wrapper around one. I haven't been able to find any via Google. ...

Measuring time taken by an exec()-ed process on linux

I'm using the times() function to measure the value but I'm not sure if my approach is correct. Please have a look and advice struct tms tms_start, tms_end; if (!(pid=fork())) { //some necessary operations here times(&tms_start); execl(...); } else if (pid) { //in parent int status; wait(&status); times(&tms_...

How to mimic the "multiple instances of global variables within the application" behaviour of a static library but using a DLL?

We have an application written in C/C++ which is broken into a single EXE and multiple DLLs. Each of these DLLs makes use of the same static library (utilities.lib). Any global variable in the utility static library will actually have multiple instances at runtime within the application. There will be one copy of the global variable p...

Figuring out a C loop

This is one of the default gnome screensavers - "personal slideshow". It displays pictures from some location, pausing ~10s between pictures. Anyone know how it's looping & pausing? I'm trying to increase the delay, but I'm hesitant to add sleep() without first knowing how it's doing it. Thanks #include "config.h" #include <stdio.h> ...

OpenAL programming - links to get Started and playing WAV

I am trying to make some games and an important part of them is audio. So far, SDL_Mixer seems great but lacks the Mix_SetMusicPosition equivalent for channels. As a result, I tried OpenAL. But I have a problem with OpenAL too. OpenAL seems way more complex than SDL_Mixer. Besides, the ALUT function for loading Wave files is deprecated...