如何创建一个非常大的数组?好吧,我无法创建一个大小为INT_MAX的数组..怎么可能实现这一点.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define SIZE 2147483647
int main() {
int *array;
unsigned int i;
array = malloc(sizeof(int) * SIZE);
if(array == NULL) {
fprintf(stderr, "Could not allocate that much memory");
return 1; }
for(i=0; i<1; i++) {
array[0] = 0;
}
free(array);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试int根据运行时获得的大小创建并归零一个s 数组:
size = [gamePiece.availableMoves.moves count]; //debugger shows size = 1;
int array[size]; //debugger shows this as int[0] !
memset(array, 0, size);
indexes = array;
Run Code Online (Sandbox Code Playgroud)
size并且indexes都是这个类的ivars:
int size;
int* indexes;
Run Code Online (Sandbox Code Playgroud)
不过,我最终得到了一个0长度的数组.如何用指示的尺寸创建它[gamePiece.availableMoves.moves count]?
C++中的伪代码
char* data = new char[determine_size()];
// ... do some stuff with data
delete[] data;
// ... repeat process
Run Code Online (Sandbox Code Playgroud)
所以基本上,每次都会将数据重复分配给具有不同大小的新数组.虽然每次先前的分配总是先取消分配.
这会导致内存碎片吗?
我对C++中的内存管理不是很熟悉; 我的直觉告诉我这不是一个好主意.
我试图读取包含随机数列表的文本文件,并使用mergesort进行排序.数字被读入动态数组.不幸的是,每当我尝试删除未使用的数组时,都会检测到堆损坏错误.
Mergesort功能:
void mergesort(int *arr, int first, int last)
{
if(first < last)
{
int middle = ((first + last)/2);
mergesort(arr, first, middle);
mergesort(arr, middle+1, last);
merge(arr, first, last);
}
}
Run Code Online (Sandbox Code Playgroud)
删除tempArr时合并函数出错:
void merge(int *arr, int first, int last)
{
int *tempArr = new int[last];
int mid = (first+last)/2;
int first1 = first;
int last1 = mid;
int first2 = mid + 1;
int last2 = last;
int index = first1;
for(; (first1 <= last1) && (first2 <= last2); ++index) …Run Code Online (Sandbox Code Playgroud) 我显然无法抽象地思考这个...但我想从一个使用数组值作为属性名的数组中创建一个Javascript对象,但它们应该是彼此嵌套的对象.
所以,如果我有这样的数组:
['First', 'Second', 'Third', 'Fourth']
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:
{
First: {
Second: {
Third: {
Fourth: {}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新 这是我在提交中提到的函数:
function appendKey(obj, to, key) {
if (obj.hasOwnProperty(to)) {
appendKey(obj[to], to, key);
} else {
obj[key] = {};
}
return obj;
}
Run Code Online (Sandbox Code Playgroud)
我的意图是这样称呼它:
var data = ['First', 'Second', 'Third', 'Fourth'];
data = appendKey(data, 'First', 'Second');
data = appendKey(data, 'Second', 'Third');
data = appendKey(data, 'Third', 'Fourth');
Run Code Online (Sandbox Code Playgroud)
很明显,这可以放入循环,这就是为什么我想这样做.我的输出结果是:
data = { 'First' : { 'Second' } } // it works this …Run Code Online (Sandbox Code Playgroud) 我需要创建一个特定对象的数组,其中我需要的数字依赖于一个单独的变量,解释它的最佳方法是使用psudo代码示例:
int num = 4;
for(int i=0;i<num;i++){
object_type arrayi [dynamic size];
}
Run Code Online (Sandbox Code Playgroud)
所以我需要4个数组,每个数组都有名称array0,array1,array2和array3,它们都必须是动态数组.无论如何在C++中这样做?
我有这个数组Cards[] temp = new Cards[13]; ,其中Cards是一个有52个对象的类.据我所知,这个语句将创建一个数组,其中包含13个卡数据类型的对象.我只是想知道在将值放在这个数组中的值之前是一个NULL的垃圾?我是说写完之后
Cards[] temp = new Cards[13];
Run Code Online (Sandbox Code Playgroud)
在放入实际值之前,在此语句之后存在哪些元素.无论是空还是垃圾.更多的解释是在编译时13对象的内存将动态分配给数组或卡我想知道在编译时该内存中的值是什么.要么是NULL还是一些垃圾?
我将创建一组结构值.条目的数量取决于输入,因此无法估计数组的长度.
在FOR循环中,解析输入,我会为每次迭代创建一个条目.这意味着我需要重新分配数组,因为大小增加,这导致性能效率低下.
如果我被允许用C++编程,我会使用vector.不幸的是我不能,我想不出更好的主意.
请帮助我,任何建议将不胜感激.
我不是一个说英语的好人.
所以在我的程序中,我想将存在于txt文件中的文本复制到数组中.
typedef struct Chaine
{
char * Lachaine;
int Taille_C;
} Chaine ;
int main (void)
{
Chaine *Tab_Texte=NULL;
Tab_Texte=(Chaine*)malloc(sizeof(Chaine));
FILE* Texte= NULL;
Texte = fopen("chaines", "r");
fseek(Texte, 0, SEEK_END);
Tab_Texte->Taille_C=ftell(Texte);
fseek(Texte, 0, SEEK_SET);
Tab_Texte->Lachaine=NULL;
Tab_Texte->Lachaine=(char*)malloc(sizeof(char)*Tab_Texte->Taille_C);
fread(Tab_Texte->Lachaine,sizeof(char)*(Tab_Texte->Taille_C),1,Texte);
printf("%s",Tab_Texte->Lachaine);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里,一切都很好,当我改变时
Tab_Texte->Lachaine=(char*)malloc(sizeof(char)*Tab_Texte->Taille_C);
Run Code Online (Sandbox Code Playgroud)
与(例如)
Tab_Texte->Lachaine=(char*)malloc(sizeof(char)*Tab_Texte->Taille_C - 10);
Run Code Online (Sandbox Code Playgroud)
它始终有效,它假设向我显示分段错误,因为sizeof(char)*Tab_Texte->Taille_C - 10它比sizeof(char)*Tab_Texte->Taille_C文件中的文本短.
你能告诉我为什么它总能奏效吗?
dynamic-arrays ×10
arrays ×4
c ×4
c++ ×3
java ×2
dynamic ×1
javascript ×1
mergesort ×1
objective-c ×1
pointers ×1
structure ×1
vector ×1