标签: dynamic-arrays

如何在 FreePascal 中正确传递动态数组

在我编写的这个 FreePascal 代码中,我发现在一个长度为“n”的动态数组中,它总是在元素“n”中包含一个随机值。

我理解为什么会这样,但是,我想知道我编写代码的方式是否存在缺陷。我把它粘贴在下面。我欢迎任何更正。

program LinearSearch;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils
  { you can add units after this };

{$R *.res}

type
  ArrayOfByte = array of Byte;

Function findMaximum(studentMarks : ArrayOfByte; numberOfStudents : integer)
: integer;
var
  maximumMark : Integer;
  studentWithMaximumMark : Integer;
  index : integer;
begin
  setLength(studentMarks, numberOfStudents);
  maximumMark := studentMarks[0];

  for index:=0 to numberOfStudents do
  begin
    write(IntToStr(studentMarks[index]));
    if index = numberOfStudents then writeln('.')
    else write(',');
  end;

  for index:= 0 to (numberOfStudents - 1) …
Run Code Online (Sandbox Code Playgroud)

freepascal parameter-passing dynamic-arrays

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

如果 malloc() 只分配一块只能存储一个变量的内存,那么如何使用 malloc() 创建动态数组?

malloc()函数创建一整块内存,然后返回第一个字节的指针。如果它只创建一块内存,那么它只能存储一个元素,对吧?但是如何malloc()将多个元素仅存储在一个内存块中,而当创建多个内存块时才有意义 calloc(),并且它可以用作动态数组。

c dynamic-arrays

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

C realloc() 无效指针错误即使使用了 malloc

我有一个动态数组实现,我正在为泛型类型工作。我的理解是realloc的无效指针错误通常是由于没有使用malloc分配原始指针引起的,但是,我使用的是malloc。

这是我的 array.h 代码

#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

struct array {
    void *elems;
    size_t obj_size;
    size_t size;
    size_t capacity;
};

struct array* new_array(size_t objsize);
int push_back(struct array* a, const void* value); 
Run Code Online (Sandbox Code Playgroud)

数组

#include "array.h"
#include <stdio.h>
#include <string.h>
#define INITIAL_SIZE (1)
#define ARR_AT(a, i) ((void *) ((char *) (a)->elems + (i) * (a)->obj_size))

struct array* new_array(size_t objsize) {
    struct array* a;

    a = malloc(sizeof a + INITIAL_SIZE * objsize);
    if (!a) { return NULL; }

    a->elems = malloc(objsize); …
Run Code Online (Sandbox Code Playgroud)

c realloc dynamic-arrays

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

如何在 C++ 中动态分配 2D std::array 或者为什么我不应该使用它?

我想在我的代码中分配一个数组,并且它的大小应该在运行时定义。

我尝试这样:

#include <iostream>
#include <array>

int main(){
    int M=4,N=3,P=5;
    M=N+P;
    std::array<std::array<double,M>,N> arr;
}
Run Code Online (Sandbox Code Playgroud)

但 MSVC 告诉我:

a variable with non-static storage duration cannot be used as a non-type argument
Run Code Online (Sandbox Code Playgroud)

我在 stackoverflow 中找不到这个问题的答案。(现有的问题似乎不能解决我的问题......)

如何在 C++ 中动态分配 2D std::array?

我知道我可以用来std::vector解决这个问题。但是向量内存大小需要我自己组织,这在我的项目中会多次使用。而且我想使用 C++ 类型代码而不是 C 类型...也许有一种方法可以将 C 类型中的二维数组转换为std::array,但我无法通过 Google 找到它...

所以我问这个问题...

我的意思是 M 和 N 应该动态获取(不改变,但我只能在运行时知道它......),例如:

#include <iostream>

int main(){
    int a=3;
    int b=4;
    int rowCount=a+b;
    int colCout=b-a;
    int** a = new int*[rowCount];
    for(int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-arrays

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

这个数组程序有什么问题?

我在这个程序的两个不同的地方得到了相同的错误,它应该是一个1d,一个2d和一个3d数组并存储值并同时显示它们.错误:下标需要数组或指针类型/表达式必须具有指针到对象类型,错误是表达式c [r] [c] [depth]

#include<iostream>
using namespace std;
#define ROW 5
#define COL 5
 #define DEPTH 5

int main()
{
int *a;           // 1d array
a=new int [COL];

int (*b) [COL];          //2d array
b=new int [ROW][COL];

int (*c)[ROW][COL];
c=new int [ROW][COL][DEPTH]; // 3d array


//---------------------------------------------------------------------------------



// storing values in the arrays:

for(int i=0;i<COL;i++)
{
    a[i]=i+2;
    cout << a[i];
}

// 2d array
for(int r=0;r<ROW;r++)
{
    for(int c=0;c<COL;c++)
    {
        b[r][c]=r+c+2;
        cout << b[r][c];
    }
}

// 3d array
for(int r=0;r<ROW;r++)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ arrays new-operator multidimensional-array dynamic-arrays

-1
推荐指数
1
解决办法
287
查看次数

如何为二维数组动态分配内存

我最近去过面试,他们让我写了一个程序,为二维数组动态分配内存(i = 3和j = 2)

c dynamic-arrays

-1
推荐指数
1
解决办法
1万
查看次数

动态数组中扫描值的分段错误(int**arr)

此代码给出了分段错误.在GDB调试时,它给出了这个错误:

"程序收到信号SIGSEGV,分段错误.在_IO_vfscanf_internal中的0x00007ffff7a6dde5(s =,格式=,argptr = argptr @ entry = 0x7fffffffdba8,errp = errp @ entry = 0x0)at vfscanf.c:1902 1902 vfscanf.c:没有这样的文件或目录. "

void  readData()
{
int **arr,m;
scanf("%d",&m);
arr = (int **)malloc(sizeof(int)*m);
    for(int i=0;i<m;i++)
    {
    arr[i] = (int *)malloc(sizeof(int) * 2);
    }
    for(int i=0;i<m;i++)
    {
    printf("..%d ..\n",i); // if m = 20 then running only 12 times
    scanf("%d %d",&arr[i][0],&arr[i][1]);

    }
}

int main()
{
readData();
}
Run Code Online (Sandbox Code Playgroud)

如果m = 20那么,第二个循环只运行12次然后给出分段错误.第一个循环运行20次. 请帮帮我.

c malloc segmentation-fault dynamic-arrays data-structures

-1
推荐指数
1
解决办法
96
查看次数

C中的动态1D数组不希望在每个框中摄取文件的每个字符

在C中,我想用每个框中的文件的每个字符填充一个动态数组.但相反,当我打印我的阵列时,我有:

0 = [] 
1 = [] 
2 = [] 
3 = [] 
4 = [] 
5 = [] 
6 = [] 
7 = [] 
8 = [] 
9 = []
Run Code Online (Sandbox Code Playgroud)

我没有编译错误,但valgrind说我有一个:

Conditional jump or move depends on uninitialised value(s) 
Run Code Online (Sandbox Code Playgroud)

在我主要的printf.这很奇怪,因为即使我的主要初始化:

array_valid_char = NULL;
Run Code Online (Sandbox Code Playgroud)

valgrind不断给我这个错误.即使我改为:

printf("%d = [%d] \n", i, array_valid_char[i]);
Run Code Online (Sandbox Code Playgroud)

显示是一样的.

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int* f(char* name_file_in)
{
    FILE *file_in;
    int* array_valid_char = malloc(10 * sizeof(int*));
    int read_char;
    file_in = fopen(name_file_in,"rb");
    if(file_in)
    {
        while ((read_char = fgetc(file_in)) …
Run Code Online (Sandbox Code Playgroud)

c file dynamic-arrays

-1
推荐指数
1
解决办法
74
查看次数

arduino中的动态数组声明

我正在尝试编写一个代码,当给出正确的密码时可以打开门。但是,在这样做的过程中,我遇到了一个问题。我必须将输入的密码存储在动态数组中。我知道有一个用于制作动态数组的包,但我不知道制作动态数组的代码。我发布了到目前为止我所做的代码,即仅启动键盘。

#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

byte rowPins[ROWS] = { 8, 7, 6, 9 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {

  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {

  // put your main code here, to run repeatedly:

}
Run Code Online (Sandbox Code Playgroud)

arduino dynamic-arrays

-1
推荐指数
1
解决办法
5879
查看次数

如何归零动态数组?

请注意,我正在学习Visual C++.

据我所知,我们应该以这种方式将动态数组归零:

void main()
{
    int *arr = new int[15];
    memset(arr,0,15*sizeof(*arr));
    ...
    delete [] arr;
}
Run Code Online (Sandbox Code Playgroud)

但我发现了另一种方法:

void main()
{
    int *a = new int[15]();
    ...
    delete [] arr;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我在括号后添加了两个括号.但它只有在我设置数组的大小时才有效.

我找不到任何关于此的信息,我不知道这需要什么目的.

编辑:第一个代码现在由VC编译器编辑和验证.EDIT2:所以我在MSDN上找不到关于第二代码的手册.在那里我可以找到这个?(没有谷歌PLZ)

c++ dynamic-arrays

-2
推荐指数
1
解决办法
180
查看次数