Tag: 分配

如何在一个分配C中动态分配2D数组

你能帮我弄清楚如何在一次分配呼叫中分配2Darrays吗? 我试着做: int ** arr =(int **)malloc(num * num * sizeof(int *)); 但它不起作用。 num是行和列。

结构数组内存realloc stderr

当我将元素放入其中时,我试图重新分配一个结构数组,因为我将元素放入其中但我仍然收到一个realloc stderr。 struct数组最终将包含235,000个元素。 当我将初始起始大小设置为100,000时,我在尝试重新分配时收到stderr。 如果我将初始起始大小设置为300,000,则不会给出错误,因为它永远不会到达realloc语句。 #define _XOPEN_SOURCE #include #include #include #include #define BUFFERLEN 200 #define START_SIZE 100000 #define GROW 1000 #define TRUE 1 #define FALSE 0 typedef struct{ char *forw; char *back; } word; typedef struct{ char *entry; } single_words; FILE *words; /*————-Function Prototypes————*/ void reverse(char* string, char* revstr, int len); int search_struct(char* find, word* […]

在C中运行时获取变量的类型

我可以在C中获取程序变量’在运行时在特定内存段中存在的类型。 C无法识别错误: int k=5; float s= 3.4; k=s; printf(“%d”, k); 我试图在运行时更改变量的类型。

C中循环中内存分配的代价

在时间成本方面,在循环内部或外部分配数组之间是否存在显着差异? 我在程序循环中的函数中使用了很多数组,我应该将所有数组作为函数参数传递以提高性能,尽管它会降低可读性吗? 例如: #include #define N 1000000 void foo() { int* array = (int*)malloc(N*sizeof(int)); /* Do something with the array */ free(array); } int main() { int i; for(i=0; i<1000000; i++) foo(); return 0; } 要么 #include #define N 1000000 void foo(int* array) { /* Do something with the array */ } int main() { […]

动态分配2D数组而不使用任何循环?

我们可以动态分配2D数组而不使用任何for循环或while循环吗? 我在c c ++中有任何直接命令或function吗?

如何在C中动态分配矩阵?

我必须做这个练习: 在C中执行一个管理名为“M”的整数矩阵和一个名为“L”的整数列表的应用程序。 M是方形矩阵[nxn],其中n由用户动态选择。 然后执行此function: 序列化:给定“M”矩阵,它返回具有n ^ 2个元素的列表L. 列表的元素是从第一个到第二个按行排序的M的元素。 第二个function: 反序列化:给定具有n ^ 2个元素的列表L,它返回一个矩阵[nxn],其中L元素按列排序。 所以main()必须是: 用户给出矩阵(n)的维度并用一些整数填充矩阵。 然后调用serialize函数,打印序列化列表 将值5添加到列表的每个值 并调用反序列化函数 并打印最后一个函数给出的矩阵。 ( 所有分配必须是动态的。 ) 我试过这个: #include #include int main(){ int n,i,j; printf(“Give the dimension of matrix [nxn]: “); scanf(“%d”,&n); int **M; M = (int**)malloc(n*sizeof(int*)); for(i=0;i<n;i++){ M[i] = (int*)malloc(n*sizeof(int*)); } int *L = serialize(M,n); int size = n*n; for(i=0;i<size;i++){ […]

C – 指针内存分配

有人可以向我解释之间的区别 int *x = malloc(sizeof(int)); && int *x = (int*)malloc(sizeof(int)); 谢谢!

我如何知道何时应该释放库函数返回的C中的字符串?

我应该使用free() ¹自己在C中释放哪些字符串? 我的知识状态: char a[256]; :不 char *a = “abcdefg”; :不 char *a = malloc(15L); :是的 getenv()返回的字符串: no Windows函数返回的字符串²:??? ¹或LocalFree() / GlobalFree() / VirtualFree() ²特别是GetCommandLineW()

在循环中调用realloc的缺点

我试图在Windows 7上用C实现一些数学算法,我需要反复增加我的数组的大小。 有时它会失败,因为realloc无法分配内存。 但是如果我在开始时一次分配大量内存它就可以正常工作。 这是内存管理器的问题吗? 有人能解释一下吗?

分配了alloca的内存在函数结束时或范围结束时被释放?

如果我有这样的function: void bla(int size) { while(b){ char tmp[size]; …… } } tmp在while循环的每次迭代中被释放,对吗? 如果我写这个函数: void bla(int size) { while(b){ char* tmp = alloca(size); …… } } tmp在范围结束时或function结束时被释放?