c

What do the * and & operators operate on if the argument is complex?

Simply, is &someObject->someAttribute.someMember; equivalent to &(someObject->someAttribute.someMember); or (&someObject)->someAttribute.someMember; or (&(someObject->someAttribute)).someMember; Or should you really put explicit parenthesis there just to be safe? ...

ansi c struct {with dynamic array} assigned to array which is realloc

Hi All, why the below code gives me error of "double free or corruption"... when i compile and run with gcc [(Debian 4.4.4-8) 4.4.5 20100728 (prerelease)]. Thanks in advance! #include <stdio.h> #include <stdlib.h> typedef struct { int *index1; } data; void doo(int *); int main(int argc, char *argv[]) { int *a = (int *) malloc(10*s...

Does apache guarantee that stdin will always have an EOF ?

AFAIK: A http POST request is sent to apache, then piped through stdin and into a CGI program. Does apache guarantee that the stdin stream will always have an EOF ? ...

ansi-c, better copy array elements to another array?

Hi All, is there anyway to do what I do in Lines 2 and 3, with smth similar to Line 1? If I just put Line 1, then both "a" and "one.index1" will be pointing to similar location, which I do not want. What I really want is done by Lines 2 and 3. So, is it the only way, or can anyone give better way? #include <stdio.h> #include <stdlib.h>...

Shrinking with realloc

I encountered this small piece of code in this question, & wanted to know, Can the realloc() function ever move a memory block to another location, when the memory space pointed to is shrinked? int * a = malloc( 10*sizeof(int) ); int * b = realloc( a, 5*sizeof(int) ); If possible, under what conditions, can I expect b to have an addr...

Importance sampling in monte carlo method (in C)

Hiya, Ive written a code which successfully approximation one, two and three dimensional integrals using a 'crude' Monte-Carlo sampling technique. I would now like to improve this by using 'importance sampling', as apparently this can reduce variance. I have read a few web pages about this but none seem particularly clear. How would I ...

error: ‘LIBNET_ERR_FATAL’ with libnet

I write a demo with libnet, but get an error when call the function: libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", errbuf); The error is "error: ‘LIBNET_ERR_FATAL’ undeclared (first use in this function)". my gcc command is: "gcc demo.c libnet-config --defines --cflags --libs", and run on ubuntu. I want to know ...

c programing questions

#include<stdio.h> void main() { int a=20,b=20; float x=3.5, y=3.5; printf("%d %d %f %f ",a,b,x,y); printf("%d %d %f %f ",a,b,x,a); printf("%d %d %f %f ",a,x,x,a); printf("%d %d %f %f ",x,b,x,y); } ...

In which segment is memory for library functions allocated?

The way automatic variables/local variables go on to stack,dynamically allocated objects/data type go on to heap; where is the memory for library function calls (say printf()) allocated. In which segment? ...

Combining C++ and C - how does #ifdef __cplusplus work?

I'm working on a project that has a lot of legacy C code. We've started writing in C++, with the intent to eventually convert the legacy code, as well. I'm a little confused about how the C and C++ interact. I understand that by wrapping the C code with extern "C" the C++ compiler will not mangle the C code's names, but I'm not entire...

How this simple program works, I/O related

This is my program: #include <stdio.h> int main(int argc, char* argv[]) { char buf[1024] = {0}; setvbuf(stdout, buf, _IOFBF, 1024); printf("error\n"); printf("%s\n", buf); } And this is the output: error error Exited: ExitFailure 7 Why are both lines 3 and line 4 blank lines ? Isn't the character '\n' flushing the output b...

Stopping individual threads

Using the pthread library in C, is it possible to send a SIGSTOP signal to an individual thread? I want to ensure that even though I create N threads in a loop, all should start executing only when all of them have been created. I ask because the man page for pthread_kill() mentions: Signal dispositions are process-wide: if ...

Create and call python function from string via C API

Is it possible to load a python function from a string and then call that function with arguments and get the return value? I'm using the python C API to run python code from inside my C++ application. I'm able to load a module from a file using PyImport_Import, get a function object from that using PyObject_GetAttrString, and call the ...

How do I track which libraries are being dynamically loaded by an application?

I have an application (for which I do not have the source code). I know that it is designed to dynamically load a shared library, depending on the command line parameters. I know which library it should be loading and I've set up LD_LIBRARY_PATH to the appropriate directory. The application works on one server without any problems, bu...

Large bit arrays in C

Our OS professor mentioned that for assigning a process id to a new process, the kernel incrementally searches for the first zero bit in a array of size equivalent to the maximum number of processes(~32,768 by default), where an allocated process id has 1 stored in it. As far as I know, there is no bit data type in C. Obviously, there's...

How scanf() handles floating point precision formats ??

Below is an excerpt from a program - float val=214.20; double val1=214.20; printf("\n\n\nfloat :: %f, %4.6f, %4.2f \n ",val,val,val); printf("double:: %f, %4.6f, %4.2f \n ",val1,val1,val1); And the output is - float :: 214.199997, 214.199997, 214.20<--- is the correct value as we wanted double:: 214.200000, 214.200000, 214.20...

Why is there a switch in the CPU core usage when I run this C code?

When I ran this code using gcc, $ cat eatup.c #include<stdio.h> #include<stdlib.h> int main() { int i = 0; while(1) { i++; } } $ the CPU graph went like this : I am not sure why there is a cross in the CPU core usage. I started the run at the rise to the left of the 40 mark, then initially core2 usage rose to...

Are there standard restrictions on C++ features?

I am about to start a new realtime project. Now there is (again) the debate about c vs c++. Yes I read about Linus and all the other threads on SO. First I was tending more towards to use C but then I read an answer that C++ includes C. Then I read on the internet about "Embedded C++". According to this article EC++ is dead. But I thi...

After byte shifitng a int to 4 bytes, how can I shift back to int (C)

So I byte shift a integer into 4 bytes of data. img[4] = (imagewidth >> 24) & 0xFF; img[5] = (imagewidth >> 16) & 0xFF; img[6] = (imagewidth >> 8) & 0xFF; img[7] = imagewidth & 0xFF; img[8] = (imageheight >> 24) & 0xFF; img[9] = (imageheight >> 16) & 0xFF; img[10] = (imageheight >> 8) & 0xFF; img[11] = imageheight & 0xFF; Now how wou...

typedef problem

hey people kindly tell me if the following declaration is correct? if it is then kindly explain typedef char HELLO[5]; HELLO name; now what datatype is name? [as in a character,integer etc] i came to know that name will be an array of strings but when i run the following programme i get error #include<stdio.h> typedef char HEL...