如果我在循环中有以下行:
Type *unite = new Type(newSize);
Run Code Online (Sandbox Code Playgroud)
要么
double *array= new double[anySize];
Run Code Online (Sandbox Code Playgroud)
如果我内部没有删除操作符,那么对内存的关注是什么?它会不断地在不同的内存位置分配对象和数组,从而导致内存泄漏?
考虑以下2个声明.
* 出现在数据类型旁边而不是变量旁边
char* ptr1, * ptr2, * ptr3; //all 3 are pointers
Run Code Online (Sandbox Code Playgroud)
* 出现在变量旁边而不是数据类型旁边
char *ptr1,*ptr2,*ptr3; //again all 3 are pointers
Run Code Online (Sandbox Code Playgroud)
两个声明之间的解释是否有任何区别.我知道变量没有区别.
引入void指针的理由是什么?
我正在将GetCurrentDate()指针传递给tm结构.在该函数中,我打印未初始化的数据,然后初始化.预期成绩.
但是当我返回时,tm结构显示为未初始化.请参阅下面的控制台输 我究竟做错了什么?
未初始日期:??? ??? - 1073908332 01:9448278:-1073908376 -1217355836
初始日期:2010年5月5日星期三23:08:40
来电日期:??? ??? - 1073908332 01:9448278:-1073908376 -121735583
int main()
{
test();
}
int test()
{
struct tm* CurrentDate;
GetCurrentDate(CurrentDate);
printf("Caller date:%s\n",asctime (CurrentDate));
return 1;
}
int GetCurrentDate(struct tm* p_ReturnDate)
{
printf("uninitialized date:%s\n",asctime (p_ReturnDate));
time_t m_TimeEntity;
m_TimeEntity = time(NULL); //setting current time into a time_t struct
p_ReturnDate = localtime(&m_TimeEntity); //converting time_t to tm struct
printf("initialized date:%s\n",asctime (p_ReturnDate));
return 1;
}
Run Code Online (Sandbox Code Playgroud) :错误C2064:term不计算为带有1个参数的函数:错误C2227:' - > name'的左边必须指向class/struct/union/generic类型
我该如何解决这个错误不会发生
for(int index = 0; index < (numStudents); index++)
{
if (student(index + 1)->score >= 90 )
student(index + 1)->grade = 'A';
else if (student(index + 1)->score >= 80 )
student(index + 1)->grade = 'B';
else if (student(index + 1)->score >= 70 )
student(index + 1)->grade = 'C';
else if (student(index + 1)->score >= 60 )
student(index + 1)->grade = 'D';
else
student(index + 1)->grade = 'F';
}
Run Code Online (Sandbox Code Playgroud)
继承人的结构:
struct StudentType
{
string name;
int score; …Run Code Online (Sandbox Code Playgroud) 所以我是第一年的计算机科学专业的学生,因为在我的最终项目中,我需要编写一个带有字符串向量的程序,并将各种函数应用于这些.不幸的是,我真的很困惑如何使用指针将向量从函数传递给函数.下面是一些示例代码,以便了解我在说什么.当我尝试使用任何指针时,我也会收到错误消息.
谢谢.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
vector<string>::pointer function_1(vector<string>::pointer ptr);
void function_2(vector<string>::pointer ptr);
int main()
{
vector<string>::pointer ptr;
vector<string> svector;
ptr = &svector[0];
function_1(ptr);
function_2(ptr);
}
vector<string>::pointer function_1(vector<string>::pointer ptr)
{
string line;
for(int i = 0; i < 10; i++)
{
cout << "enter some input ! \n"; // i need to be able to pass a reference of the vector
getline(cin, line); // through various functions, and have the results
*ptr.pushback(line); // reflectedin …Run Code Online (Sandbox Code Playgroud) 这是析构函数是否足够或者我必须迭代删除新节点?
#include "stdafx.h"
#include<iostream>
using namespace std;
struct node{
int row;
int col;
int value;
node* next_in_row;
node* next_in_col;
};
class MultiLinkedListSparseArray {
private:
char *logfile;
node** rowPtr;
node** colPtr; // used in constructor
node* find_node(node* out);
node* ins_node(node* ins,int col);
node* in_node(node* ins,node* z);
node* get(node* in,int row,int col);
bool exist(node* so,int row,int col);
//add anything you need
public:
MultiLinkedListSparseArray(int rows, int cols);
~MultiLinkedListSparseArray();
void setCell(int row, int col, int value);
int getCell(int row, int col);
void display();
void …Run Code Online (Sandbox Code Playgroud) 我正在努力提高我对程序架构的了解,最近向我提出了一个与我最近发布的指针问题相关的问题.
问题在于,在一个简单的层次结构中,你有一个A类,其指针指向B类,最后一个指向C类.不要混淆与面向对象编程的继承属性,但基本上我所说的是C类是B级的孩子,B级是A级的孩子.
关键是我希望能够通过指针直接从A类访问C类(类比中的孙子).其他一些成员指出这是糟糕的设计,主要是因为如果从类B集合中删除类C的实例会在类A集合中留下指向"无"的指针.那么,这是如何正确建模的?
非常感谢!
Julen.
假设我有以下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->取消引用,所以我想知道它是否实际上不是可能的性能下降.通过指针访问值是否有任何开销?谢谢!
我正在尝试为多维数组(8行,3列)分配内存.
这是分配的代码(我确定错误很清楚)
char **ptr = (char **) malloc( sizeof(char) * 8);
for (i = 0; i < 3; i++)
ptr[i] = (char *) malloc( sizeof(char) * 3);
Run Code Online (Sandbox Code Playgroud)
当我引用这个崩溃时发生崩溃:
ptr[3][0];
Run Code Online (Sandbox Code Playgroud)
xxxx.exe中0x0135144d处的未处理异常:0xC0000005:访问冲突写入位置0xabababab.
是否有针对此类主题的推荐参考/读物?
谢谢.
pointers ×10
c++ ×7
c ×3
architecture ×1
dereference ×1
destructor ×1
if-statement ×1
malloc ×1
memory ×1
memory-leaks ×1
oop ×1
performance ×1
time ×1