下面是代码
代码:
#include <stdio.h>
int * num(void);
int main(void)
{
int * num2;
num2 =num();
printf("%d\n" , *num2);
return 0;
}
int * num(void)
{
int num = 20;
return #
}
Run Code Online (Sandbox Code Playgroud)
问题:
我们知道,函数num是函数的本地函数num(),所以在这段代码中我尝试num将函数中变量的地址返回给调用它的函数,即main().
之后我只使用解除引用运算符来提取特定num变量的值并将其打印出来main().
有一点我很困惑.我记得我读过一本关于javascript的书,提到变量生命周期在函数内,这意味着在函数完成执行其指令并将控制权传递给调用它的函数之后,函数中每个变量的值将是干净的out(垃圾收集器).但为什么在这段代码中我的main()函数仍然可以指向特定内存地址的值?
根据这个答案,我做了一个简单的例子,以确保我正确理解:
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
int x;
} data;
void fill_data (data *** ptr_all, int l)
{
int i = 0;
*ptr_all = (data**) calloc(l, sizeof(data));
if ((*ptr_all) == NULL){
fprintf(stderr, "error: can't allocate memory");
abort();
}
for (i = 0; i < l; i++)
{
data * d = (data*) calloc(1, sizeof(data));
if (d == NULL){
fprintf(stderr, "error: can't allocate memory for %i-th data", i+1);
abort();
}
d->x = i;
(*ptr_all)[i] = d;
} …Run Code Online (Sandbox Code Playgroud) 假设您有一个带有指向数组的私有指针的类.如何使用getter访问(或有效复制数据),以便可以使用其他变量访问它.
class MyClass
{
private:
double *x;
public:
myClass();
virtual ~MyClass();
double* getX() const;
void setX(double* input);
};
MyClass::MyClass()
{
double foo[2];
double * xInput;
foo[0] = 1;
foo[1] = 2;
xInput = foo;
setX(xInput);
}
void MyClass::setX(double * input)
{
x = input;
}
double * MyClass::getX() const;
{
return x;
}
int main()
{
MyClass spam(); // Construct object
double * bar = spam.getX(); // This doesn't work
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,bar [0]和bar [1]等于jibberish : -9.2559631349317831e+061.
我有一个相机类,有两个指针作为参数:
Camera::Camera(
float fOVDeg, float nearCull, float farCull,
float xPos, float yPos, float zPos,
D3DMATRIX* matProjection, D3DMATRIX* matView)
{
this->SetCamera(fOVDeg, nearCull, farCull);
this->AdjustCamera(xPos, yPos, zPos);
matProjection = &(this->projection);//gets the addresses of two private members
matView = &(this->view);
}
Run Code Online (Sandbox Code Playgroud)
这是调用它的代码:
D3DMATRIX* matProjection,* matView;
//called once
void Initialise(HWND hWnd)
{
camera = new Camera(
45.0f, 1.0f, 100.0f,
0.0f, 9.0f, 24.0f,
matProjection, matView);
...rest of code...
Run Code Online (Sandbox Code Playgroud)
基本上问题是我想要代码中的两个指针调用相机构造函数来保留相机类的两个私有成员的内存地址,所以我可以用它们来做事!麻烦的是,这工作正常,但一旦构造函数完成,指针就变为空(0x00000000).我不知道为什么!相机的私人成员仍然有价值观,因为我之前只是用吸气剂抓住他们的价值而且它运作良好.
在C++中,推荐删除指针的方法是什么?例如,在下面的例子中,我是否需要所有三行来安全地删除指针(如果是这样,他们做了什么)?
// Create
MyClass* myObject;
myObject = new MyClass(myVarA, myVarB, myVarC);
// Use
// ...
// Delete
if(myObject) {
delete myObject;
myObject = NULL;
}
Run Code Online (Sandbox Code Playgroud) 我有一个unsigned char指针,其中包含一个结构.现在我想要执行以下操作
unsigned char *buffer ;
//code to fill the buffer with the relavent information.
int len = ntohs((record_t*)buffer->len);
Run Code Online (Sandbox Code Playgroud)
其中record_t结构包含一个名为len的字段.我无法这样做并且收到错误.
error: request for member ‘len’ in something not a structure or union.
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
这段代码抛出了seg错误.请帮我找出相同的原因
#include<stdio.h>
int main() {
char* str;
str = "abcd";
str[0] = 'r';
printf("%c\n" , str[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
刚开始学习C语言.我有一个指针数组int*parr,我需要用随机数填充它,然后用它做一些其他的事情.但我甚至不明白如何用随机数填充它.我试过这样的东西,但它挂起了程序:
for(i=0 ; i<R ; i++)
{
for(j=0 ; j<C; j++)
{
*(parr+i*C+j)=rand() % 10;
printf("%d",*(parr+i*C+j));
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud) 我有两个非常相似的程序,如下所示.
程序A:运行时没问题,
#include <string.h>
#include <stdio.h>
typedef struct p_struct{
unsigned char* pulist;
int length;
} list_type;
int get_struct(list_type* l)
{
memset(l->pulist, 0, 4);
l->length=4;
}
int main ()
{
list_type str;
get_struct(&str);
}
Run Code Online (Sandbox Code Playgroud)
程序B:有一个额外的函数调用,仍然编译,但崩溃与运行时错误"分段错误"与gcc.
#include <string.h>
#include <stdio.h>
typedef struct p_struct{
unsigned char* pulist;
int length;
} list_type;
int get_struct(list_type* l)
{
memset(l->pulist, 0, 4);
l->length = 4;
}
int get_struct_a()
{
list_type str;
get_struct(&str);
}
int main ()
{
get_struct_a();
}
Run Code Online (Sandbox Code Playgroud)
我真的很想弄清楚这里的问题.任何人都可以告诉我导致"分段错误"的原因是什么?另外,为什么程序B给出"分段错误"错误,而程序A没有?
问题
1.)是否可以声明一个指针变量来引用一个常量的内存地址??我之前尝试过这个pt = &20;(**pt是一个指针变量)但它不起作用,所以它意味着我们不能这样做?顺便说一下,如果有可能我该如何解决它?
pointers ×10
c ×7
c++ ×3
arrays ×1
constructor ×1
function ×1
gcc ×1
gcc-warning ×1
getter ×1
variables ×1