This is a discussion on [C malloc/free] ~ free-ing the allocated memory gives c0000005 Windows error within the C Programming forums, part of the General Programming Boards category; Hi everybody!
I hope you all had a merry christmas... =)
....because I need your help!! >.<
I'm new to ...

I'm new to C and I'm getting to like this language more and more at every step I take, but I keep banging my head against several walls.

Today it's the "malloc/free" wall.

I've built an exercise to learn how to pass values and memory pointers to functions. It's more like a simple "test my knowledge" thing, actually, but still good enough to understand and exercise logic.

The exercise is very simple:
- given two "int" variables, pass their values to a function that returns their sum; then print it on the screen
- given a structure containing three "int" elements, allocate memory and pass its pointer to a function that calculates the product of the first two elements and saves it in the third one; then print it on the screen

No problems AT ALL with the first part. Happy!!

The second part, instead, works great until I use "free" to de-allocate memory.

For a matter of speed, I've written it partly with italian words (my language), but I'll translate the "printf" messages so that you know what's happening.

The error comes out randomly, not at every execution; clear sign that it's bound to memory addresses, as if the address I'm trying to free is no longer reserved and gets used by another application.

I've tried to analyze the whole thing excluding the most common errors like scopes, re-assignements and the likes.
As you can see I've even set an extra "printf" after the call for the external function to ensure the address inside the "oggetto1" variable is still the same as before.

I've already (made and) done another exercise that finds all the prime numbers between 3 and "n" (value given by user) and memorizes them in a "list" of structures; then frees all the allocated memory through the "free" function, but I get absolutely no errors, there. Argh!! >.<
The "good side" is that it finds all the prime numbers between 3 and 40.000.000 in about 14 seconds. (Intel i7 920 @ 4.00GHz)
The "bad side" is that it's written in a single block of code (awful!!), while I'd like to separate each routine in a different header file, but first I want to be sure I'm able to pass data safely between separate functions.

I guess this error has something to do with passing the pointer to another function, but I cant understand how.

Please, help!! =|

PS: about the address I "return" from the "pass_struct" function and that I assign to "oggetto1", I've also re-written it to return absolutely nothing and not assign it to "oggetto1", but the error still happens.
Just to be clear:
- the function call would be simply "struct_pass(oggetto1);"
- at the end of the "struct_pass" function there would be a simple "return 0;"

Actually, from the lists exercise, I remember using the "typedef" of the structure type itself, to set a "sizeof".

You could do that, or you could use sizeof(struct test), but sizeof(*oggetto1) is better since it would be correct even if the type of oggetto1 changes.

Originally Posted by Spiegel

I've also written full declarations, instead of those bare prototypes I used before, but what's the difference?
Why a full declaration, instead of a simplified one? What problems could it bring to?

If you make a mistake when calling the function by passing arguments of the wrong type or a wrong number of arguments, the compiler is likely to warn you if you declare with a prototype. But if you declare without specifying the number and type of the parameters, then there will likely be no such warning.

By the way, did you fix the problem with the return type of pass_struct?

If you make a mistake when calling the function by passing arguments of the wrong type or a wrong number of arguments, the compiler is likely to warn you if you declare with a prototype. But if you declare without specifying the number and type of the parameters, then there will likely be no such warning.

By the way, did you fix the problem with the return type of pass_struct?

You're welcome

Got the point with the full prototype of a function and sounds reasonable. Will always do.

Ehm. "Problem" with the return type of pass_struct?
No problem, just useless data passing for and back. ^^
Before it was:

the prototypes are there for what laserlight said. As well another usage is so that your compiler knows exactly how much space to allocate based on the types you give as arguments. If you don't want to give actual identifiers in your prototypes you can simply specify the types like

Code:

unsigned long secondary (unsigned long, unsigned long);

If you don't want to write the prototypes at all that's fine as well but you will have to include the full function definition BEFORE main() like so

also notice i changed the return type to pass_struct() and removed the return statement. Pointers allow you to go to the address of the actual 'object' and manipulate it so you don't need to use the return statment as you did. That's the whole point

As well another usage is so that your compiler knows exactly how much space to allocate based on the types you give as arguments.

I believe the compiler will know that either way when it reaches the function definition.

Originally Posted by caroundw5h

If you don't want to give actual identifiers in your prototypes you can simply specify the types like

Don't do that: the parameter names are useful to document the parameters. Granted, they are not necessarily the same names as those used in the function definition, but they otherwise provide useful context.

the prototypes are there for what laserlight said. As well another usage is so that your compiler knows exactly how much space to allocate based on the types you give as arguments. If you don't want to give actual identifiers in your prototypes you can simply specify the types like

Code:

unsigned long secondary (unsigned long, unsigned long);

If you don't want to write the prototypes at all that's fine as well but you will have to include the full function definition BEFORE main() like so

also notice i changed the return type to pass_struct() and removed the return statement. Pointers allow you to go to the address of the actual 'object' and manipulate it so you don't need to use the return statment as you did. That's the whole point

Yeah, I know I can just write down the whole function without any prototype, but I hate having full spans of code BEFORE main.
Anyway I think I'll follow laserlight's suggestion: all or nothing. =)

"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

i am surprised, the classes i have taken required up to calc before you were even allowed to start programming....

I'm not sure what you mean with "required up to calc", but I'm learning from a friend who is studying at the university.

I have a high-school diploma in accounting and programming, but the kind of languages and "situations" I've been taught in that school are ridiculous.
Thus, now I'm trying to learn something better, more powerful and more useful. I will lack real-world applications experience, but at least I'll understand and have chances to improve.
I'd LOVE to have a chance to improve through experience in a real work place, but this is not an economical situation where companies invest on inexperienced people and make them grow.

So here we are, with me asking silly questions and inventing myself some exercises to better understand things and to improve my capabilities. ^^

So here we are, with me asking silly questions and inventing myself some exercises to better understand things and to improve my capabilities.

it is good to keep reaching ahead - and it is good you want to give yourself problems that interest you, bear in mind do this with restraint in order to properly gain as you go along - we sometimes see people that clearly have zero programming skill asking

'how do i make this satellite controller for my new GPRS mapping program that i will make into an online multiplay game'

If anybody's still following this thread, I'd like to ask a question that is not important enough to deserve a new thread itself.

The question is: is it just the compiler I'm using, or it's an official C rule, that I cant use "_" for "typedef" names?

For instance: I had set a structure and then declared a typedef with that same structure to use it as a type of data more easily. The reason of this was that I had to declare several pointers of the same struct type, so I preferred to make it simpler by reducing the declaration to a neat "type * variable" instead of "struct str_name * variable".
The name given to this typedef was something like "abc_def" and I was getting an error about something like an "incomplete statement". Banged my head really hard for quite a while, before trying to change the typedef name by taking out the freaking "_". After that, everything worked great.

So, what is it? A rule I didnt know about or is it my compiler that's a bit picky?

It's just for the "typedef", I have no errors of sort from any other declaration. Even the structure itself had a "_" in the name.... =p

Thank you for dedicating me so much of your time. ^^

PS: I'm using "QT Creator" as my "pseudo-teacher" told me to, so that we could compare methods and GUI controls more easily.

[....] we sometimes see people that clearly have zero programming skill asking

'how do i make this satellite controller for my new GPRS mapping program that i will make into an online multiplay game'

haha, well nearly as bad as that anyway!

Yeah, I've seen that kind of questions myself. ^^
Honestly, I felt so frightened by the level of those questions, that I thought "I could never learn C/C++, if that's the average level for a programmer!!" XD