我试图使用一个大的2D矢量,我想用新的分配(因为它很大).
如果我说:
vector< vector<int> > bob;
bob = vector< vector<int> >(16, vector<int>(1<<12,0));
bob[5][5] = 777;
Run Code Online (Sandbox Code Playgroud)
有用.但如果我说:
std::vector< std::vector<int> > *mary;
mary = new vector< vector<int> >(16, vector<int>(1<<12, 0));
mary[5][5] = 777;
Run Code Online (Sandbox Code Playgroud)
它不起作用,我收到错误:
错误1错误C2679:二进制'=':找不到哪个操作符采用'int'类型的右操作数(或者没有可接受的转换)c:\ Users\jsparger\Documents\My Dropbox\ARI\VME_0.01\VME_0.01\V965.cpp 11 VME_0.01
显然我是C++的新手.有人可以解释我需要使用什么语法来执行此操作.玛丽是一个指针,所以我可以看出为什么这不起作用,但是*mary [5] [5] =因为"新"而不允许的任何东西,对吧?
谢谢您的帮助.这个载体就是我现在要用的东西,因为它似乎很容易让我的小c ++大脑理解,但是如果像这样的大型载体是一个坏主意等,请随时告诉我.
谢谢一堆.
编辑:我错误地认为"因为新的而不允许".我不知道我在哪里误读了它,因为它显然有效,并且不会因为它不太有意义.谢谢.
我有以下代码片段:
typedef struct person {
char *first ;
char *last ;
char *location ;
struct person *next_person ;
} person ;
person *make_person(char *first, char *last, char *location) {
person *personp = (person*) malloc(sizeof(struct person));
personp->first = (char*) malloc(sizeof(strlen(first) + 1));
personp->last = (char*) malloc(sizeof(strlen(last) + 1));
personp->location = (char*) malloc(sizeof(strlen(location) + 1));
strcpy(personp->first, first);
strcpy(personp->last, last);
strcpy(personp->location, location);
personp->next_person = NULL;
return personp ;
}
Run Code Online (Sandbox Code Playgroud)
当我将它与我的其余代码集成时,它开始执行,然后继续弹道.
*** glibc detected *** ./level1: free(): invalid next size (fast): 0x0804a188 ***
Run Code Online (Sandbox Code Playgroud)
知道出了什么问题吗?我觉得它与我的malloc有关.
我正在尝试读取一个二进制文件,该文件具有以标识符开头的块(如3DS文件).我遍历文件并使用开关,程序确定块具有什么标识符,然后将数据读入文件结构.有时我需要使用malloc为动态大小的数据分配内存.在读取时,交换机经常会分配内存的相同情况,但是在文件中的特定点,它会在同一个malloc上崩溃.我想读的文件大约是1MB.但是当我用另一个大约10kB的文件和相同的结构来尝试该程序时,它会成功地读取它.
什么可能导致这个问题?
我在调试时得到的错误代码是:
Heap corruption detected at 0441F080
HEAP[prog.exe]: HEAP: Free Heap block 441f078 modified at 441f088 after it was freed
Run Code Online (Sandbox Code Playgroud)
此外,当我在调试模式下执行它时,由于某种原因,我可以从文件中读取更多数据.程序在崩溃之前会持续更长时间.
这是代码崩溃的代码:
switch (id) {
case 0x62:
case 0x63:
// ...
{
char n_vertices = id - 0x60 + 1;// just how I calculate the n_vertices from the block ID
fread(&mem.blocks[i].data.attr_6n.height, 2, 1, f);
mem.blocks[i].data.attr_6n.vertices = malloc(2 * n_vertices);// crash
for (short k = 0; k < n_vertices; k++) {
fread(&mem.blocks[i].data.attr_6n.vertices[k], 2, 1, f);// read shorts
}
}
break; …Run Code Online (Sandbox Code Playgroud) 我最近发布了一个关于此的问题,但这是一个不同的问题.我使用动态内存分配创建了一个2D数组,在使用矩阵之后,我们需要通过删除来释放内存,我不明白为什么我们不能用delete [] matrix它来删除它而不是下面代码中的方法
int **matrix;
// dynamically allocate an array
matrix = new int *[row];
for (int count = 0; count < row; count++)
matrix[count] = new int[col];
// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
delete [] matrix[i] ;
delete [] matrix ;
}
Run Code Online (Sandbox Code Playgroud)
因为问题是因为在main()我创建了一个2D数组并使用其他int **函数分配值,我不知道如何删除分配的内存,循环将导致运行时错误
int main()
{
int **matrixA = 0, **matrixB = 0, **matrixResult = 0; // dynamically allocate an …Run Code Online (Sandbox Code Playgroud) 你好,我有以下结构:
typedef struct GRAPHE{
int n; /* Nombre de sommets */
int **M; /* Matrice d'Adjacence */
} GRAPHE;
Run Code Online (Sandbox Code Playgroud)
我需要分配内存,我尝试了这个功能,但似乎没有用
void reservation_en_memoire(int n, GRAPHE *g) {
int i, j;
g = (GRAPHE *) malloc(n * sizeof (struct GRAPHE));
g->M = (int **) malloc(n * sizeof (int*));
for (i = 0; i < n; i++)
g->M[i][j] = (int) malloc(n * sizeof (int));
g->n = n;
}
Run Code Online (Sandbox Code Playgroud)
而在主要我只是这样做:
int main(int argc, char** argv) {
GRAPHE *g;
reservation_en_memoire(3, g);
printf("%d", g->n);
// printf("%d", …Run Code Online (Sandbox Code Playgroud) 我试图动态分配一组记录.当我使用./a.out运行我的程序时它看起来工作正常但是当我尝试运行程序从.txt文件(./myProg <)输入数据时,它会重复记录整个数组中的第一行文本并且崩溃并且最终不会打印出所选书籍.我的猜测是我没有正确地创建数组,但我似乎无法弄清楚我的问题是什么.
struct Book {
char *title; //entered with no spaces
int date; // in the form ddmmyy
};
Book *createRecord(int);
void input(Book *, int);
void display(Book *, int, int);
void destroyRecord(Book *);
int main() {
int arrN = 0;
int n = 0;
cout << "Enter size of array: ";
cin >> arrN;
Book *bookArr;
bookArr = new Book[arrN];
bookArr = createRecord(arrN);
input(bookArr, arrN);
cin.ignore();
cout << "Book: ";
cin >> n;
display(bookArr, arrN, n);
destroyRecord(bookArr);
return EXIT_SUCCESS;
} …Run Code Online (Sandbox Code Playgroud) 当我们尝试调整malloc使用分配的内存大小时realloc,我们通常会这样做:
char *ptr = (char *)malloc(size_1);
ptr = (char *)realloc(ptr, size_2);
Run Code Online (Sandbox Code Playgroud)
如果size_2可能大于或小于size_1.如果新大小较大,则旧数据不会丢失,新分配的字节未初始化.ptr如果旧地址处没有足够的内存来连续存储所有字节,则包含的起始地址可能会改变.realloc将旧块的内容移动到新块中,并ptr指向该新块的初始字节.
但是,如果使用分配内存calloc,我无法理解realloc函数的行为方式.有人可以给我一个关于如何realloc分配内存的工作的简要概述calloc吗?
我有以下结构:
typedef struct pair {
int *key; // search key for this item
int *count; // pointer to data for this item
struct pair *next; // children
} pair_t;
typedef struct counters {
struct pair *head;
} counters_t;
Run Code Online (Sandbox Code Playgroud)
我有以下功能:
static pair_t * // not visible outside this file
pair_new(const int key)
{
pair_t *pair = malloc(sizeof(pair_t));
free(pair->key);
free(pair->count);
if (pair == NULL) {
// error allocating memory for pair; return error
return NULL;
} else {
pair->key = malloc(sizeof(int));
pair->count …Run Code Online (Sandbox Code Playgroud) int *p=malloc(20);
Run Code Online (Sandbox Code Playgroud)
现在Heap将分配20个字节的内存.并将第一个字节的地址返回到指针p.(假设没有返回NULL指针).
现在我这样做,
int *q=realloc(p, 40);
Run Code Online (Sandbox Code Playgroud)
现在他们有以下可能性:
1].q = P
2].!Q = P
3].Q = NULL
忘了可能性2和3.
现在我写道:
free(p);
Run Code Online (Sandbox Code Playgroud)
现在会发生什么?
前20个字节是否会被释放,休息仍然会被分配,或者所有40个字节都将被释放还是其他东西?
我几乎每隔三次就会遇到一个分段错误,我正在努力理解为什么.我认为它的原因是使用malloc()和free()错误.我必须读取用户标准输入,然后使用malloc将其保存在数组中.这部分工作,直到错误开始发生.
我的代码:
char *Input() {
char user_input;
int length;
char *buffer = malloc(2 * sizeof(char));
while (((user_input = getchar(stdin)) != EOF) && (user_input != '\n')) {
buffer[length] = user_input;
length++;
char *buffer_new = realloc(buffer, length + 2);
if (buffer_new != NULL) {
buffer = buffer_new;
} else {
free(buffer);
printf("Error.\n");
return 1;
}
}
buffer[length] = '\0';
if (strlen(buffer) > 200) {
printf("Error.\n");
return 2;
}
return buffer;
}
Run Code Online (Sandbox Code Playgroud)
我不确定我的错误在哪里或错误发生的原因.以下是我如何调用该函数:
int main() {
char *input = …Run Code Online (Sandbox Code Playgroud)