Bitshift Char Array(Bitshift Char Array)
如果我想将整数
5
移位3,那么int a = 5; int b = a << 3;
int a = 5; int b = a << 3;
,结果将是十进制40
,因为5
是101000
是101000
。但是,如果我有以下
char
数组:00000 00101
并且通过向左移位三,我希望结果为00001 01000
。 所以我想适应0
的填充。 你有什么建议?If I want to bit shift the integer
5
by 3, soint a = 5; int b = a << 3;
, the result would be40
in decimal as5
is101
and40
is101000
.What if however, I have the following
char
array:00000 00101
and by bit shifting three to the left, I want the result to be00001 01000
. So I want to accommodate for the0
's padding. What do you suggest?
原文:https://stackoverflow.com/questions/8085521
最满意答案
几个较旧的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 fromlocaltime
.The problem is that there is one and only one
localtime
buffer in the process space, and the next call tolocaltime
(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 ...
-
在Golang中释放C变量?(Freeing C variables in Golang?)[2023-10-20]
文档确实提到 : // 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 ... -
堆栈上的C ++变量在函数作用域结束时似乎没有被释放(C++ variable on stack does not appear to be freed at end of function scope)[2022-05-20]
编译器正在执行返回值优化(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 ... -
当函数返回时,函数中分配的内存是否被释放?(Does memory that is allocated in a function get freed when function returns?)[2021-06-05]
这取决于。 您需要定义谁拥有此类对象的所有权。 您可以在函数中分配数组,返回它,让调用者释放它,或者将一个指针放在一个类中,这将破坏析构函数中的指针。 如果应该在许多实体之间共享此类对象的所有权,则应使用类似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[] = ...
-
释放函数返回的内存(Free up memory returned by functions)[2023-09-14]
这些函数都没有分配任何内存。 所以实际上没有什么可以免费的。 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的空间可以重用 ...
-
如何处理在C / C ++中返回的动态内存的指针(How to deal with pointers to dynamic memory being returned in C/C++)[2023-05-12]
strtok不返回指向新分配的内存的指针,而是返回先前已分配的内存位置的指针。 我们假设这个: char String[1024]; strcpy(String, "This is a string"); char *Ptr = strtok(String, " "); Ptr不会指向新分配的内存位置,而是指向String的位置(如果我的计数现在没有让我失败),空格将被替换为'\ 0'。 ( 从引用开始:令牌的这一端由函数自动替换为空字符,并且函数返回令牌的开头。 这也意味着,如果你在strtok完成其工 ... -
是否需要释放来自函数的返回变量的内存(C)?(Does memory used of returned variables from functions need to be freed (C)?)[2021-07-30]
几个较旧的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... }; ...