首页 \ 问答 \ Bitshift Char Array(Bitshift Char Array)

Bitshift Char Array(Bitshift Char Array)

如果我想将整数5移位3,那么int a = 5; int b = a << 3; int a = 5; int b = a << 3; ,结果将是十进制40 ,因为5101000101000

但是,如果我有以下char数组: 00000 00101并且通过向左移位三,我希望结果为00001 01000 。 所以我想适应0的填充。 你有什么建议?


If I want to bit shift the integer 5 by 3, so int a = 5; int b = a << 3;, the result would be 40 in decimal as 5 is 101 and 40 is 101000.

What if however, I have the following char array: 00000 00101 and by bit shifting three to the left, I want the result to be 00001 01000. So I want to accommodate for the 0's padding. What do you suggest?


原文:https://stackoverflow.com/questions/8085521
更新时间:2022-05-24 22:05

最满意答案

几个较旧的C函数返回指向静态缓冲区的指针。 localtime就是其中之一。 您不需要(实际上不应该;如果您这样做,您可能会断言您的程序)从localtime释放返回的指针。

问题是进程空间中只有一个localtime缓冲区,下一次调用localtime (即使在另一个线程中)将覆盖先前返回的结果。 这就是为什么几乎所有行为方式的函数( strtok是另一个例子)现在都有新的_r版本,它们将结果放在用户传入的缓冲区中,因此是线程安全的。


Several older C functions return pointers to static buffers. localtime is one of those. You do not need to (and indeed should not; you will probably segfault your program if you do) free the returned pointer from localtime.

The problem is that there is one and only one localtime buffer in the process space, and the next call to localtime (even in another thread) will overwrite the results returned previously. This is why nearly all functions that behave that way (strtok is another example) now have new _r versions that put their results in a buffer passed in by the user and are therefore threadsafe.

相关问答

更多
  • 是的,有必要释放由function1返回的字符串。 为了让您做到这一点,您需要获取返回值的副本: - gchar* temp = function1("something"); gchar* string = function2(temp); g_free(temp); g_free(string); Yes it is necessary to free the string returned by function1. To allow you to do this, you need to take ...
  • 文档确实提到 : // Go string to C string // The C string is allocated in the C heap using malloc. // It is the caller's responsibility to arrange for it to be // freed, such as by calling C.free (be sure to include stdlib.h // if C.free is needed). func C.CString ...
  • 编译器正在执行返回值优化(RVO)。 你可以在这里阅读: https : //en.wikipedia.org/wiki/Copy_elision#Return_value_optimization The compiler is performing a return value optimization (RVO). You can read about it here: https://en.wikipedia.org/wiki/Copy_elision#Return_value_optimizati ...
  • 这取决于。 您需要定义谁拥有此类对象的所有权。 您可以在函数中分配数组,返回它,让调用者释放它,或者将一个指针放在一个类中,这将破坏析构函数中的指针。 如果应该在许多实体之间共享此类对象的所有权,则应使用类似shared_ptr 。 我还建议总是使用某种智能指针来处理你的原始指针。 It depends. You need to define who has got the ownership of such object. You can allocate the array in your functi ...
  • 当您从read_person返回时,它将复制内容。 一旦程序离开read_person就会删除局部变量a的存储,因为它是一个自动变量。 name是一个指针,它将指向一个字符串文字"Peter" ,它具有静态存储持续时间,这意味着它将持续程序的生命周期,并且指针不需要free编辑。 请注意,尝试修改字符串文字是未定义的行为,因此您无法修改name的内容。 另一方面,如果name指向malloc ed内存,则需要free编辑。 或者你在read_person声明了一个局部变量,例如: char arr[] = ...
  • 这些函数都没有分配任何内存。 所以实际上没有什么可以免费的。 asctime返回的char*是一个内部缓冲区。 所以你无论如何都无法释放它。 Neither of these functions allocate any memory. So there's actually nothing to free. The char* returned by asctime is an internal buffer. So you can't free it anyway.
  • 我习惯于把所有的变量放在函数的顶部 这在过去的C版本中是必需的,但现代编译器已经放弃了这一要求。 只要他们知道首次使用时的变量类型,编译器就可以获得所需的所有信息。 我想知道是否有人能解释我如何分配内存。 编译器决定如何在自动存储区域中分配内存。 实现不限于为每个变量声明一个单独位置的方法。 允许它们重用超出范围的变量位置,也可以重用某个点后不再使用的变量。 在你的第一个例子中,变量y被允许使用原来由变量x占据的空间,因为y的第一个使用点在x的最后一个使用点之后。 在第二个示例中,循环中用于x的空间可以重用 ...
  • strtok不返回指向新分配的内存的指针,而是返回先前已分配的内存位置的指针。 我们假设这个: char String[1024]; strcpy(String, "This is a string"); char *Ptr = strtok(String, " "); Ptr不会指向新分配的内存位置,而是指向String的位置(如果我的计数现在没有让我失败),空格将被替换为'\ 0'。 ( 从引用开始:令牌的这一端由函数自动替换为空字符,并且函数返回令牌的开头。 这也意味着,如果你在strtok完成其工 ...
  • 几个较旧的C函数返回指向静态缓冲区的指针。 localtime就是其中之一。 您不需要(实际上不应该;如果您这样做,您可能会断言您的程序)从localtime释放返回的指针。 问题是进程空间中只有一个localtime缓冲区,下一次调用localtime (即使在另一个线程中)将覆盖先前返回的结果。 这就是为什么几乎所有行为方式的函数( strtok是另一个例子)现在都有新的_r版本,它们将结果放在用户传入的缓冲区中,因此是线程安全的。 Several older C functions return po ...
  • 看起来你没有遵循三法则,如果你复制一个Student对象就会发生坏事。 具体来说,两者都将包含指向相同对象的指针,两者都会尝试删除 - 因此会尝试删除另一个已删除的对象。 赋予类有效复制语义的最简单方法是通过删除复制构造函数和复制赋值运算符来禁止复制: class Student { Student(Student const &) = delete; void operator=(Student const &) = delete; // rest of class... }; ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)