标签: dynamic-memory-allocation

c ++中的动态分配不检查数组边界?

这是我的代码

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
     int* arr = new(nothrow)int [100];
     int  i;
     if(arr == 0){//heap is full or dynamic allocation fails
     cout<<"Cannot allocate memory\n";
     return 0;
    }
    ofstream file("myFile.bin",ios::out|ios::binary);//opening the file in binary mode

    for(i = 0;i<100;++i){//dynamic array which contains numbers form 0 to99
        arr[i] = i;
    }
    if( file.is_open() ){

        if( file.good() )
            file.write((char*)arr,400);

        delete [] arr;
        file.close();
    }

    ifstream file1("myFile.bin",ios::in|ios::binary|ios::ate);
    ifstream::pos_type size;
    char* buff;
    if(file1.is_open()){

        size = file1.tellg();
        buff = new char[size];
        file1.seekg(0);

        if( …
Run Code Online (Sandbox Code Playgroud)

c++ file dynamic-memory-allocation

0
推荐指数
1
解决办法
242
查看次数

如果将数据移动到不同的块,c ++ realloc函数是否对旧数据块应用删除操作?

该功能可以将存储块移动到新位置,在这种情况下返回新位置.
例如,我有一个指向数组的指针:

int *arr; // somewhere next it initialized, filled with elements and etc
Run Code Online (Sandbox Code Playgroud)

在某个地方我需要:

void* location = realloc(arr, NEW_SIZE);
Run Code Online (Sandbox Code Playgroud)

旧的内存块会发生什么?

如果realloc返回不是数学的指针到arr,我应该使用下一个代码吗?:

delete arr;
arr = (int*)location;
Run Code Online (Sandbox Code Playgroud)

c++ memory-management realloc dynamic-memory-allocation

0
推荐指数
1
解决办法
1267
查看次数

Fortran可分配的数组生命周期

说我有以下代码:

program test
  call foo
  call foo
contains
  subroutine foo
    integer(8),dimension(:),allocatable:: var1
    allocate(var1(10))
    ...
    return
  end subroutine foo
end
Run Code Online (Sandbox Code Playgroud)

变量var1会被分配两次吗?(我猜是).如果为每个呼叫分配,第一次呼叫期间分配的内存是否会空闲?

fortran memory-management dynamic-memory-allocation fortran90 fortran95

0
推荐指数
1
解决办法
899
查看次数

新动态记忆"第一次"

为什么错误

#include <stdio.h>

int main(void)
{
    int *p, size, i;
    FILE *fp;

    fp = fopen("input.txt","r");
    fscanf(fp, "%d", &size);

    p = (int*)malloc(size*sizeof(int));  //error
    for (i = 0; i <size; i++)
        fscanf(fp, "%d", &p[i]);

    for (i = size-1; i>= 0; i--)
        printf("%d\n", p[i]);

    free(p);
    fclose(fp);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在ubuntu上使用"Geany"

并在Geany编译器上:

fileName.c:11:2:警告函数'malloc'的隐式声明[-Wimplicit-function-declatation] fileName.c:11:12:警告:内置函数'malloc'的不兼容隐式声明[默认启用] fileName.c:18:12:warning:函数'free'的隐式声明[-Wimplicit-function-declaration] fileName.c:18:12:警告:内置函数'free'的不兼容隐式声明[enabled-by默认]编译成功完成

c dynamic-memory-allocation

0
推荐指数
1
解决办法
249
查看次数

c中的动态数组初始化

根据C99标准我们可以做到这一点

int n = 0;
scanf("%d",&n);
int arr[n];
Run Code Online (Sandbox Code Playgroud)

这是在c中创建动态数组的方法之一.现在我想将这个数组初始化为0这样

int arr[n] = {0};
Run Code Online (Sandbox Code Playgroud)

这里我的编译器产生错误.我想知道我们可以这样做吗?它是按照标准吗?在编译时,我们为数组提供了足够的内存,但是在编译时它是未知的.怎么回事?

c dynamic-memory-allocation

0
推荐指数
1
解决办法
1111
查看次数

C++破坏指针数组

所以这是一个关于动态内存分配和对象创建的C++练习.基本上 - 一个自定义类学生和一个自定义类组,它保留了一系列指向学生内部的指针.Group的析构函数显然存在问题,但我花了几个小时阅读手册和浏览论坛,但仍然无法理解我做错了什么.

欢迎任何评论.
UPD:问题是 - 退出时出错."调试断言失败了...... _BLOCK_TYPE_IS_VALID ......"

class Student{
    char *firstName;
    char *lastName;

public:

    Student(): firstName(NULL), lastName(NULL){}

    Student(const char *fname, const char *lname){
        firstName = new char[32];
        lastName = new char[32];
        strcpy_s(firstName, 32, fname);
        strcpy_s(lastName, 32, lname);
    }

    void Show(){
        cout << firstName << " " << lastName << endl;
    }

    ~Student(){

        if(lastName != NULL)
            delete[] lastName;

        if(firstName != NULL)
            delete[] firstName;
    }
};

class Group{
    Student *students;
    int studentCounter;
public:

    Group(){
        students = NULL;
    }

    Group(const int …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers destructor dynamic-memory-allocation

0
推荐指数
1
解决办法
186
查看次数

C:realloc()无法按预期运行

如果我有一个变量,str我想在堆上分配内存,我会使用malloc():

char* str = (char*)malloc(sizeof("Hello"));
Run Code Online (Sandbox Code Playgroud)

malloc()返回一个void*指针,这是我的记忆所在的内存位置.所以现在,我可以给它一些数据

str = "Hello";
Run Code Online (Sandbox Code Playgroud)

所以,内存位置现已满,有6个字节.现在,我想增加它的大小,以包含字符串"Hello World".所以,我用realloc().根据man, void* realloc(void *ptr, size_t size)将:

The realloc() function changes the size of the memory block pointed to by ptr to size bytes. 
The contents will be unchanged in the range from the start of the region up to the minimum of 
the old and new sizes. If  the  newsize is larger than the old size, …
Run Code Online (Sandbox Code Playgroud)

c memory-management realloc dynamic-memory-allocation

0
推荐指数
1
解决办法
199
查看次数

C char****数组动态分配行为尴尬

类似于20个小时的事情已经过去了,我仍然不会想要得到什么问题.

首先,这些参数有主要功能.

  char ***matrix;
  int line, maxCollumn, i, j;
  int lineData[100];
Run Code Online (Sandbox Code Playgroud)

然后我正在调用该函数

PutFirstAndLast(&matrix, &line, &maxCollumn, lineData);
Run Code Online (Sandbox Code Playgroud)

此函数用于动态分配我的矩阵并返回其行数和列数.lineData用于存储我在每行上有多少项,因为稍后在函数中我将用""填充"空"空格.

我的麻烦功能有这个标题

int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int *lineArray)
Run Code Online (Sandbox Code Playgroud)

在这个函数中,我将生成一个以半延迟方式"数组"调用的新矩阵,我将为新的"数组"分配内存,以便将每个单词从文件"in.txt"中放入其中.

int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int     *lineArray)
 {
FILE *inputFile;
char buffer[100], *p;
char ***array;
int i, j, collumn, maxCollumn = 0, line = -1;




inputFile = fopen("in.txt", "r");

while (fgets(buffer, 100, inputFile))
{

    line++;
    collumn = 0;


    if (line == 0)
        array = (char***)malloc(sizeof(char**));
    else
        array = (char***)realloc(array, sizeof(char**)* …
Run Code Online (Sandbox Code Playgroud)

c string stream matrix dynamic-memory-allocation

0
推荐指数
1
解决办法
80
查看次数

为什么这会让我违反访问权限?(C++)

我正在为我的数据结构类构建一个vector类,我无法弄清楚为什么会抛出异常.这是完整的Vector.h文件:

#include <iostream>

using namespace std;

template <class T>
class Vector {
private:

  // not yet implemented
  Vector(const Vector& v);
  Vector& operator=(const Vector& v);
  T * Tarray;
  int arraySize;
  int currentSize;

public:

Vector() {
    arraySize = 2;
    currentSize = 0;
    Tarray = new T[arraySize];
};
~Vector() {
    delete[] Tarray;
};

void push_back(const T &e) {
    ++currentSize;
    if (currentSize > arraySize) {
        arraySize *= 4;
        T * temp = new T[arraySize];

        for (int i = 0; i < currentSize; i++) { …
Run Code Online (Sandbox Code Playgroud)

c++ vector dynamic-memory-allocation

0
推荐指数
1
解决办法
119
查看次数

malloc如何获取比分配的数据更多的字节数据

根据我的理解,malloc()它允许我们在运行时动态分配内存.以下是我正在研究的代码

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
  char *description;
  clrscr();
  description = malloc(2*sizeof(char));
  strcpy(description,"Hello there!");
  printf("%s",description);
  free(description);
  printf("%s",description);
  getch();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我要求系统在malloc()函数中分配2个字节的内存.因此,当我尝试填充字符串"Hello there!"并打印相同时,我应该只获得字符串的前2个字符作为输出,但我得到了我在strcpy()函数输出中给出的整个字符串.

并且在我使用free()函数后,如果我description再次尝试打印,如果我没有错,我不应该得到任何输出,但我仍然得到相同的字符串.可能知道这是如何工作的.我正在使用turbo C++编译器.

c malloc free pointers dynamic-memory-allocation

0
推荐指数
1
解决办法
331
查看次数