c

Use after free error?

int main(void) { int* p = (int*) malloc(sizeof(int)); int* q = (int*) malloc(sizeof(int)); *p = 10; *q = 20; p = q; printf(“%d %d”, *p, *q); free(p); free(q); } Why does the above code contain use-after-free error? There's no more expression after free(p) and free(q). Obviously we are not using them anymore! ...

What's wrong with this code?

int main(void) { char *p = "hello"; char *q = "world"; *p = *q; printf("%s", *p); } I am trying to overwrite hello with world... ...

C file transfer question

Hi there, I implemented a client-server program that allows to transfer files b/w them. The server is using select() to check changes of sockets. Every test is good except this one: - When server is sending a huge file to client (not yet finished), client hit "Ctrl-C" to kill the client program, then the server is killed too :( The sni...

return values from a struct

typedef struct Value{ int id; char type; char a; } Value; void fooV(Value v){ v.id = 10; v.type = 'L'; v.a = 'R'; } int main(void){ Value v; Pointer p; int id = 5; char type = 't'; char a = 'a'; printf("id: %d, type: %c, a: %c \n",id,type,a); v.a = a; v.id = id; v.type = typ...

problem in char array ?

char *funcNames[]= {"VString","VChar","VArray","VData"}; for(int i=0;i<4;i++) { char* temp = funcNames[i]; int len = strlen(funcNames[i]); for(int j = 0;j<len ;j++) { if(j!=0) { char arr = temp[j]; } } } here i want to separate "V" from all string in char array ...and create a...

Can I increase the size of a statically allocated array?

I know its possible to increase the size of a dynamically allocated array. But can I increase the size of a statically allocated array? If yes,how? EDIT: Though this question is intended for C language, consider other languages too.Is it possible in any other language? ...

debugging a thread process using gdb/dbx

This might be genuine question but i am asking here since i was out of any clue when i was asked this question in an interview. how could we debug a thread which was created by another thread? let's say there is a main process and it calles the function pthread_create to create a thread process which is not joinable and that means both ...

message queue. msgsend msgrcv. System V IPC system calls in C (Linux)

Hi I'm working with some shared memory where different processes read and write data. I'm using a message queue to store messages of when data has changed between read and write operations. /* struct that defines a message */ struct msgbuf{ long mtype; /* must be positive */ int childId; //ID of child sending message ...

Confusion in htons- little endian/ big endian

When I send a integer variable from one process to other through socket, and then printing the value at received end, the value is still the same without using ntohl/htonl, then where do I need to use these functions other than initializing socket structures. I understand litte/big endian. But why do we need to convert port and IP nos to...

How would I go about writing a Virtual Machine

I'm interested in programming a virtual machine, nothing as fancy as virtualbox or vmware, but something that can emulate a simple architecture, be it cisc or risc, say the Zilog, SPARC, MIPS, or 80686 architecture models. I suppose by doing this it would be relatively simple to make an emulator of the same kind, I'm just interested in ...

how to rotate the given string to left or right in C?

C function to rotate a string by a given number to the right or to the left. When a character is rotated past the end or the beginning of a string depending on the direction, it should wrap around ...

Why does the following equation always round up?

size = (size_in_bytes + sizeof(int) - 1) / sizeof(int); This is more of a math questions than real programming question... Is it because C always round down? ...

Is Solaris or Linux the better C GUI development environment?

What might be a better choice? I have my code (mostly C - as output from the GNU Eiffel compiler and some C++ for the GUI bindings) working with Sun Studio compiler and gcc. But i now have to setup a new developer computer and wonder if i should use Solaris with DTrace, locklint or Linux with Valgrind etc for development. It's just ab...

Printing error messages

Hello, I am just wondering what is the best way to make custom print error functions. For example I have some #defines like this in header file: #define SOCKET_ERR 0 #define BIND_ERR 1 #define LISTEN_ERR 2 etc Then maybe using this like this: if(/*something has gone wrong with socket*/) { print_error(SOCKET_ERR); } print_err...

Decoding Binary via fget / buffer string (Trying to get mp3 header)

I'm writing some quick code to try and extract data from an mp3 file header. The objective is to extract information from the header such as the bitrate and other vital information so that I can appropriately stream the file to a mp3decoder with the necessary arguments. Here is a wikipedia image showing the mp3header information: http:...

libxml2 error handling

I'm writing a small wrapper around libxml2 in C++, and I'm trying to work out how to handle errors. For now, let's say I just want to print them out. Here's what I've got at present: My error handling function: void foo(void *ctx, const char *msg, ...) { cout << msg << endl; return; } Initialised like this: xmlGenericErrorFunc h...

is there a way to look at the preprocessor expanded file in C

Hi i want to know how could we look at the c file after it has been expanded by the preprocessor before compilation with all the macro values put in the code inside the function where ever they are used. in there a way to do it? ...

MINIX 3 as a dns-server?

Is is it possible to have Minix 3 as a dns-server and if one would dare to code the service self how would that code look like (roughly)? ...

Posix threads problem

Hello, I am trying to understand pthreads by example. I have made the following code that is giving different answers everytime I run! Could anyone explain the bug please? TIA, Sviiya The code is here: #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 4 struct thread_data { int thread_id; ...

C - Excess elements in array

Hey, I am learning C and I am playing around with pointers and arrays. I am trying to create an array of pointers with the code below: const int NUM_P = 50; // Line 10 char *pS[NUM_P] = { NULL }; // Line 11 I am getting the following warnings and errors when I compile: → gcc array.c -o array array.c: In function ‘main’: array.c:11:...