我一直在研究将浮点数(浮点数和双精度数)转换为IEEE 754的方法,目的是创建例程以有效地跨网络连接发送/接收信息。(类似于perl的包/解压缩功能。)我已完成创建经由IEEE 754表示的方法涉水无锁,technical-recipes.com,位操作, Bitwizardry,Haskell.org(C ++)等等,但我不明白为什么这些方法比仅使用联合来获得转换更快速/高效/更好?涉及整数/浮点数或长/双数的并集转换似乎是让C担心符号,指数和尾数的一种更好的方法,而不是手动进行移位和旋转。
例如,通过位旋转,您可以使用以下方法手动创建IEEE 754表示形式:
/* 23 bits of float fractional data */
#define I2F_FRAC_BITS 23
#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
/* Find the log base 2 of an integer (MSB) */
int
getmsb (uint32_t word)
{
int r;
#ifdef BUILD_64
union { uint32_t u[2]; double d; } t; // temp
t.u[__FLOAT_WORD_ORDER==LITTLE_ENDIAN] = 0x43300000;
t.u[__FLOAT_WORD_ORDER!=LITTLE_ENDIAN] = word;
t.d -= 4503599627370496.0;
r = (t.u[__FLOAT_WORD_ORDER==LITTLE_ENDIAN] >> 20) - …
Run Code Online (Sandbox Code Playgroud) 我有一个 C 程序,它应该验证用户的输入是否是 1 到 8 之间的整数。如果输入整数,它就可以工作,但是当输入字符时,验证循环将永远重复。你能告诉我我做错了什么吗?
#include <stdio.h>
int main(void)
{
int i;
int input;
domainEntry *myDomains = buildDomainDB();
printf("You have the choice between the"
" following top domains: 1-EDU, 2-COM"
", 3-ORG, 4-GOV, 5-MIL, 6-CN, 7-COM.CN, 8.CAN\n");
printf("Which one do you want to pick?");
scanf(" %d", &input);
if(input < 1 || input > 9)
{
do
{
printf("Invalid input. Please try again.");
printf("You have the choice between the"
" following top domains: 1-EDU, 2-COM"
", 3-ORG, 4-GOV, 5-MIL, …
Run Code Online (Sandbox Code Playgroud) 我刚开始学习 C++,我在 C++ 入门中看到了一些这样的函数:
double total_receipt(ostream &os)const{...}
Run Code Online (Sandbox Code Playgroud)
然后我尝试cout
使用以下代码查找地址:"cout << &cout << endl;"
这ostream &os
和直接使用没有区别cout
。
那么为什么不直接使用cout
代替ostream &os
呢?或者这只是一个“好”的习惯?
我正在尝试将多行插入到一个表中,其中一列的值来自另一个查询。但是我收到以下错误
用作表达式的子查询返回的多行
我该怎么做?
INSERT INTO
accounts_account_preferences (account_id, preference_id)
VALUES
((SELECT account_id
FROM accounts_account_preferences
WHERE preference_id = 1), 2);
Run Code Online (Sandbox Code Playgroud) Valgrind 告诉我,内存中存在泄漏,我曾尝试 free() 它,但我认为它没有正确完成。有任何想法吗?谢谢你。
0x4C27D4E 处的 free()/delete/delete[]/realloc() 无效:free (vg_replace_malloc.c:427)
by 0x400C00: main (main.c:149)
地址 0x51ba138 是大小为 8 的块分配后的 0 字节
在 0x4C28BED: malloc (vg_replace_malloc.c:263) by 0x400B0E: main (main.c:119)
堆摘要:退出时使用:1 个块中的 2 个字节总堆使用:5 个分配,5 个释放,14 个分配的字节
1 个块中的 2 个字节在丢失记录 1 of 1 中肯定丢失
在 0x4C28BED:malloc (vg_replace_malloc.c:263)
由 0x40084F: strdup (main.c:19)
通过 0x4009C4:置换(main.c:83)
由 0x400B9C: main (main.c:138)
char *strdup (const char *s)
{
char *d = malloc (strlen (s) + 1); // Space for length plus null //line 19
if (d …
Run Code Online (Sandbox Code Playgroud) 好,我不知道我已经打什么限制或是否这是一个问题与valgrind
,libc
或me
,但我需要知道这是可重复的,如果是这样,在这个问题所在.我把问题归结为我的两个AMD机箱上的MCVE产品.基本上,我动态分配struct dirent *
指针,然后struct dirent
为每个成功分配一个 readdir
.valgrind
没有投诉1017
,但随后数字1018
,我得到一个invalid read
错误(没有涉及重新分配),例如
==9881== Invalid read of size 8
==9881== at 0x4C2F316: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9881== by 0x40098E: main (readdir_mcve.c:35)
==9881== Address 0x51df070 is 0 bytes after a block of size 32,816 alloc'd
==9881== at 0x4C2ABD0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9881== by 0x4EE93E3: __alloc_dir (in /usr/lib/libc-2.23.so)
==9881== by 0x4EE94D2: opendir_tail (in /usr/lib/libc-2.23.so)
==9881== by 0x400802: main (readdir_mcve.c:9)
Run Code Online (Sandbox Code Playgroud)
(block …
我想将Card组件添加到此模块:https : //snack.expo.io/@xcarpentier/gifted-chat(演示)
例如,如果onLongPress()
在气泡消息上使用,我希望显示其他信息(在气泡消息的正下方,作为小卡片,例如Tinder卡)。
我怎么做?我是否需要克隆源代码,然后对其进行修改以适合我的需要?
#include< stdio.h>
struct node {
int data;
};
int main() {
struct node *ptr;
ptr->data=3;
printf("%d",ptr->data);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:3
我的问题是,即使我没有声明结构节点的实例.
例如struct node n;
我还没有使用malloc为ptr分配内存,它仍然为ptr-> data分配空间.为什么?它不应该只为堆栈上的地址ptr分配内存.
并且在堆栈或堆上分配数据字段的内存.---------
我正在尝试将 void * 指针更改为结构数组。\n重点是将全局可见指针初始化为 NULL,当 main 启动时,该指针将转换为结构数组\n 这是一个最小的示例。
\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nvoid * hashtable;\n\nstruct bucket {\n int a;\n int b;\n};\n\nint main (void)\n{\n hashtable = (struct bucket)malloc(6*sizeof(struct bucket));\n int i ;\n //for(i=0;i<6;i++)\n // hashtable[i] = malloc(sizeof(struct bucket));\n\n *(struct bucket)hashtable[0]->a = 12;\n\n return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n我得到的错误是:
\n\ntest.c:16:52: error: conversion to non-scalar type requested\n hashtable = (struct bucket)malloc(6*sizeof(struct bucket));\n ^\ntest.c:21:27: warning: dereferencing \xe2\x80\x98void *\xe2\x80\x99 pointer\n *(struct bucket)hashtable[0]->a = 12;\n ^\ntest.c:21:27: error: void value not ignored as it ought to be\n
Run Code Online (Sandbox Code Playgroud)\n 当我尝试将一个对象与任何具有13或26或7值的元素存储在一起,然后尝试读取此文件时,它只是给出了垃圾值。其他值(例如1、64、78(随机值))则不会发生这种情况。
我分别使用了ofstream和ifstream来保持代码简单。我正在使用旧的编译器(Borland C ++),但是我的朋友们没有遇到此错误。
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
class xyz
{
public:
int x;
};
void main()
{
xyz a;
a.x = 45; // replace by 45 or 78 it works. Replace by 13, 26 it shows garbage values.
ofstream of;
of.open("file.dat", ios::out||ios::binary);
of.write((char*)&a, sizeof(a));
of.close();
xyz b;
ifstream sf;
sf.open("file.dat", ios::in||ios::binary);
sf.read((char*)&b, sizeof(b));
sf.close();
cout<<b.x;
getch();
}
Run Code Online (Sandbox Code Playgroud)
当前输出将是45,但是如果将其替换为13或26,它将返回垃圾值。
c ×6
c++ ×2
malloc ×2
free ×1
fstream ×1
ieee-754 ×1
memory-leaks ×1
opendir ×1
pointers ×1
postgresql ×1
react-native ×1
scanf ×1
sql ×1
sql-insert ×1
struct ×1
turbo-c++ ×1
valgrind ×1
validation ×1