c

How to get function address by name?

 ̄綄美尐妖づ 提交于 2021-02-18 12:10:53
问题 I'd like to get function's address by name. For example, currently I am using dlsym : unsigned long get_func_addr(const char *func_name) { return (unsigned long)dlsym(NULL, func_name); } However, dlsym only works for extern function. It won't work for static function. I know there could multiple static functions with same name in different files. But I need to at least get one static function's address with the name. Sometime static function will be inlned. But it's OK if C file is compiled

how can i store an int array into string [closed]

Deadly 提交于 2021-02-18 12:10:44
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Improve this question I have an integer array: int a[5]={5,21,456,1,3} I need to store these number into char array so that the char array will have some thing like this: char *s="52145613"; Is there any library function in c for this? 回答1: sprintf do what you need. Little example

how can i store an int array into string [closed]

北城余情 提交于 2021-02-18 12:10:14
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Improve this question I have an integer array: int a[5]={5,21,456,1,3} I need to store these number into char array so that the char array will have some thing like this: char *s="52145613"; Is there any library function in c for this? 回答1: sprintf do what you need. Little example

Non-threadsafe file I/O in C/C++

余生颓废 提交于 2021-02-18 12:03:10
问题 While troubleshooting some performance problems in our apps, I found out that C's stdio.h functions (and, at least for our vendor, C++'s fstream classes) are threadsafe. As a result, every time I do something as simple as fgetc , the RTL has to acquire a lock, read a byte, and release the lock. This is not good for performance. What's the best way to get non-threadsafe file I/O in C and C++, so that I can manage locking myself and get better performance? MSVC provides _fputc_nolock, and GCC

Opening a file from command line arguments in C

依然范特西╮ 提交于 2021-02-18 11:45:13
问题 I want my C program to ask the user to type the name of the file they want to open and print the contents of that file to the screen. I am working from the C tutorial and have the following code so far. But when I execute it, it doesn't actually allow me to enter the file name. (I get the 'press any button to continue', I am using codeblocks) What am I doing wrong here? #include <stdio.h> int main ( int argc, char *argv[] ) { printf("Enter the file name: \n"); //scanf if ( argc != 2 ) /* argc

maximum size of bss and data

匆匆过客 提交于 2021-02-18 11:20:12
问题 I want to declare all the variables in my C program at compile time, like for example: char cache[CACHE_SIZE]; char udp_ring[MAX_UDP_PACKET_SIZE*MAX_REQUESTS]; int num_packets; char error_codes[NUM_ERRORS][MAX_ERROR_STRING]= { {"Unknown user\n"}, {"Wrong password\n"}, .... }; The question is, are there any limits on the size of the variables in a C program when they go in BSS or DATA segment? For example if I declare CACHE_SIZE of 8GB of RAM, will it work? Is there any difference for 32 bits

How to fill a section within c++ string?

℡╲_俬逩灬. 提交于 2021-02-18 11:13:28
问题 Having a string of whitespaces: string *str = new string(); str->resize(width,' '); I'd like to fill length chars at a position. In C it would look like memset(&str[pos],'#', length ); How can i achieve this with c++ string, I tried string& assign( const string& str, size_type index, size_type len ); but this seems to truncat the original string. Is there an easy C++ way to do this? Thanks. 回答1: In addition to string::replace() you can use std::fill : std::fill(str->begin()+pos, str->begin()

va_list misbehavior on Linux

て烟熏妆下的殇ゞ 提交于 2021-02-18 10:55:08
问题 I have some code that converts variadic parameters into a va_list , then passes the list on to a function that then calls vsnprintf . This works fine on Windows and OS X, but it is failing with odd results on Linux. In the following code sample: #include <string.h> #include <stdio.h> #include <stdarg.h> #include <stdlib.h> char *myPrintfInner(const char *message, va_list params) { va_list *original = &params; size_t length = vsnprintf(NULL, 0, message, *original); char *final = (char *)

What is the difference between %d and %*d in c language?

无人久伴 提交于 2021-02-18 10:50:29
问题 What is %*d ? I know that %d is used for integers , so I think %*d also must related to integer only? What is the purpose of it? What does it do? int a=10,b=20; printf("\n%d%d",a,b); printf("\n%*d%*d",a,b); Result is 10 20 1775 1775 回答1: The %*d in a printf allows you to use a variable to control the field width, along the lines of: int wid = 4; printf ("%*d\n", wid, 42); which will give you: ..42 (with each of those . characters being a space). The * consumes one argument wid and the d

Unix: What happens when a read file descriptor closes while calling select()

白昼怎懂夜的黑 提交于 2021-02-18 10:42:14
问题 Say that I call select() on a FD_SET containing a bunch of read file descriptors. What happens if during the select() call, one of the file descriptor closes? Assuming that some sort of error occurs, then is it my responsibility to find and remove the closed file descriptor from the set? 回答1: I don't believe this is specified anywhere; some systems may immediately return from select while others may continue blocking. Note that the only way this can happen is in a multi-threaded process