经验教训,在做基准测试时总是使用优化...
我决定将其std::unique_ptr视为我的计划的替代方案.关于为什么不重要的原因.
使用编译器优化后,它们似乎需要等量的时间.
我是如何测试的:
time_t current_time;
time(¤t_time);
srand((int)current_time);
//int* p0 = new int[NUM_TESTS];
//int* p1 = new int[NUM_TESTS];
std::unique_ptr<int[]> u_p0{ new int[NUM_TESTS] };
std::unique_ptr<int[]> u_p1{ new int[NUM_TESTS] };
for (unsigned i = 0; i < NUM_TESTS; ++i){
u_p0[i] = rand(); // Use p0 and p1 for the standard ptr test
u_p1[i] = rand();
}
int result;
auto start = std::chrono::steady_clock::now();
for (unsigned index = 0; index < NUM_TESTS; ++index){
result = u_p0[index] + u_p1[index]; // Use p0 and …Run Code Online (Sandbox Code Playgroud) 我有这个功能
void clean_strs(void *p){
if (!p){
printf("Clean str ptr that is NULL !!\n");
fflush(stdout);
return;
}
char *a = (char*)p;
free(a);
a = NULL;
}
Run Code Online (Sandbox Code Playgroud)
并传入这样的指针:
char *a = malloc(4*sizeof(char));
// check if memory was allocated
if(a) {
a = "asd\0";
clean_strs(a);
a = NULL;
if(a) {
getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
结果信号SIGABORT.有人可以解释为什么铸造和释放动态分配的指针是一个错误?
我写了一些代码:
#include <stdlib.h>
#include <stdio.h>
int a_function(int *a, int a_length, int *returned_address)
{
returned_address=a+a_length-1;
return 0;
}
int main(void)
{
int n=5;
int *a;
int *address=NULL;
a=(int*)malloc(n*sizeof(*a));
a_function(a,n,address);
printf("%p",address);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期的结果是指针'地址'指向a + 4地址处的内存块.但结果是指针"地址"仍指向NULL(屏幕上的打印结果类似于'00000000').但是,当我开始调试时,在'a_function'中,'returned_address'确实指向了+ 4的内存块.我错过了什么?
如果我创建动态char数组:
char * c = new char[5];
Run Code Online (Sandbox Code Playgroud)
我\0不用字符串来填充它,空字符是否在这个数组的末尾?
我写了一个程序
include<stdio.h>
struct record
{
int i;
char ch[20];
};
int main()
{
struct record a,*b;
b=&a;
printf("intial pointer is %p and final is %p",b++,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
a的大小应为20 + 4 = 24,但输出为
intial pointer is 0x7fffb0455e40 and final is 0x7fffb0455e58
Run Code Online (Sandbox Code Playgroud)
算术是18个字节.为什么输出是这样的?
我用C++编写了一个类.在其中我写道:
class Node{
private:
Node*m[200]=NULL;
};
Run Code Online (Sandbox Code Playgroud)
(这是课程的一部分)但我收到错误.我该怎么做才能定义一个指针数组,NULL?
我正在尝试一些基本的C代码,用于定义int带指针的矩阵.
typedef int **Matrix;
Matrix createMatrix(int lines, int columns) {
int i, j;
Matrix m = (Matrix) malloc(sizeof(int) * lines * columns);
for (i = 0; i < lines; ++i) {
for (j = 0; j < columns; ++j) {
m[i][j] = 0;
}
}
return m;
}
int main(int argc, char**argv) {
Matrix m = createMatrix(5, 10);
// ...
if (m[2][3] == 20) {
// ...
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,这些m[i][j]访问会导致分段错误.这有什么不对?太多的星号?
我确信指向int的指针实际上与矩阵相同.
我在运行大量数据时遇到性能问题.为简单起见,我记下以下代码:
double *a = new double();
for (int i = 0; i < 1000000; i++){
double x = 0;
double y = 0;
for (int j = 0; j < 1000; j++){
x = 1000;
y++;
}
*a = x; //*a = y;
}
Run Code Online (Sandbox Code Playgroud)
这需要将近0毫秒.但是,如果我将y分配给*a:
double *a = new double();
for (int i = 0; i < 1000000; i++){
double x = 0;
double y = 0;
for (int j = 0; j < 1000; j++){
x = 1000;
y++; …Run Code Online (Sandbox Code Playgroud) 此链接:http://research.swtch.com/godata
它说(切片的第三段):
因为切片是多字结构而不是指针,所以切片操作不需要分配内存,甚至切片头也不需要分配内存,切片头通常可以保留在堆栈上.这种表示使切片与在C中传递显式指针和长度对一样便宜.最初将切片表示为指向上述结构的指针,但这样做意味着每个切片操作都分配了一个新的内存对象.即使使用快速分配器,也会为垃圾收集器创建大量不必要的工作,并且我们发现,与上面的字符串一样,程序避免了切片操作,转而使用显式索引.删除间接和分配使得切片足够便宜,以避免在大多数情况下传递显式索引.
什么...?为什么不分配任何内存?如果是多字结构或指针?它不需要分配内存吗?然后它提到它最初是指向该切片结构的指针,它需要为新对象分配内存.为什么现在不需要这样做?非常困惑
我想很久以前我尝试了以下代码,一切顺利.但现在,我有一个分段错误,我无法找出哪个部分提供它.
#include <stdio.h>
#include <stdlib.h>
int main() {
char *test = NULL; // Empty string
char *a = "programming test";
int i = 0;
/* While the string "a" does not encounter a space,
I put its characters in the empty string "test" one by one. */
while(a[i] != ' ') {
test[i] = a[i];
i++;
}
printf("%s\n", test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个小代码对我来说似乎是对的,我无法确定是什么问题.