基本上,数组应该在用户输入时初始化。如果是input = 3,这意味着这个数组可以分别在索引0,1,2中存储一个链接列表(因此总共有3个列表)
int input = 3;
list* array[n]//not allowed as n is not constant, also not in heap so can't pass it across functions
list* array[] = (list*) malloc(sizeof(list)*input)//compiler error
为interviews...so做准备,你可以说是在家工作!
我已经创建了一个char* str_1,并为它分配了64个字节。
然后,我创建了另一个char* str_2,并将其引用到初始字符串。
char* str_1 = new char[64];
char* str_2 = str_1; // does it reference the object created with the new statement.
我的问题是,str_2是否包含对分配内存的引用?在这种情况下,我能做这样的事吗?
char* str_1 = new char[64];
char* str_2 = str_1;
delete[] str_2;
调用delete[]
大家好!我是C++的初学者,所以我没有很好的体验。
我需要一些帮助,
我尝试创建对象(type ELEMENT)的动态数组,当我想从数组中删除对象时遇到问题(错误2157)
这是代码的一部分:
class ELEMENT
{
private :
int id_num;
int nbnr;
int BI;
public :
: void () ................
: ...............
:
};
ELEMENT *T;
/* before calling the next fonction, I allocate a dy
如何在不使用向量的情况下调整数组的大小?我要调整大小的数组是指向类的指针。
class A
{
private:
B * b;
int numOfItems;
int maxNumOfItems;
public:
A();
~A();
resizeMX();
};
A::A()
{
numOfItems = 0;
maxNumOfItems = 20;
b = new B[maxNumOfItems];
for(int i=0;i<maxNumOfItems ;i++)
内存中的变量覆盖存在问题,所以我决定尝试动态分配一个数组。
在下面的简化代码中,我正在尝试使用动态分配创建一个整数数组,然后让一个函数编辑该整数数组中的值。一旦函数执行完毕,我希望有一个处理良好的数组,以便在其他函数中使用。
据我所知,数组不能传递给函数,所以我只是将指向数组的指针传递给函数。
#include <iostream>
using namespace std;
void func(int *[]);
int main(){
//dynamically allocate an array
int *anArray[100];
anArr
我有一个数组和一个保持其大小的变量。这个数组是全局的(main之外)。问题是,我不知道大小,所以我必须这样声明它(psuedo代码):
int a[]; // don't know the size yet (outside main)
int size = [user input] // this is inside main
大小变量在main中。这就是出现问题的地方:
在用户输入后,我将如何给数组大小?我的数组需要是全局的,所以我很难弄清楚这一点。
我在一项作业上遇到了问题。任务是创建一个集合。IntegerSet是一个由100个元素组成的数组,表示从0到99的数字。例如,如果数字5出现在IntegerSet a中,则a[5] = 1中的空集就是一个0数组。
我创建了一个名为IntegerSet的类,下面是integerSet.cpp中的代码
#include "integerset.h"
#include <iostream>
using std::cout;
using std::endl;
IntegerSet::IntegerSet(){
int temp[100] = {0};
se
我问这个问题是为了缓解我对以下节目的困惑。我知道在特定的上下文中使用数组会使数组衰减到指向其第一个元素的单个指针。我有一个函数,它通过指针返回这个数组(这个函数是使用new[]创建的)。数组会衰变,导致指针只指向第一个元素吗?下面是一个示例:
int *foo() {
int *t = new int[10];
return t;
}
int main() {
int *p = foo();
}
这就是混乱的原因。我不知道p是指向第一个元素还是指向整个数组。因此,我有以下问题:
通过指针返回数组是否会导致数组的衰变(并必然导致内存泄漏)?
p是否指向数组的第
我有一个完整的程序,用来处理指针、动态分配和数组等等。
给我的最后一个函数是一个删除矩阵的函数。以下是给出的信息:
/*
Deletes a two dimensional dynamically allocated matrix
-- rows: The number of rows in the matrix
-- **matrix: the matrix to be deleted
*/
void delete_matrix(int rows, char **matrix)
{
delete[] matrix;
}
我的问题是,这样做对吗?此外,为什么要传入行的值?
对于即将到来的期中考试,我们的练习问题之一是编写一个函数,该函数将删除一个二维int数组并将指针设置为NULL。我以为我知道怎么做,但是班上的其他一些人说了一些让我很困惑的事情(他们说我们应该使用三重指针,这样我们就可以修改数组了,但是数组本身不就是一个指针吗?所以**数组不是等同于三重指针吗?)我的函数正确吗?
void cleanUp(int** array, int numRows, int numCols) {
for (int i = 0; i < numRows; ++i) {
free((*array)[i]);
}
free(*array);
*a
我正在尝试使用一个向量来创建一个堆栈,但是我似乎无法让它工作...下面是我的代码:
#ifndef _STACK_VEC_TPT_H_
#define _STACK_VEC_TPT_H_
#include <stdexcept>
using namespace std;
// abstract stack class implemented using vector
template<class T>
class abs_stack_vec {
public:
// pushes an element onto the top of the stack.
我很难通过引用将动态分配的数组传递给函数。“数组应该通过引用传递给函数”。我的程序应该接受n个整数,并找出最小值。应该将该最小值添加到用户输入的所有数组中。但是引用部分要了我的命。我尝试了(int &x[],int &n),但失败了。PLease帮助,非常感谢。
void add_min(int x[], int n)
{
int add[n];
int mini = x[0];
int i;
for(i = 0; i < n; i++)
{
if(x[i] < mini)
{
问题是如何在C中获得动态分配的二维数组的长度?我认为下面的代码应该得到行数,但它没有。
char** lines;
/* memory allocation and data manipulation */
int length; //the number of rows
length = sizeof(lines)/sizeof(char*);
对此有什么想法吗?
The program I should create will use a dynamic array which will store Revenue objects. The array should be dynamic so that the user of the program can enter the number of Revenue records. I am confused as to how to pull this off.
谢谢
例如,我有一个:
class Revenue
{
private:
(tons of function members in mid