#ifndef DELETE
#define DELETE(var) delete var, var = NULL
#endif
using namespace std;
class Teste {
private:
Teste *_Z;
public:
Teste(){
AnyNum = 5;
_Z = NULL;
}
~Teste(){
if (_Z != NULL)
DELETE(_Z);
}
Teste *Z(){
_Z = new Teste;
return _Z;
}
void Z(Teste *value){
value->AnyNum = 100;
*_Z = *value;
}
int AnyNum;
};
int main(int argc, char *argv[]){
Teste *b = new Teste, *a;
a = b->Z();
cout << "a->AnyNum: " << a->AnyNum << "\n";
b->Z(new …Run Code Online (Sandbox Code Playgroud) 在我的代码中,我有一个Student对象的向量.
vector<Student> m_students;
Run Code Online (Sandbox Code Playgroud)
我想要:
请考虑以下代码:
// Check to see if the Student already exists.
Student* targetStudent = NULL;
for each (Student student in m_students)
{
if (student.Name() == strName)
{
targetStudent = &student;
break;
}
}
// If the Student didn't exist, add it.
if (targetStudent == NULL)
{
targetStudent = new Student(strName);
m_students.push_back(*targetStudent);
}
// Add the course info to the Student.
targetStudent->Add(strQuarter, strCourse, strCredits, strGrade);
Run Code Online (Sandbox Code Playgroud)
当我调用m_students.push_back(*targetStudent);它时,似乎向量"m_students"最终得到了"targetStudent"当时指向的Student对象的副本.
后续尝试添加到targetStudent不会更改向量中包含的对象.
我怎样才能从指向对象的指针开始,将该对象添加到向量中,然后访问向量中的对象?
我刚刚完成了我的第一个简单的iPhone应用程序; 我正在使用Instruments来查找内存泄漏.
关于如何重用指针,我有点迷茫.我已经阅读了Apple文档,但我仍然不明白正确的程序.
Apple文档说,"另一个典型的内存泄漏示例发生在开发人员分配内存,将其分配给指针,然后为指针分配不同的值而不释放第一块内存.在此示例中,覆盖地址指针擦除对原始内存块的引用,使其无法释放."
我是否真的必须每次释放并创建一个新指针?
在dateFormatter上创建内存泄漏的示例:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// year
[dateFormatter setDateFormat:@"yyyy"];
NSInteger year = [[dateFormatter stringFromDate:date] integerValue];
// month
[dateFormatter setDateFormat:@"MM"];
NSInteger month = [[dateFormatter stringFromDate:date] integerValue];
...
[dateFormatter release];
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我的同事告诉我,void foo(int** p)它在C#中像out参数一样使用.有人可以准确解释如何?
我明白了,但是有些东西不见了.我知道如果我们将指针p本身传递给foo(*p)函数体p = new int(),我们可能会有一个悬空修改器!但是如何foo(**p)阻止这样的事情发生呢?
我正在编译这段代码,我得到编译错误,说"解除指向不完整类型的指针".我得到了最后一个print语句的错误,在此之前我尝试指向(*temp).num到b的地址
void main()
{
struct {
int xx;
char *y;
int * num;
struct x *next;
}x;
struct x* temp;
int b = 10;
temp = ((struct x *)malloc(sizeof(x)));
(*temp).num = &b;
x.next = temp ;
printf(" %d\n",temp->num, x.next->num);
}
Run Code Online (Sandbox Code Playgroud) 提供指向字符串的指针数组作为输入.任务是反转存储在指针输入数组中的每个字符串.我创建了一个名为reverseString()的函数,它反转传递给它的字符串.据我所知,这个功能正常工作.
在指针的输入数组中存储/引用的字符串逐个发送到reverseString()函数.但是当使用临时变量交换传递的字符串的值时,代码在reverseString()函数中的某个点挂起.我无法弄清楚为什么代码在交换值时挂起.请帮我解决一下这个.
代码如下:
#include <stdio.h>
void reverseString(char*);
int main()
{ char *s[] = {"abcde", "12345", "65gb"};
int i=0;
for(i=0; i< (sizeof(s)/sizeof(s[0]) ); i++ )
{ reverseString(s[i]);
printf("\n%s\n", s[i]);
}
getch();
return 0;
}//end main
void reverseString(char *x)
{ int len = strlen(x)-1;
int i=0;
char temp;
while(i <= len-i)
{ temp = x[i];
x[i] = x[len-i];
x[len-i] = temp;
i++;
}
}//end reverseString
Run Code Online (Sandbox Code Playgroud) 我一直收到这个错误 -
错误C2819:类型'List'没有重载成员'operator - >'
我无法弄清楚为什么?救命?
Main.cpp -
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
#include "List.h"
#include "Node.h"
Run Code Online (Sandbox Code Playgroud)
错误发生在这里:
void PrintList ( List list ) {
Node * temp = list.getFirst();
Node * temp2 = list->getLast();
while ( temp->getItemName() != temp2->getName() ) {
cout << temp.getItemName() << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
List.h -
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
private:
Node * First;
Node * Last;
int num_in_list;
public:
List () { num_in_list = 0; …Run Code Online (Sandbox Code Playgroud) // strings is a 2D array (each string is 11 bytes long)
char strings[][11] = {"0123456789", "2222244444", "3333366666"};
printf("String 3 Character 2 is %c\n", strings[2][1]);
Run Code Online (Sandbox Code Playgroud)
如何使用指针算法而不是strings[2][1]?来编写此print语句?
我需要一个带有2D数组并生成随机位的函数,因此结果是一个随机二进制字符串数组.
我有以下代码,
#define pop_size 50
#define chrom_length 50
main() {
int population[pop_size][chrom_length];
init_pop(&population);
}
int init_pop(int *population[][]) {
for(i = 0; i < pop_size; i++) {
for(j = 0; j < chrom_length; j++) {
*population[i][j] = rand() % 2;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,我收到以下错误消息:
数组类型具有不完整的元素类型
有什么建议?
我的大脑从来没有真正掌握链表和指针的细节,但我正在尝试用一些C++任务帮助一个朋友.(在我再进一步之前,是的,有std :: list但我正在寻找一个学术答案,也许会让链接列表对他和我自己更容易理解).
我们需要做的是Employee根据用户输入生成对象(对象)的链接列表,然后将该信息显示给用户.每当我尝试将对象分配到链接列表容器时,它就会发生段错误.
我有以下Linked List对象:
class LinkedListContainer {
private:
Employee *emp;
LinkedListContainer *next;
public:
Employee getEmployee() { return *emp; }
void setEmployee(Employee *newEmp) {
*emp = *newEmp // This is what is causing the segfault
}
LinkedListContainer getNext() { return *next; }
void setNext(LinkedListContainer *newContainer) {
*next = *newContainer;
}
}
Run Code Online (Sandbox Code Playgroud)
我确信我做的事情非常糟糕.
pointers ×10
c++ ×5
c ×4
memory-leaks ×2
arrays ×1
class ×1
cocoa-touch ×1
dereference ×1
iphone ×1
memory ×1
set ×1
string ×1
structure ×1
vector ×1