Jones
2006.08.17, 12:08 AM
I like OOP classes. You know why? Because, although I have not admitted it until now, I'm incapable of writing Free/Flush/Clear/D'alloc functions for anything else. Why? Because I have only an imaginary grasp of how these address of/pointer of things really work. What do I mean?
I was so sure that this was perfectly valid code (for example):
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *x;
} SuperX;
SuperX MakeBigX(int number) {
SuperX temp;
temp.x = (int*)malloc(4);
temp.x = (int*)number;
return(temp);
}
void FreeBigX(SuperX *target) {
free(target->x);
}
int main (int argc, const char * argv[]) {
SuperX myX;
myX = MakeBigX(12);
printf("%i", myX.x);
FreeBigX(&myX);
printf("%i", myX.x);
return 0;
}
And then I hit compile and I get slapped in the face. It's like... "Yeah, that knowledge you thought you have... you don't really have it!"
I thought for the longest time that modifying an argument actually modified the variable the user passed, until one day I was told I needed to use the address of operator. So I did... but wrong. (Big surprise there.)
A quick look at the gpdirect C reference and a C dictionary only told me what I already knew, that "&" was the address of operator, and was equal to a byte address (in memory) of the item in question.
Before I plod on any further, making broken useless functions that don't actually de-allocate anything, but rather leave memory leaks behind, I need to stop and ask somebody(s) what I'm doing wrong.
It happens to be your misfortune that you are those special somebody(s) I'm going to ask. :lol: ;)
Thanks. (I know, I realized this a little too late... :p)
I was so sure that this was perfectly valid code (for example):
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *x;
} SuperX;
SuperX MakeBigX(int number) {
SuperX temp;
temp.x = (int*)malloc(4);
temp.x = (int*)number;
return(temp);
}
void FreeBigX(SuperX *target) {
free(target->x);
}
int main (int argc, const char * argv[]) {
SuperX myX;
myX = MakeBigX(12);
printf("%i", myX.x);
FreeBigX(&myX);
printf("%i", myX.x);
return 0;
}
And then I hit compile and I get slapped in the face. It's like... "Yeah, that knowledge you thought you have... you don't really have it!"
I thought for the longest time that modifying an argument actually modified the variable the user passed, until one day I was told I needed to use the address of operator. So I did... but wrong. (Big surprise there.)
A quick look at the gpdirect C reference and a C dictionary only told me what I already knew, that "&" was the address of operator, and was equal to a byte address (in memory) of the item in question.
Before I plod on any further, making broken useless functions that don't actually de-allocate anything, but rather leave memory leaks behind, I need to stop and ask somebody(s) what I'm doing wrong.
It happens to be your misfortune that you are those special somebody(s) I'm going to ask. :lol: ;)
Thanks. (I know, I realized this a little too late... :p)