c

C++ Linker Error

Hello, I'm new to C++ and was working on an assignment for a class. We were given a .txt file, have to read information from it, and store it in a linked list, and then print it out to the user. After hours of trying to manipulate the examples we were given, and another couple hours of trying to write the code from scratch, I'm getting ...

freeing substring in c - loop

I'm trying to get a sub-string for each member of the struct 'structs' and then assign that sub-string to a new member of the temp_struct. The problem I'm having is how to free the sub-string on each iteration, for some reason the code runs, however valgrind throws an Invalid read of size 1, which I assume I'm reading off the block of m...

Undefined behaviour (?) in C with char arrays

when i try char bla[32] = "foobar"; int i; putchar(bla[i]); with strlen(bla) < i < 32, bla[i] is always \0. but isn't this in fact undefined behaviour, and should be avoided? ...

[C] fread/ftell apparently broken under Windows, works fine under Linux

So here's the problem, I'm reading a level file for my game, works fine under linux: @0 @12 200x200 version 3 @16 973 blocks @989 @993 18 zones But under windows I get the following result: @0 @212 200x200 version 3 @216 973 blocks @1200 @1204 18 zones Uh? The windows ftell stats with an offset of 200? Reading the file apparently y...

Error "No such device" in call setsockopt

Hello, everyone! I need help! I have a code in which send multicast datagrams. A critical piece of code: uint32_t port; int sockfd, err_ip; const uint32_t sizebuff = 65535 - (20 + 8); unsigned char *buff = (unsigned char *) malloc(sizebuff); struct sockaddr_in servaddr, cliaddr; struct in_...

Posix AIO Bad/Broken?

I'm working on a TFTP implementation that is transitioning away from a convoluted multi-threaded implementation to a single-thread/single-process implementation which uses a state machine to track the state of the sessions connected. TFTP is simple enough, and the number of concurrent sessions are small enough that the there really is no...

Is there an equivalent of D3DXMatrixRotationYawPitchRoll() for iphone developers?

The D3DXMatrixRotationYawPitchRoll() function builds a matrix with a specified yaw, pitch, and roll. Is there an equivalent function for iphone developers?? I found a beautiful little sample project for drawing a winding road or rollercoaster here. It's not a lot of code, and the comments seem mostly understandable, but the code is re...

Debug C program compiled using nmake

I have a program written in c that I compiled (The project structure is not my choice) using make files and the Visual C++ compiler (nmake.exe). I want to debug the application when it is called from a java application. I set debug break calls (__debugbreak()) in the code but when I debug using Visual Studio I only get the disassemble d...

pcm16 to pcm14 conversion using add + shift

I am studying a sound converting algorithm where an array of signed shorts is received. At a given point in the algorithm it converts the samples from 16 bits to 14 bits, and it does it like this: int16_t sample = (old_sample + 2) >> 2; For me its clear that the shifting is needed as we want to get rid of the least 2 significant bits,...

gcc -O3 question, calling same function from different file yields different performance

Hey, I'm running the following benchmark: int main(int argc, char **argv) { char *d = malloc(sizeof(char) * 13); TIME_THIS(func_a(999, d), 99999999); TIME_THIS(func_b(999, d), 99999999); return 0; } with normal compilation, the results are the same for both functions % gcc func_overhead.c func_overhead_plus.c -o func_overhead...

dynamic memory allocation teaching materials?

Dear all; Besides the (somewhat dated) papers that appear in Wikipedia's ref section, are there good (teaching) materials that you know of that comprehensively discuss dynamic memory allocation pitfalls, techniques, good practices, etc. in C? Thanks. ...

Declaring and initializing variable in for loop

Can I write simply for (int i = 0; ... instead of int i; for (i = 0; ... in C or C++? (And will variable i be accessible inside the loop only?) ...

C Fixed array pointing to parts of an array

I'm doing a project where I struck against this situation: typedef unsigned char Page[16384]; unsigned char Memory[16384*64]={...values...}; void foo(Page* page); Now as you can see Memory is composed of "Pages", now I would like to pass a Page to a function but a Page should be a POINTER to values of Memory (so indexes of a Page shou...

detecting a VPN connection in Windows?

Is there any way to detect if I am connected to a VPN using standard windows APIs in C? Basically I have a client that has to sync with a server but only if the VPN is connected. This could be a standard windows VPN client or a Citrix. Is RAS helpful here? thank you, code is appreciated. EDIT: to make it clearer. This is a client th...

Create a C program to find the sum of digits in a mixed string of letters and digits (eg co45p263) entered by the user?

/*C program to get a string with letters and numbers from the user then find the sum of all the numbers in the string */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main() { char ch; int j; char str1[9]; int sum = 0; printf("\n\n Please enter a string of letters and numbers: ")...

Execute C program till Ctrl+C hit in the terminal

Hi, I am writing a program that repeatedly does an operation till Ctrl+C is hit by the user in the Linux terminal. I am programming in C. Any ideas how i can implement this. I have tested my program by using "for" loops with a condition but now i want to make it run as long as Ctrl+C is hit by the user and interrupted. What I was thin...

How to figure out whether a Linux TTY is controlling a process group

So I have a tty (let's say /dev/tty5) and want to know whether it currently is a controlling tty of a process group or session, or whether it is currently unowned. POSIX has two API functions which suggest themselves here: tcgetpgrp() and tcgetsid(), both of which only work however if the caller has the tty as controlling tty -- which in...

reading primitives from file in C

Hi Folks, I am new to C, and want to read some data from a file. Actually, I find many reading functions, fgetc, fgets, etc.. But I don't know which one/combination is the best to read a file with the following format: 0 1500 100.50 1 200 9 2 150 10 I just need to save each row above into a struct with three data members. I...

characters XOR with caret manipulation

Working with exclusive-OR on bits is something which is clear to me. But here, XOR is working on individual characters. So does this mean the byte which makes up the character is being XORed? What does this look like? #include <iostream.h> int main() { char string[11]="A nice cat"; char key[11]="ABCDEFGHIJ"; for(int x=0; x<10; ...

Is it better to allocate memory in the power of two?

When we use malloc() to allocate memory, should we give the size which is in power of two? Or we just give the exact size that we need? Like //char *ptr= malloc( 200 ); char *ptr= malloc( 256 );//instead of 200 we use 256 If it is better to give size which is in the power of two, what is the reason for that? Why is it better? Thanks...