On 8 Gen, 16:02, Mark Bluemel <mark_blue...@pobox.comwrote:
frakie wrote:
I'm experiencing difficulties on parameter passing.
[Snip]
The above case is something like this:
Don't give us "something like"! Boil the problem down to the smallest
version that displays the problem, and then cut and paste that into your
posting.
struct SimpleStruct
{
void * a;
}SimpleType;
Did you mean to put a "typedef" in there?
void callee(SimpleType *instance)
{
...
}
void caller()
{
SimpleType *instance=(SimpleType*)malloc(sizeof(SimpleType));
The cast isn't needed, and can conceal problems.
You don't check that the malloc() worked.
Did you include "stdlib.h"?
instance->a=0x123456789;
callee(instance);
}
in execution sometimes the callee crashes and using gdb i find that
callee has instance=0x123456789
How is it possible? Any idea??
Nope, and we're working in the dark if you don't show us the real code.
As i said it is a library, it's not a 2 code line program..
And the problem occurs in different function calls, not just in a
specific one, during execution so it's not a compiling issue (of
course i forgot in the example the typedef and i included the
necessary '.h').
I'll try to cut&paste a significant part of code where the error
occurs...
typedef struct struct2_s
{
long long struct2_data;
}struct2_t;
typedef struct struct1_s
{
struct2_t *info;
int size;
int length;
}struct1_t;
void size_control(struct1_t * instance)
{
if(!instance)
{
printf("Error\n");
return;
}
/* Allocating */
if(!instance->info)
{
instance->size=100;
instance->length=0;
instance->info=(struct2_t *)
malloc(
instance->size
*
sizeof(struct2_t)
);
}
else
{
/* Reallocating */
if(instance->size==0)
{
free(instance->info);
instance->info=(struct2_t *)
malloc(
instance->size
*
sizeof(struct2_t)
);
}
else
/* Increasing */
if(instance->length>=instance->size)
{
struct2_t * t=(struct2_t *)
malloc(
instance->size*10
*
sizeof(struct2_t)
);
memcpy(t, instance->info, instance-
>length*sizeof(struct2_t));
free(instance->info);
instance->info=t;
instance->size*=10;
}
else
/* Reducing */
if(instance->length+10<instance->size/10)
{
struct2_t * t=(struct2_t *)
malloc(
(instance-
>length*5)
*
sizeof(struct2_t)
);
memcpy(t, instance->info, instance-
>length*sizeof(struct2_t));
free(instance->info);
instance->info=t;
instance->size=instance->length*5;
}
}
}
void add_data(struct1_t * instance, long long data)
{
if(!instance)
return;
size_control(instance);
instance->info[instance->length].struct2_data=data;
instance->length++;
}
I think now it's simpler and readable...
Here the error occurs in memcpy because size_control doesn't receive
the instance pointer but the instance->info pointer... And i don't
know why..