Problem with a C program
Hello!
I understand that this question is probably for another topic or thread but I have this little problem trying to understand memory allocation in C. I want to allocate memory from inside a function A() and use this memory from another function B(). The whole process should be controlled from main... To help you understand on what I mean, I wrote this simple program. But after a successful compilation It gives me many memory allocation errors when run. And memory segmentation errors if I move the definition struct apoint *p outside the main(). How can I correct this to have a successful run? Please someone with great experience in C could help me? Regards Vas Code:
#include <stdio.h> |
tried your code in windows and it crashed, dont know why.
|
Quote:
But I don't know what causes it ... In linux also crashes ... Unfortunately all malloc() examples I have seen so far use it inside main(). But the application I write ( a small database) Is very big to fit in main() so I have to split it in functions ... Any Ideas? Vas |
Hi -
I'm a huge fan of C ... but frankly, this kind of design cries out for a C++ class (instead of a C struct with a C malloc in a C "initial()" function). Anyway: Code:
#include <stdio.h> |
got it:
Code:
#include <stdio.h> so you get the real thing not a copy of it |
Thank you guys!
I'm very obliged to you Vas |
Quote:
In C you can get the semantic equivalent of pass by reference (using uglier syntax) with the method you showed: Add an extra level of indirection. But in this example in C, that obscures the function and there was a simpler solution. The apoint* is conceptually the return value of the allocate function. There are many times in C where it is appropriate to implement a conceptual return value by passing a pointer in rather than passing the value out. But in simple cases like this one, a conceptual return value is best implemented as an actual return value. Instead of Code:
void allocate (struct apoint **point_pp); Code:
struct apoint *allocate (void); Code:
struct apoint *allocate (void) { Code:
allocate (&point1_p); Code:
point1_p = allocate(); Quote:
The extra level of indirection was needed for allocate() when a value that is effectively an output from allocate() was declared as an input. If you have a good reason to pass a function output as an input to that function (there are often such good reasons) then the way to deal with that in C is to add a level of indirection. But the apoint* is just an input to printpnt(). Adding an extra level of indirection there just confuses things. |
Quote:
|
Quote:
I posted a correction to your code mainly to keep the OP from being confused by your suggestion. But maybe you can also learn something here. |
Quote:
I prefer smeezekitty's solution. the struct apoint* allocate() solution applies in this simple example but not in the application I write, where functions have to return multiple pointer values... Vas |
Quote:
Quote:
I still think it is better to use straight forward code in the simple case and save the more complicated solutions for more complicated problems. But if you want to practice the complicated solution on a simple problem, that's your choice. I hope you understand that there is an additional flaw in Smeezkitty's expansion of Paulsm4's solution. If a value is just an input, not one of multiple return values, not even a single return value, adding the extra layer of indirection just adds overhead and confusion. |
Hi, vasmakk -
Gentle suggestion - when you hear people babbling subjective nonsense about "straight forward code" and "uglier syntax" - run!:) But to go back to your original question: Quote:
Quote:
Q: Why does passing "**p" as an argument (my reply) do the same thing as returning "*p" as a "return" value (johnsfine's suggestion)? Q: What would happen if you wanted to allocate memory for TWO (or three, or more) structs, instead of just one? I think the main point is to understand "pointer", "address", and "pointer to pointer". And again, if this were C++ ... then your "struct" would be a "class", your "allocate()" would be a "constructor" ... and we probably wouldn't even be having this conversation :) Anyway - I hope this discussion was useful for you. IMHO .. PSM |
All times are GMT -5. The time now is 04:17 AM. |