473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Ever see anything like this?

I'm working on some legacy code and came across something like this.
Anybody know exactly what the ramifications are to the memory if you do
something like this:

int somefunction (some arguments)
{
....

Mystructtype mystruct;

....

otherfunc(&mystruct); /* this one appears to load up the struct*/
....

free(&mystruct);

func3((void *}&mystruct, sizeof(Mystructtype)); /* func3 takes a void
** and mallocs memory

to set the pointer to point to! */

free(&mystruct);

....
} /* end somefunction */

Now this obviously is really bad, but just what happens? It seems to me
that there is a memory overwrite at the very least or possibly a memory
leak that only lasts as long as the function since the mystruct memory
is automatic anyway.

What would you do about it?

Feb 21 '06 #1
4 1171


gb******@excite.com wrote On 02/21/06 15:50,:
I'm working on some legacy code and came across something like this.
Anybody know exactly what the ramifications are to the memory if you do
something like this:

int somefunction (some arguments)
{
...

Mystructtype mystruct;

...

otherfunc(&mystruct); /* this one appears to load up the struct*/
...

free(&mystruct);
Right here, the program goes off the rails. The
memory for mystruct was not obtained from malloc() et
al., so attempting to release it with free() produces
undefined behavior.

It is pointless to ask "exactly what the ramifications
are" from the point of view of the C language: the language
says only that the consequences are undefined. As far as
the language is concerned, the program can do anything at
all; it is an outlaw in the sense that the laws no longer
apply to it.

The question may be answerable from the point of view
of a particular C implementation, but not in terms of
the language itself.
[...]
What would you do about it?


First, double- and triple-check that the code really,
truly does what your paraphrase says. Pay close attention
to indirection levels; note that the superficially similar

typedef struct s Mystructtype;
typedef Mystructptrtype *Mystructtype;

Mystructptrtype mystruct;
create (&mystruct);
...
destroy (&mystruct);

might be perfectly legitimate, given something like

void create (Mystructptrtype *ptr) {
*ptr = malloc(sizeof *ptr);
...
}

void destroy (Mystructptrtype *ptr) {
...
free (*ptr);
}

Examine the typedefs with particular care; when typedef is
used to create aliases for pointer types, the fact that
you're dealing with a multiple indirection can be less than
obvious. If it turns out that the code is in fact correct
but that the typedef names are muddying the waters, I'd
suggest getting rid of pointer typedefs and using just the
"base" types instead. Keep the typedef aliases for the
structs themselves, if you like (some people don't), but
get rid of the pointer typedefs based on them.

If it turns out that the code actually does what your
paraphrase says, you've got a problem: you need to read the
minds of the people who wrote the code and try to figure
out what in the world they were trying to accomplish. This
will require some ESP on your part, because the C doesn't
reveal their intentions -- as we've seen, the C as written
has no meaning and is therefore uninformative. Once you've
figured out what they were trying to do, rewrite the code so
it does that thing in some legitimate way.

Good luck!

--
Er*********@sun.com

Feb 21 '06 #2
gb******@excite.com wrote:
I'm working on some legacy code and came across something like this.
Anybody know exactly what the ramifications are to the memory if you do
something like this:

int somefunction (some arguments)
{
....

Mystructtype mystruct; Mystructtype * ptrmystruct;
....

otherfunc(ptrmystruct); /* this one appears to load up the struct*/
....

free(&mystruct); /* ==> segmentation fault... you can free variables mallocated (or callocated..) only as long as i know */
free (ptrmystruct);
func3((void *}&mystruct, sizeof(Mystructtype)); /* func3 takes a void
** and mallocs memory func3((void *}&ptrmystruct, sizeof(Mystructtype));
to set the pointer to point to! */

free(&mystruct); free (ptrmystruct);
....
} /* end somefunction */

Now this obviously is really bad, but just what happens? It seems to me
that there is a memory overwrite at the very least or possibly a memory
leak that only lasts as long as the function since the mystruct memory
is automatic anyway.

What would you do about it?


Xavier
Feb 21 '06 #3
gb******@excite.com writes:
I'm working on some legacy code and came across something like this.
Anybody know exactly what the ramifications are to the memory if you do
something like this:

int somefunction (some arguments)
{
...

Mystructtype mystruct;

...

otherfunc(&mystruct); /* this one appears to load up the struct*/
...

free(&mystruct);

func3((void *}&mystruct, sizeof(Mystructtype)); /* func3 takes a void
** and mallocs memory

to set the pointer to point to! */

free(&mystruct);

...
} /* end somefunction */

Now this obviously is really bad, but just what happens? It seems to me
that there is a memory overwrite at the very least or possibly a memory
leak that only lasts as long as the function since the mystruct memory
is automatic anyway.

What would you do about it?


The first thing I'd do is look at the actual code rather than a
paraphrase of it. Since you re-typed a summary of the code rather
than copy-and-pasting an actual copy of it, it's impossible to be sure
what the original code actually looks like. I'm sure it doesn't
literally have the lines
int somefunction (some arguments)
and
func3((void *}&mystruct, sizeof(Mystructtype));
both of which are syntax errors.

*If* the code actually contains
free(&mystruct);
where mystruct is a declared object, it invokes undefined behavior.

C99 7.20.3.2p2:

The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If
ptr is a null pointer, no action occurs. Otherwise, if the
argument does not match a pointer earlier returned by the calloc,
malloc, or realloc function, or if the space has been deallocated
by a call to free or realloc, the behavior is undefined.

Undefined behavior means that anything can happen, including no
visible bad effects.

What I would do about it is fix it, if practical.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #4

serrand wrote:
gb******@excite.com wrote:
I'm working on some legacy code and came across something like this.
Anybody know exactly what the ramifications are to the memory if you do
something like this:

int somefunction (some arguments)
{
....

Mystructtype mystruct; Mystructtype * ptrmystruct;

....

otherfunc(ptrmystruct); /* this one appears to load up the struct*/
....

free(&mystruct);

/* ==> segmentation fault... you can free variables mallocated (or callocated..) only as long as i know */
free (ptrmystruct);

Actually it doesn't cause a segmentation fault. As others pointed out,
this results in undefined behavior and on the machine it's running on
it appears to do absolutely nothing. I did write a correction very
similar to the one that you came up with (before I saw your post).

func3((void *}&mystruct, sizeof(Mystructtype)); /* func3 takes a void
** and mallocs memory


I believe that the code that I'm looking at causes a memory leak
because instead of passing a (void) pointer to pointer, it passes (void
*)&mystruct. When this happens, malloc is looking at the pointer to the
structure and returns the pointer value of the memory it allocated into
the first bytes of the structure's memory. The rest of the program uses
the memory declared in the program, not the memory that was allocated
and since it's address has been written into the structure and
subsequently overwritten, the result is a memory leak.

So the code works, but I believe it causes a memory leak.
func3((void *}&ptrmystruct, sizeof(Mystructtype));

to set the pointer to point to! */

free(&mystruct);

free (ptrmystruct);

....
} /* end somefunction */

Now this obviously is really bad, but just what happens? It seems to me
that there is a memory overwrite at the very least or possibly a memory
leak that only lasts as long as the function since the mystruct memory
is automatic anyway.

What would you do about it?


Xavier


Feb 22 '06 #5

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

Similar topics

47
by: Nick Mudge | last post by:
Hey everybody, let's make the longest newsgroup thread ever made. Find the posting "Any girls?" in this newsgroup and add a posting. The name of the thread is a great name for a programming...
27
by: garyolsen | last post by:
In C++ what kind of unexpected conditions should be handled as exceptions? Besides dividing by 0, bad memory allocation, what're the most popular exceptions? When should not use exception,...
14
by: root | last post by:
Hi group, Apologies in advance if this has been asked somewhere before, but I haven't managed to get anything from the Google archives - I've been getting introductory guides to C++ all day...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
2
by: greatbooksclassics | last post by:
Open Source DRM? What does everyone think about it? Will Open Source DRM ever catch up to MS DRM? Will DRM ever be integrated into common LAMP applications?...
7
by: Lyle Fairfield | last post by:
I was just showing a youngster some MS-SQL stuff on a remote Internet enabled shared server. He logged in with my User Name and Password. I was busy for a minute. Then he said, "This (stored...
8
by: MLH | last post by:
A97 HELP shows the proper syntax for using Nz as Nz(variant) I'm wondering what to expect from potential past misuse I've made. For example, consider the following... Private Sub...
3
by: jhcorey | last post by:
I have a stored procedure that takes 14 parameters, including two for start date and end date. The procedure then creates a sql string and does an 'exec sql'. The stored procedure always works...
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
9
by: jaysome | last post by:
Anyone know what ever happened to Lawrence Kirby and Dan Pop and Kaz? They used to be some of the most influential posters here on c.l.c (among others who are still around). I sure miss their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.