我正在尝试创建一堆共享指针并将它们放入各种容器中.
使用原始指针我会考虑做以下事情:
Container a, b, c;
MyClass *ptr;
while(!finishedCreating){
ptr = new MyClass(SOME_CHANGING_THING);
a.add(ptr);
b.add(ptr);
c.add(ptr);
}
Run Code Online (Sandbox Code Playgroud)
不过,当然,如果我现在想破坏a,b和c我需要删除它们所包含的指针.如果我做了以下事情:
~Container{
delete[] myListOfPointers;
}
Run Code Online (Sandbox Code Playgroud)
在破坏时我会得到一个错误,因为删除a会删除相同的内存b并且c应该删除它.
特别是,输入智能指针std::shared_ptr.我想做一些类似于之前我可以创建单个实体并使用它指向大量内存位置的东西,但我不知道该怎么做?
我想创建一个指向a的指针,std::shared_ptr以便我可以重新分配该指针,例如
std::shared_ptr<MyClass> *ptr = new std::shared_ptr<MyClass>(new MyClass(THING));
Run Code Online (Sandbox Code Playgroud) 我刚刚在一个小型嵌入式产品的启动加载器中看到了一个非常有趣的C代码.
代码由两部分组成:引导加载程序和主代码.主代码地址的开头存储在函数指针中.以下是我正在谈论的代码
typedef struct {
uint32_t myOtherField;
void (*mainCodeStartAddress)(void);
} MyStruct;
MyStruct myStruct = {0x89ABCDEF, 0x12345678};
void main(void) {
// Some code
...
myStruct.mainCodeStartAddress(); // Jump to the start of the application
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么这有效?为什么机器会转到应用程序代码?任何帮助表示赞赏.
我在将struct中的指针指定为null时遇到了一些困难.由于指针是按值传递的,我找不到一种简单的方法来执行此操作.也许这是漫长的一天,我不能直接思考.无论如何,这是我的代码:
void
init_wordrec (wordrec *rec)
{
if ((rec = (wordrec *) malloc(sizeof(wordrec))) == NULL) {
perror("Malloc failed");
exit(1);
}
rec->word = NULL;
rec->hits = 0;
rec->nxt_wd = NULL;
}
Run Code Online (Sandbox Code Playgroud)
这是wordrec结构:
typedef struct wordrec
{
char *word;
int hits;
struct wordrec *nxt_wd;
} wordrec;
Run Code Online (Sandbox Code Playgroud)
我希望实际的单词指针指向null,不幸的是我的尝试只会导致gcc大声抱怨.
编辑:这是我传入word结构的方法.
void
add_word (char *word, wordrec *head)
{
wordrec *iter = head;
wordrec *tmp;
if (iter->word == NULL) { //This should be NULL but is not
if ((iter->word = (char *) malloc((strlen(word) + 1) …Run Code Online (Sandbox Code Playgroud) 我必须初始化一个只包含NULL指针的字符串数组,没有别的.如果我理解正确分配NULL指针,如下所示:
char **array = NULL;
Run Code Online (Sandbox Code Playgroud)
否则,我试过了
char *array[] = {NULL};
Run Code Online (Sandbox Code Playgroud)
但是应该动态分配NULL指针的空间,在这里我很困惑,我的代码不起作用.
char **array = (char**)malloc(sizeof(char*));
Run Code Online (Sandbox Code Playgroud)
如果你能帮助我,我将非常感激.
我试图测试存储在特定内存地址中的值是否为NULL,我有这个并且在调试时它将该地址中的char值显示为'\ 0'.然而,它总是跳过这个IF声明.
这是正确的语法吗?我已确保将地址设置为null.
if (test->address + length == NULL)
{
..code..
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用指针正常运行.我应该在函数strcmp内部创建一个指向函数的指针check,但程序会立即打开和关闭.据我所知,函数指针在我的代码上是正确的 returnType (*pointer)(parameters)); 那么出了什么问题?提前致谢.
void check(char *a,char *b,int (*cmp)(const char*,const char*))
{
printf("Testing equality\n");
if(!(*cmp)(a,b)) printf("equals");
else printf("different");
}
int main(void)
{
char s1[80] = "daniel" ,s2[80] = "daniel";
int (*p)(const char*,const char*);
p = strcmp();
check(s1,s2,p);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在C++中遇到了Hash Map实现.HashMap的构造函数包含下面的代码.这条线new HashEntry*[TABLE_SIZE]说的是什么.我以前从未见过这样的结构.它如何返回指针指针?
class HashMap {
private:
HashEntry **table;
public:
HashMap() {
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
};
Run Code Online (Sandbox Code Playgroud) 这段代码有什么问题?
void input(int *nmbrOfUnits);
int main() {
int *nmbrOfUnits;
input(nmbrOfUnits);
}
void input(int *nmbrOfUnits) {
printf("numnber if units: ");
scanf(" %d", nmbrOfUnits);
}
Run Code Online (Sandbox Code Playgroud)
编辑:变量是在main中创建的,这应该意味着如果主调用输入变量没有从堆栈中加载.我为什么要在堆上分配它?我传递一个指针并在scanf中使用它.为什么我在这里遇到分段错误?
我已将整数变量的地址存储在指针中,然后将该先前的地址存储到另一个指针中.我无法理解它是如何工作的.
#include <iostream>
using namespace std;
#include <stdio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 30;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at ptr = %d\n", ptr );
printf("Value available at pptr = %d\n", pptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我无法找到解释为什么指针在下面的代码中发生变化的原因.
struct node{
int val;
node *left;
node *right;
}*root;
int main() {
node *tmp = (node *)malloc(sizeof(node *));
tmp->val = 5;
tmp->left = NULL;
tmp->right = NULL;
root = tmp;
node *t = (node *)malloc(sizeof(node *));
cout<<"earlier: "<<&root->right<<" "<<root->right<<endl;
t->val = 4;
cout<<"after: "<<&root->right<<" "<<root->right<<endl;
t->left = NULL;
t->right = NULL;
root->left = t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是 -
earlier: 0x7fb812404ac0 0x0
after: 0x7fb812404ac0 0x4
Run Code Online (Sandbox Code Playgroud)
价值root->right已发生变化,现已不再发生变化NULL.
注意 - 我的OSX g ++编译器中出现此错误
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 …Run Code Online (Sandbox Code Playgroud)