我一直在寻找一种方法来自己做这件事,但我还没有找到与我的情况完全匹配的情况,而且我的经验不足,无法从类似情况中得出该怎么做。所以我希望在我的具体情况下得到一些帮助。
我有一个结构体,我需要创建 3 个它们的数组。但是当我使用 [] 分配内存时,内存不足。所以我想我需要使用malloc;但我不知道该怎么做。这是我的代码:
struct key {
char symbol[10];
int quantity;
char GroupID[10];
};
Run Code Online (Sandbox Code Playgroud)
然后在主要我有:
struct key PrevKeys= malloc(345000*sizeof(struct key));
struct key ActivityKeys= malloc(345000*sizeof(struct key));
struct key CurKeys= malloc(345000*sizeof(struct key));
Run Code Online (Sandbox Code Playgroud)
但是我不断从编译器中收到“无效的初始值设定项”错误。
早些时候我试过这个,它编译得很好,但是当我运行它时给了我一个段错误(我假设是因为堆栈中没有足够的内存):
struct key PrevKeys[345000];
struct key ActivityKeys[345000];
struct key Curkeys[345000];
Run Code Online (Sandbox Code Playgroud)
关于如何分配这 3 个结构数组的任何想法都将非常非常感谢。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *p = malloc(10);
int i;
for(i=0;i<15;i++)
{
p[i]='c';
printf("INDEX:%d %c\n",i,p[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么在上面的代码中,我只分配了 10 块内存,但我仍然能够访问指针的第 15 个索引。
我不确定为什么,但我认为这是因为这个指针指向一些随机的内存块,而我只是覆盖了这部分内存,但我只分配了特定数量的内存,所以我不确定为什么它会起作用。
有人可以确认吗?
使用gcc -Wall -std = c99进行编译时遇到错误:
pokerhand.c:20:17: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
Card *cards = malloc(sizeof(Card)*5);
这是我的错误发生的代码
typedef struct card
{
char suit;
char *face;
} Card;
typedef struct hand
{
Card *cards = malloc(sizeof(Card)*5);
char *result;
} Hand;
Run Code Online (Sandbox Code Playgroud)
在这些结构之前我所拥有的只是标题包括
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
Run Code Online (Sandbox Code Playgroud) malloc(0)C11标准中的行为引用以下*:
void*malloc(size_t size);
如果size为零,则行为是实现定义的(可能返回空指针,或者可能返回一些非空指针,这些指针可能不用于访问存储,但必须传递给free).
*(引自cppreference.com而不是官方的ISO/IEC 9899:2011标准,因为前者需要购买.)
有了这个定义,我就不会期望以下工作(用icc18.0.0 编译):
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
double *u = malloc(0);
u[0] = 3.0;
printf("The value of u[0] is %lf", u[0]);
free(u);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是它起作用并给出了输出:
The value of u[0] is 3.000000
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
它似乎u[0] = 3.0;访问存储.如果malloc返回NULL,则会导致分段错误.
我是否误解了访问存储的概念,或者是否还有其他事情发生?
更新/澄清
似乎我的问题的关键在于" 可能不会用于访问存储 " 的措辞,以及"可能不"意味着不应该或 …
试图传递数组的指针:
class aaa{
public:
int a ;
int b ;
std::string c ;
};
void abc(aaa* a [])
{
*a = (aaa*)malloc(sizeof(aaa)* 5);
a[0]->c ="ddd" ;
a[1]->c ="ccc" ; //crash
a[2]->c ="eee" ;
}
int main() {
aaa * a;
abc(&a);
cout << "!!!Hello World!!!"<< a++->c << endl;
cout << "!!!Hello World!!!"<< a++->c << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在第二个数组元素赋值我崩溃了.问题出在哪儿?并malloc没有创造足够的空间?
UPD.
void abc(aaa* a [])由于某种原因,我无法更改功能签名.即使看起来不太好,它也不是错误的签名.
我根据recomendations在答案中更新了程序,但我仍然在获取第二个数组元素成员时崩溃:
cout << "!!!Hello World!!!"<< a[1].c << endl;
Run Code Online (Sandbox Code Playgroud)
为什么?我在下面的代码中做错了什么?
struct aaa{
public:
int …Run Code Online (Sandbox Code Playgroud) 这是一个基本的堆栈实现代码.但是,它会产生信号中止.
int *arr;
int size = 2;
int top = 0;
int pop() {
int i;
if (top <= size / 4) {
int *arr2 = (int*)malloc(sizeof(int) * size / 2);
for ( i = 0; i < size; i++)
arr2[i] = arr[i];
free(arr);
arr = arr2;
size /= 2;
}
return arr[--top];
}
void push( int a) {
int i;
if (top >= size) {
int *arr2 = (int*)malloc(sizeof(int)*size * 2);
for ( i = 0; i < size; …Run Code Online (Sandbox Code Playgroud) 我试图释放为该malloc函数分配的空间,但每次收到此消息时:
“malloc:*** 对象 0x7ffeefbff510 错误:未分配正在释放的指针”
即使我明确地将其分配为malloc().
我已经尝试过发送& manufacture并且只是manufacture。我对整个内存分配非常陌生。
char* manufacture = (char*)malloc(15*sizeof(char));
manufacture = "Suzuki";
free(manufacture);
Run Code Online (Sandbox Code Playgroud) 为什么这段代码没有给我分段错误?我只分配了1个字符,但输入了1个以上的字符。
char **names;
names=malloc(2*sizeof(char *));
names[0]=malloc(sizeof(char)*1) ;
names[0]="ATCAHCTACHATCCACTATCAHCTACHATCCACTATCAHCTACHATCCACTATCAHCTACHATC";
printf("%s",names[0]);
Run Code Online (Sandbox Code Playgroud)
我希望它会产生细分错误。
我对 C 语法感到困惑。如果我分配内存:
int* x = (int*)malloc(1 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)
int 指针的这两个代码片段是否相等?
*x = 0;
Run Code Online (Sandbox Code Playgroud)
和
x[0] = 0;
Run Code Online (Sandbox Code Playgroud) 我希望这里不要听起来很愚蠢,但是 NULL 模块在执行此操作时实际上是否需要内存分配:
TheNull = malloc(sizeof(NULL));
Run Code Online (Sandbox Code Playgroud)
如果是真的,那么没有分配内存的东西怎么可能存在于内存中呢?