假设我有以下C++类:
class Foo
{
double bar(double sth);
};
double Foo::bar(double sth)
{
double a,b,c,d,e,f
a = b = c = d = e = f = 0;
/* do stuff with a..f and sth */
}
Run Code Online (Sandbox Code Playgroud)
函数bar()将在循环中被调用数百万次.显然,每次调用时,都必须分配变量a..f.通过使变量成为Foo类的a..f成员并在函数的入口点初始化它们,我能获得任何性能吗?另一方面,a..f的值将通过this->取消引用,所以我想知道它是否实际上不是可能的性能下降.通过指针访问值是否有任何开销?谢谢!
以下代码在我的系统上产生分段错误.我无法弄清楚为什么.任何帮助,将不胜感激.
#include<stdio.h>
int main() {
char * a = "abc";
*a = 'c';
printf("%c\n", *a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 给出以下代码
#include <stdlib.h>
#include <stdio.h>
typedef struct Foo {
int **bar;
}Foo;
int main(){
Foo *foo = malloc(sizeof(Foo));
foo->bar = malloc(sizeof(int**));
int *tmp = malloc(sizeof(int)*2);
tmp[0]= 0;
tmp[1]=1;
*(foo->bar) = tmp;
//printf("%d",*(foo->bar)[1]); <=== This line
int *tmp2 = *(foo->bar);
printf("%d ",tmp2[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注释掉的行会导致分段错误.
有人可以解释实际发生的事情吗?
为什么那行和下一个print语句不相同?
谢谢
我正在进行一个讲座,它显示了一些代码以递归方式打印出二进制搜索树
void printTree(node *t){
if(t!=NULL){
printTree(t->left);
cout<<t->key<<endl;
printTree(t->right);
}
}
Run Code Online (Sandbox Code Playgroud)
我理解它在做什么,但我不明白指针.该函数正在向"cout"行中的节点传递指针,它试图访问节点结构中的键值而不首先解除引用它.我的意思是,不应该是这样的
cout<<(*t)->key<<endl;
Run Code Online (Sandbox Code Playgroud)
代替?
所以我最近遇到了以下代码:
struct Student
{
int *number;
char *name;
double *marks;
};
int main(){
int n;
Student *s;
s = new Student;
cout << "Enter the number of subjects the student learns: ";
cin >> n;
s->number= new int;
s->name=new char[20];
s->marks=new double[n];
cout << "Enter the name of the student: ";
cin >> s->name;
cout << "Enter the number in class of " << s->name << ": ";
cin >> *(s->number);
for (int i = 0 ; i < n …Run Code Online (Sandbox Code Playgroud) 我有一个数组引用列表:
my @a = ([1,2],[3,4])
如何使用@a((1,2))中的第一个数组获得一个名为"@b"的数组?我尝试了各种解除引用的组合,但它们似乎都会抛出错误.
我正在学习JAVA.
我似乎无法找到摆脱这种'可能的null derefence'警告的方法.我已经在适当的范围内创建了fOut为null,否则我得到一个'可能没有被初始化'错误.
我无法找到一个简单的例子来帮助我解决这个问题.我知道这可能是一个简单的答案.
谢谢.
public static int waveToFile (String filename, byte[] byteWave)
{
FileOutputStream fOut = null;
File file = new File (filename);
try
{
fOut = new FileOutputStream(file);
}
catch (IOException e)
{
}
try{
fOut.write(byteWave); //THIS IS WARNING of POSSIBLE DE-REFERENCE
fOut.close(); //OF NULL POINTER
}
catch (IOException e)
{
}
return mErr;
}
Run Code Online (Sandbox Code Playgroud) 美好的一天,
我已经遇到这个问题,但我特别感兴趣的是"指向的对象成员..."作为上市型运营商在这里维基百科.
我从未在实际代码的上下文中看到过这个,所以这个概念对我来说有些深奥.
我的直觉说他们应该按如下方式使用:
struct A
{
int *p;
};
int main()
{
{
A *a = new A();
a->p = new int(0);
// if this did compile, how would it be different from *a->p=5; ??
a->*p = 5;
}
{
A a;
a.p = new int(0);
// if this did compile, how would it be different from *a.p=5; ??
a.*p = 5;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这不会编译因为p未声明.(见例)
任何人都可以提供一个在C++中使用operator - >*和/或.*的真实示例吗?
码:
int arr[5] = {1,2,3,4,5};
int (*p)[5] = &arr;
printf("p:%p\n",p);
printf("*p:%p\n",*p);
Run Code Online (Sandbox Code Playgroud)
结果:p = *p = arr = 0x7ffee517c830它们是数组的所有地址

p用于访问arr [i] 的正确方法是*(*p+i)
指针的类型p是int(*)[5],所以p指向一个类型为的数组int [5].但我们不能说p指向一个看不见的外壳arr,p毕竟是一个变量.它存储地址arr,也是arr[0]第一个元素的地址arr.
我以为*p会得到我1,这是第一个元素arr.
取消引用操作意味着将值p作为地址获取并从该地址获取值.对?因此P存储的地址arr,也就是0x7ffee517c830在这里,并1存储在该地址.不**p违法吗?第一个取消引用给我们1,第二个取消引用将1用作非法的地址.
我错过了什么?
我有一个遗留代码,其中为指针定义了接口.我试图调整一些函数来获取迭代器,例如转发迭代器.
是否允许使用InputIterator解除引用的元素的地址,例如istream_iterator?
结果是暂时的,并且必须在内存的某个地方进行通话,但我不确定.
以下示例使用double,但类型可能更复杂(大).
#include<iostream>
#include<iterator>
#include<sstream>
void f_legacy(double const* t){
std::cout << *t << std::endl;
};
void f(std::istream_iterator<double> it){
f_legacy(std::addressof(*it)); // OK????
// to avoid a copy: auto v = *it; f_legacy(std::addressof(v));
}
int main(){
double d = 5.;
std::istringstream iss("1 2 3");
std::istream_iterator<double> it(iss);
f_legacy(&d);
f(it);
}
Run Code Online (Sandbox Code Playgroud)