为函数中结构内部的指针分配内存
Allocating memory for pointers inside structures in functions
假设我声明了一个结构如下:
typedef struct {
int *numbers;
int size; // Size of numbers once treated as array
} sstruct;
然后我使用 sstruct *example;
.
在 main()
中创建了一个指向结构的指针(以便稍后通过引用传递它)
然后我有一个函数,称之为allocpositions()
,我在其中假装为*example
中包含的*p
指针分配内存位置。
如果我想将位置分配给 *example
,将方向 &example
传递给函数就足够了,该函数将接收它作为 **a
然后做一些事情像 a = (sstruct **)malloc(N*sizeof(sstruct *))
,但我不知道如何在函数内部直接分配给 *p
。
一旦分配,我是否仍然能够将 *p
中的元素引用为 allocpositions()
中的 example->p[index]
?
如有任何帮助,我将不胜感激!
编辑
说明我尝试实现的示例代码:
typedef struct {
int *numbers;
int size; // size of numbers once treated as array
} ssm;
main() {
ssm *hello;
f_alloc(&hello);
}
void f_alloc(ssm **a) {
// Here I want to allocate memory for hello->p
// Then I need to access the positions of hello->p that I just allocated
}
带注释的代码:
void f_alloc(ssm **a) {
*a = malloc(sizeof(ssm)); // Need to allocate the structure and place in into the *a - i.e. hello in main
(*a)->p = malloc(sizeof(int)); // Allocate memory for the integer pointer p (i.e. hello ->p;
}
编辑
我认为这就是您的要求:
void f_alloc(ssm **a, unsigned int length) {
*a = malloc(sizeof(ssm)); // Need to allocate the structure and place in into the *a - i.e. hello in main
(*a)->p = malloc(sizeof(int) * length); // Allocate memory for the integer pointer p (i.e. hello ->p;
(*a)->v = length; // I am assuming that this should store the length - use better variable names !!!
}
然后一个函数到set/get
bool Set(ssm *s, unsigned int index, int value) {
if (index >= s->v) {
return false;
}
s->p[index] = value;
return true;
}
bool Get(ssm *s, unsigned int index, int *value) {
if (index >= s->v) {
return false;
}
*value = s->p[index];
return true;
}
我把免费位留给 reader。
编辑 2
因为心情好
void Resize(ssm**a, unsigned int new_length)
{
(*a)->p = relloc((*a)->p, sizeof(int) * new_size);
(*a)->v = new_length;
}
void Free(ssm *a)
{
free(a->p);
free(a);
}
你可以提高容错性来检查malloc/realloc
是否有效
假设我声明了一个结构如下:
typedef struct {
int *numbers;
int size; // Size of numbers once treated as array
} sstruct;
然后我使用 sstruct *example;
.
main()
中创建了一个指向结构的指针(以便稍后通过引用传递它)
然后我有一个函数,称之为allocpositions()
,我在其中假装为*example
中包含的*p
指针分配内存位置。
如果我想将位置分配给 *example
,将方向 &example
传递给函数就足够了,该函数将接收它作为 **a
然后做一些事情像 a = (sstruct **)malloc(N*sizeof(sstruct *))
,但我不知道如何在函数内部直接分配给 *p
。
一旦分配,我是否仍然能够将 *p
中的元素引用为 allocpositions()
中的 example->p[index]
?
如有任何帮助,我将不胜感激!
编辑
说明我尝试实现的示例代码:
typedef struct {
int *numbers;
int size; // size of numbers once treated as array
} ssm;
main() {
ssm *hello;
f_alloc(&hello);
}
void f_alloc(ssm **a) {
// Here I want to allocate memory for hello->p
// Then I need to access the positions of hello->p that I just allocated
}
带注释的代码:
void f_alloc(ssm **a) {
*a = malloc(sizeof(ssm)); // Need to allocate the structure and place in into the *a - i.e. hello in main
(*a)->p = malloc(sizeof(int)); // Allocate memory for the integer pointer p (i.e. hello ->p;
}
编辑
我认为这就是您的要求:
void f_alloc(ssm **a, unsigned int length) {
*a = malloc(sizeof(ssm)); // Need to allocate the structure and place in into the *a - i.e. hello in main
(*a)->p = malloc(sizeof(int) * length); // Allocate memory for the integer pointer p (i.e. hello ->p;
(*a)->v = length; // I am assuming that this should store the length - use better variable names !!!
}
然后一个函数到set/get
bool Set(ssm *s, unsigned int index, int value) {
if (index >= s->v) {
return false;
}
s->p[index] = value;
return true;
}
bool Get(ssm *s, unsigned int index, int *value) {
if (index >= s->v) {
return false;
}
*value = s->p[index];
return true;
}
我把免费位留给 reader。
编辑 2
因为心情好
void Resize(ssm**a, unsigned int new_length)
{
(*a)->p = relloc((*a)->p, sizeof(int) * new_size);
(*a)->v = new_length;
}
void Free(ssm *a)
{
free(a->p);
free(a);
}
你可以提高容错性来检查malloc/realloc
是否有效