参数传递错误 [英] Errors on parameter passing

查看:57
本文介绍了参数传递错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨''身体,

我在参数传递方面遇到了困难。

我在过去几个月里写了一个库,现在它在一个库中崩溃了几次

月份因为参数传递错误:

在核心转储上使用gdb我可以看到有时在函数中

调用,调用者通过一个值和被调用者接收另一个

值...通常它似乎是一个转移参数(被调用者

找到存储在第二个参数中的第一个值,所以在...)。

这已经很奇怪但是上次我看到一个更奇怪的事情:

调用者只传递指向结构的指针而被调用者没有

接收指针,它接收

结构实例的第一个元素的值!!


以上情况是类似这样的事情:


struct SimpleStruct

{

void * a;

} SimpleType;


void callee(SimpleType *实例)

{

...

}


void caller()

{

SimpleType * instance =(SimpleType *)malloc(sizeof(SimpleType));

instance-> a = 0x123456789;

callee(实例);

}


执行中有时被叫方崩溃并使用gdb我发现

callee有instance = 0x123456789

怎么可能?任何想法??

解决方案

frakie写道:


我遇到了困难参数传递。



[Snip]


以上情况如下:


不要给我们类似的东西!将问题简化为显示问题的最小版本,然后将其剪切并粘贴到您的

帖子中。


>

struct SimpleStruct

{

void * a;

} SimpleType;



你的意思是放一个typedef在那里?


void callee(SimpleType * instance)

{

...

}


void caller()

{

SimpleType * instance =(SimpleType *)malloc(sizeof( SimpleType的));



演员不需要,可以隐藏问题。


你不能检查malloc ()工作。


您是否包含stdlib.h?


instance-> a = 0x123456789;

callee(实例);

}


执行中有时被叫方崩溃并使用gdb我发现

callee有instance = 0x123456789


怎么可能?任何的想法??



不,如果你没有向我们展示真正的代码,我们正在黑暗中工作。

文章< 07 ********************************** @ x69g2000hsx。 googlegroups.com>,

frakie< fr ******* @ gmail.comwrote:


>我写了一个库在过去几个月里,由于参数传递错误,现在它在一个月内几次崩溃:



我们不能说太多,除非你向我们发送真实,完整的代码

失败。


但是莫名其妙的一个常见原因是像这样的错误是你的b $ b没有你所有的代码同步:也许你在改变一个结构之后没有重新编译一个源文件的
一个标题,或者

之类的东西。


- 理查德


-

:wq


On 8 Gen,16:02,Mark Bluemel< mark_blue ... @ pobox.comwrote:


frakie写道:


我在参数传递方面遇到了困难。



[Snip]


以上情况如下:


别给我们类似的东西!将问题简化为显示问题的最小版本,然后将其剪切并粘贴到您的

帖子中。


struct SimpleStruct

{

void * a;

} SimpleType;



你的意思是放一个typedef在那里?


void callee(SimpleType * instance)

{

...

}


void caller()

{

SimpleType * instance = (SimpleType的*)malloc的(的sizeof(SimpleType的));



演员不需要,可以隐瞒问题。


你不能检查malloc ()工作。


您是否包含stdlib.h?


instance-> a = 0x123456789;

被调用者(实例);

}

执行
有时被调用者崩溃并使用gdb我发现

callee有实例= 0x123456789

怎么可能?任何的想法??



不,如果你没有向我们展示真正的代码,我们正在黑暗中工作。



正如我所说它是一个库,它不是一个2代码行程序..

问题出现在不同的函数调用,而不仅仅是在某个特定的函数中执行,因此它不是编译问题(

当然我在示例中忘记了typedef和i包含

必要''。h'')。

我会尝试剪切并粘贴大部分代码错误

发生...


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);;

返回;

}

/ *分配* /

if(!instance-> info)

{

instance-> size = 100;

instance-> length = 0;

instance-> info =(struct2_t * )

malloc(

instance-> size

*

sizeof(struct2_t)

);

}

其他

{

/ *重新分配* /

if(instance-> size == 0)

{

free(instance-> info);

instance- > info =(struct2_t *)

malloc(

instance-> size

*

sizeof( struct2_t)

);

}

其他

/ *增加* /

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 - >尺寸* = 10;

}

其他

/ *减少* /

if(实例) - >长度+ 10< instance->尺寸/ 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 - >长度++;

}


我想现在它更简单易读......

这里错误发生在memcpy中因为size_control没有收到

实例指针但实例 - >信息指针......而且我不知道
知道为什么..


Hi ''body,
I''m experiencing difficulties on parameter passing.
I wrote a library in last months, and now it crashes few times in a
month because of errors in parameter passing:
using gdb on the core dumps I can see that sometimes in function
calls, the caller passes a value and the callee receives another
value... Usually it seems to be a shifting on parameters (the callee
finds the first value stored in the second argument and so on...).
This is already strange but last time i saw a more strange thing: the
caller passes just a pointer to a structure and the callee does not
receive the pointer, it receives the value of the first element of the
structure instance!!

The above case is something like this:

struct SimpleStruct
{
void * a;
}SimpleType;

void callee(SimpleType *instance)
{
...
}

void caller()
{
SimpleType *instance=(SimpleType*)malloc(sizeof(SimpleType));
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??

解决方案

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.


In article <07**********************************@x69g2000hsx. googlegroups.com>,
frakie <fr*******@gmail.comwrote:

>I wrote a library in last months, and now it crashes few times in a
month because of errors in parameter passing:

We can''t tell much unless you post us the real, complete code that
fails.

But one common cause of "inexplicable" errors like this is that you
don''t have all your code in sync: perhaps you didn''t recompile one
of the source files after changing a structure in a header, or
something like that.

-- Richard

--
:wq


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..


这篇关于参数传递错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆