c

Detect keypress in console application?

假如想象 提交于 2021-02-19 05:31:08
问题 I need to detect a keypress in a console application, without prompting the user. Basically, my app is normally a daemon that listens to a special input device, but i need to simulate it on a dev box using the keyboard in interactive mode. How can I do this? - Im on a Linux system. 回答1: If you can't block while waiting for input, then you can use e.g. select to check if the STDIN_FILENO file descriptor is ready for reading, and if it is then you can use normal input functions ( scanf , fgets

sscanf usage on matrix of unknown size?

此生再无相见时 提交于 2021-02-19 05:26:16
问题 So, I have a file that contains a matrix of NxM size. For example: P2 3 3 1 1 0 0 0 1 0 0 0 1 The 'P2' is just an useless indicator, the first '3' indicates how many columns are and the second '3' indicates how many lines, '1' indicates the maximum value among the matrix's numbers. This matrix was stored in a data structure like this: typedef struct { int c; // columns int l; // lines unsigned char max; // max value unsigned char** data // variable to store matrix's numbers } Matrix; To store

Why does my sin calculating code in C return the wrong value?

牧云@^-^@ 提交于 2021-02-19 05:20:28
问题 I want to calculate the sinus of user inputs using my own functions based on this equation: sin(x) = sum_(i=0)^n (-1)^i * (x^(2 i + 1)/((2 i + 1)!)) I have this code and to my understandings I do exactly the same as what's written in the equation: #include <stdio.h> #include <math.h> int faculty(int factor) { int result = 1; if (factor > 0) { for (int i = 1; i <= factor; i++) { result = result * i; } } else { result = 1; } return result; } double my_sin(double x, int n) { double my_sin = 0;

Pass a C++ member function to a C function

落爺英雄遲暮 提交于 2021-02-19 05:17:32
问题 We have a structure that accepts C function pointers: int one(int x) { } int two(int x) { } struct Cstruct { int (*fn1)(int); int (*fn2)(int); }; Now I have a C++ class that has below methods: class A { public: int one(int x) { } int two(int x) { } int three(int x) { struct Cstruct cstr = {&this->one, &this->two}; } }; While trying to initialize class A methods address to a instance of Cstruct compiler is giving error of an invalid conversion? How can I assign the Class member function

Heap corruption in C

和自甴很熟 提交于 2021-02-19 05:10:06
问题 int main () { int * b; b = (int*) malloc (1); *b=110000; free (b); return 0; } Why does heap corruption happen at free (b); ? IMO, heap corruption already happens at *b=110000; . 回答1: malloc() 's argument is the number of bytes to allocate. You need to use: b = (int*) malloc(sizeof(int)); You've allocated too small a block, and then written more bytes to it than you've allocated, which overwrites bookkeeping information next to the block, corrupting the heap. 回答2: It is at *b=110000; Because

Heap corruption in C

倖福魔咒の 提交于 2021-02-19 05:10:02
问题 int main () { int * b; b = (int*) malloc (1); *b=110000; free (b); return 0; } Why does heap corruption happen at free (b); ? IMO, heap corruption already happens at *b=110000; . 回答1: malloc() 's argument is the number of bytes to allocate. You need to use: b = (int*) malloc(sizeof(int)); You've allocated too small a block, and then written more bytes to it than you've allocated, which overwrites bookkeeping information next to the block, corrupting the heap. 回答2: It is at *b=110000; Because

Calling C methods with Char** arguments from Python with ctypes

故事扮演 提交于 2021-02-19 05:01:47
问题 I need a way to pass an array to char* from Python using ctypes library to a C library. Some ways I've tried lead me to segmentation faults, others to rubbish info. 回答1: As I've been struggling with this issue for some time, I've decided to write a small HowTo so other people can benefit. Having this C piece of code: void passPointerArray(int size, char **stringArray) { for (int counter=0; counter < size; counter++) { printf("String number %d is : %s\n", counter, stringArray[counter]); } } We

Can I prevent the user of my program to resize the console window? (in C) [duplicate]

不想你离开。 提交于 2021-02-19 04:45:52
问题 This question already has an answer here : How to change console window style at runtime? (1 answer) Closed 3 years ago . So I was searching (a lot) and haven't find anything on how to prevent user from resizing my program's console window. I had found information for language C++ and C# but not for C . I already managed to set the size of the console but if the user changes it afterwards it is not good for my program's looking. Is there anything I can do to perfectly resize the console and

Getting GCC/Clang to use CMOV

本小妞迷上赌 提交于 2021-02-19 04:38:05
问题 I have a simple tagged union of values. The values can either be int64_ts or doubles . I am performing addition on the these unions with the caveat that if both arguments represent int64_t values then the result should also have an int64_t value. Here is the code: #include<stdint.h> union Value { int64_t a; double b; }; enum Type { DOUBLE, LONG }; // Value + type. struct TaggedValue { Type type; Value value; }; void add(const TaggedValue& arg1, const TaggedValue& arg2, TaggedValue* out) {

When is integer to floating point conversion lossless?

╄→гoц情女王★ 提交于 2021-02-19 04:35:27
问题 Particularly I'm interested if int32_t is always losslessly converted to double . Does the following code always return true ? int is_lossless(int32_t i) { double d = i; int32_t i2 = d; return (i2 == i); } What is for int64_t ? 回答1: Question: Does the following code always return true? Always is a big statement and therefore the answer is no . The C++ Standard makes no mention whether or not the floating-point types which are known to C++ ( float , double and long double ) are of the IEEE-754