PDA

View Full Version : Please, teach me the memory address junk!


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)

OneSadCookie
2006.08.17, 12:23 AM
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int *x;
} SuperX;

SuperX MakeBigX(int number) {
SuperX temp;

temp.x = (int*)malloc(4);

// The cast here tells you you're doing something horribly wrong
// I think you mean *(temp.x) = number.
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);

// myX.x is a pointer, you can't print it with %i. The compiler should be yelling.
// if it's not, you need to crank up your warnings.
// http://tips.onesadcookie.net/tips/published/GCC+Warning+Flags
// you probably meant printf("%i\n", *(myX.x));
printf("%i", myX.x);

FreeBigX(&myX);

// same issue as above, but if you change to printf("%i\n", *(myX.x)); then
// you're dereferencing a pointer that has been freed, which will crash.
printf("%i", myX.x);

return 0;
}

Jones
2006.08.17, 12:29 AM
THANK YOU! :wow:

I think it works. (Except that I still see the second print out even after the free... Meh, random freak coincidence.)

In relation to your comments...

I don't use C as much as C++, so I'm used to the simpler command line i/o stuff. But it did work without the *( )'s.

Thanks for the link!

I don't get the malloc error anymore. :)

Your help has been appreciated.

arekkusu
2006.08.17, 12:32 AM
Learn an assembly language.