473,626 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Errors on parameter passing

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(SimpleTy pe *instance)
{
...
}

void caller()
{
SimpleType *instance=(Simp leType*)malloc( sizeof(SimpleTy pe));
instance->a=0x12345678 9;
callee(instance );
}

in execution sometimes the callee crashes and using gdb i find that
callee has instance=0x1234 56789
How is it possible? Any idea??
Jan 8 '08 #1
13 1686
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(SimpleTy pe *instance)
{
...
}

void caller()
{
SimpleType *instance=(Simp leType*)malloc( sizeof(SimpleTy pe));
The cast isn't needed, and can conceal problems.

You don't check that the malloc() worked.

Did you include "stdlib.h"?
instance->a=0x12345678 9;
callee(instance );
}

in execution sometimes the callee crashes and using gdb i find that
callee has instance=0x1234 56789
How is it possible? Any idea??
Nope, and we're working in the dark if you don't show us the real code.
Jan 8 '08 #2
In article <07************ *************** *******@x69g200 0hsx.googlegrou ps.com>,
frakie <fr*******@gmai l.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 "inexplicab le" 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
Jan 8 '08 #3
On 8 Gen, 16:02, Mark Bluemel <mark_blue...@p obox.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(SimpleTy pe *instance)
{
...
}
void caller()
{
SimpleType *instance=(Simp leType*)malloc( sizeof(SimpleTy pe));

The cast isn't needed, and can conceal problems.

You don't check that the malloc() worked.

Did you include "stdlib.h"?
instance->a=0x12345678 9;
callee(instance );
}
in execution sometimes the callee crashes and using gdb i find that
callee has instance=0x1234 56789
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(st ruct1_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>=instan ce->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<inst ance->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=instanc e->length*5;
}
}
}

void add_data(struct 1_t * instance, long long data)
{
if(!instance)
return;

size_control(in stance);

instance->info[instance->length].struct2_data=d ata;
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..
Jan 8 '08 #4
On 8 Gen, 16:28, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <07567700-570a-4781-9a2c-cf670c667...@x6 9g2000hsx.googl egroups.com>,

frakie <frakie...@gmai l.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 "inexplicab le" 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
I don't think it is a sync issue: it occurs _sometimes_ ..
Usually that code is executed regularly and as it is expected to be
executed...
Jan 8 '08 #5
frakie wrote:
On 8 Gen, 16:02, Mark Bluemel <mark_blue...@p obox.comwrote:
frakie wrote:
....
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...
I didn't see any obvious problems in the code you pasted. This is to
be expected. As a general rule, if you have a problem in your program
that you don't understand, and you try to post only the part of the
program that seems to be relevant, you're usually wrong. That's the
problem with posting incomplete code.

It would be nice if you could trim this down to a 2-line program, but
no one is asking you to. What you should do is first create a program
that reliably reproduces the problem, 100% of the time. Then, simplify
that program as much as possible, while still reliably reproducing the
problem. In the course of trimming it down, there's a good chance that
you'll figure out what the problem is. If not, post the COMPLETE
program, no matter how long it is.

If it depends upon an input file, include the input file. However,
unless you have a problem with the I/O routines which read that file,
it's generally better, to simplify your code which reads the file by
replacing it with code which initializes data with the values that it
would have had after reading the input file.

Jan 8 '08 #6
frakie wrote:
[...]
/* Reallocating */
if(instance->size==0)
{
free(instance->info);
instance->info=(struct2_ t *)
malloc(
instance->size
*
sizeof(struct2_ t)
);
Are you sure malloc(0) is what you intended to do?

--
Er*********@sun .com
Jan 8 '08 #7
In article
<73************ *************** *******@e25g200 0prg.googlegrou ps.com>,
frakie <fr*******@gmai l.comwrote:

<snip>

I notice that you never check to see that malloc() didn't fail. That
could cause all sorts of odd behavior.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 8 '08 #8
Eric Sosman wrote:
frakie wrote:
>[...]
/* Reallocating */
if(instance->size==0)
{
free(instance->info);
instance->info=(struct2_ t *)
malloc(
instance->size
*
sizeof(struct2_ t)
);

Are you sure malloc(0) is what you intended to do?
And if you do, are you aware that this is causeing implementation defined
behavoir?
malloc(0) is allowed to either return NULL or a (non NULL) pointer to 0
bytes.
What behavoir does your implementation define here?

Bye, Jojo
Jan 9 '08 #9
On 9 Gen, 08:10, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
Eric Sosman wrote:
frakie wrote:
[...]
/* Reallocating */
if(instance->size==0)
{
free(instance->info);
instance->info=(struct2_ t *)
malloc(
instance->size
*
sizeof(struct2_ t)
);
Are you sure malloc(0) is what you intended to do?

And if you do, are you aware that this is causeing implementation defined
behavoir?
malloc(0) is allowed to either return NULL or a (non NULL) pointer to 0
bytes.
What behavoir does your implementation define here?

Bye, Jojo
D'oh!
That's what happens doing cut&paste...
That was intended to be

/* Reallocating */
if(instance->size==0)
{
free(instance->info);
instance->size=100;
instance->length=0;
instance->info=(struct2_ t *)
malloc(
instance->size
*
sizeof(struct2_ t)
);
}

I know it is nearly impossible to find out the effective error from a
scratch...
I don't know when and how it happens, I know only where (in the code),
that's why i need effort to resolve it:
the library input is a highly preprocessed DVBH stream received from a
DVBH decoder and this means that i cannot reproduce error cases.
I'm sorry, I don't want to waste your time any more.
Do you know a general reason that could cause a problem like the one i
posted (excluding the loss of sync on code compilation)?
Is it possible that a unauthorized (accidental) access to code memory
heap could modify argument information in the call stack?
Jan 9 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
16939
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
2
3206
by: PinkBishop | last post by:
Hello All, Hope you can help. Below is the code I use to send data from form to database. Problem is if an apostrophe is entered in cQuestion field or any field for that matter the form errors out. Please Help.
6
9257
by: Brian Ross | last post by:
Hi, I am trying to do something similar to the following: int Func1(int x, int y); double Func2(double x, double y); template <typename FuncT> // <- What would go here class CObjectT {
6
16259
by: wiredless | last post by:
What is the advantage of passing an interface type? according to UML (visio) when i reverse engineer existing code to a UML diagram I get the error "UMLE00046: sample : - An interface cannot be used as the type of a parameter." for this code:
21
3288
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string sFname, int w, int h) { FileStream fs = new FileStream(sFname, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); // Read data
20
5039
by: Brien King | last post by:
If I have a parameter that has an Object type (as opposed to something like a string), can I make that parameter a CONST? Right now, if you pass an object into a sub/function, that sub/function can modify the object no matter how it's defined (ByVal or ByRef). In some cases, I want to make sure that you cannot modify the object. Is there a way to do that in VS2003 or the up comming VS2005?
4
2752
by: Ranginald | last post by:
Hi, I'm having trouble passing a parameter from my default.aspx page to my default2.aspx page. I have values from a query in a list box and the goal is to pass the "catID" from default.aspx to a stored procedure on the details2.aspx page. I can successfully pass the values from the listbox control to a
2
7320
by: Doug | last post by:
On a server that we run automated apps on, that has VB code that uses the MSSoap tool kit to access a web service, we periodically get these errors: Soap error: XML Parser failed at linenumber 1, lineposition 0, reason is: No data is available for the requested resource. Soap error: Loading of the WSDL file failed. Soap error: One of the parameters supplied is invalid..
88
2684
by: Bill Cunningham | last post by:
Would anyone be interested in giving this a quick look. The compiler gave me so many errors I don't know where to start. This is my first use of isalpha. #include <stdio.h> #include <stdlib.h> #define ex exit(EXIT_FAILURE) int main(int argc, char *argv) {
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.