Click to See Complete Forum and Search --> : Simple C problem


McBalls
12-01-2002, 11:41 PM
Hi,

I've been trying to learn some C, and have a problem with a pointer here:

#include <stdio.h>

int main() {

int * pAge = NULL;
*pAge = 5;

return 0;

}

Thanks!
mcballs

I can't get this simple assignment (which I've got directely from a C++ for linux book) to work. It gives me the ol' Segmentation Fault.

Any ideas??

khankell
12-01-2002, 11:50 PM
Well, assuming that the syntax error in the pointer assignment is not intentional, try assigning the value like this -> pAge = 5;

It's been a while since I've written anything in C, but I think I remember that the asterisk is unecessary after the initial assignment.

UnderDog138
12-01-2002, 11:57 PM
The reason is, when you initialize and assign pAge, it seg faults because you're pointing it to NULL instead of to the address of another variable.

This might be what you're looking for. It initializes a integer variable, age, then an integer pointer, pAge, and points it to the address of age, then prints it out to the screen.

If that's not what you're trying to do, well, at least I tried. :)


#include <stdio.h>

int main() {

int age = 5;
int *pAge = &age;

printf("%d", *pAge);

return 0;
}


Compile that with gcc and it works nicely.

PhatBarren
12-02-2002, 12:14 AM
You can also do as follows, if you don't want to initialize a 'real' variable just to get it's address:

int *pAge = (int *) malloc(sizeof(int));
*pAge = 5;

You must also include the stdlib.h header to do this.

check out http://www.unet.univie.ac.at/aix/libs/basetrf1/malloc.htm
for info on using malloc()

UnderDog138
12-02-2002, 01:01 AM
That seems more like what he originally wanted to do. It skips the integer variable and initializes an integer pointer, points it to an allocated place in memory the size of an integer variable, then assigns an integer to that place in memory (in case he wanted to know what that does, since I doubt he's played with malloc() yet).

Nice one.

Rüpel
12-02-2002, 03:41 AM
dont forget to call

free(pAge);

at the end of the program. if you forget this, you have nice little memory leak! every malloc()-call needs its corresponding free()-call!

UnderDog138
12-02-2002, 04:32 AM
I think MordorSoft should take your advice when coding their subsequent and buggy Winblows kernels...

Spawn913
12-02-2002, 05:39 AM
#include <stdio.h>

int main() {

int * pAge = NULL;
*pAge = 5;

return 0;

}


Just a note... if you remove the assignment to NULL, i.e.


#include <stdio.h>

int main() {

int * pAge;
*pAge = 5;

return 0;

}


This would compile and run correctly. The reason your first code didn't work is because you previously assigned the pointer to NULL.

Dereferencing the pointer via *pAge translates to:

"the contents of address NULL",

which doesn't have any meaning, since NULL is not really a memory location. Thus, if you don't previously point pAge to Null, this should work.

One danger to this is that you may inadvertently alter a memory location that is used by some other running program, since pAge contains a random value at the start. The safest way is still via malloc(), or by assigning a valid address to pAge (via pAge=&<some variable>).

binaryDigit
12-02-2002, 10:36 AM
Originally posted by Spawn913


#include <stdio.h>

int main() {

int * pAge;
*pAge = 5;

return 0;

}



you might as well not even make pAge a pointer in this case. doesn't make much sense IMHO.

int age;
age = 5;

works just as well and is cleaner code.


#include <stdio.h>

int
main(void) {

int *p;
int p0;

*p = 7;
p0 = 7;

printf("*p = %d\np = %hx\np0 = %d\n&p0 = %hx\n", *p, p, p0, &p0);

return 0;

}


Output:

*p = 7
p = a2d0
p0 = 7
&p0 = f980

McBalls
12-02-2002, 11:07 AM
Wow, thanks for all the tips!

Just one thing though! If I don't use malloc, but rather just use the following:

#include <stdlib.h>;
#inclued <stdio.h>;

int main() {

int * pAge;
*pAge = 7;

return 0;
}


Will I still need to use free(pAge) to avoid memory leak?

mcballs

binaryDigit
12-02-2002, 11:36 AM
no. if you don't use malloc you don't need to use free.

Stuka
12-02-2002, 01:35 PM
Originally posted by McBalls

#include <stdlib.h>;
#inclued <stdio.h>;

int main() {

int * pAge;
*pAge = 7;

return 0;
}

DON'T DO THIS
It may work (in fact it probably will, in this tiny example). However, what happens in general is that code like this will (and should) segfault. The contents of pAge at the point where you dereference it are COMPLETELY ARBITRARY. If you want to use a pointer variable, initialize it first! In fact, when declaring pointers, assigning them to NULL is a Good Idea (TM), unless you know when you declare them what you want 'em to point to. Doing that will force you to initialize the pointer before use, guaranteeing that you don't go stack-smashing.

kmfan
12-03-2002, 12:13 AM
#include <stdlib.h>;
#inclued <stdio.h>;

int main() {

int * pAge;
*pAge = 7;

return 0;
}

save yourself some keystrokes:

int *pAge = 7;

will work just the same

good luck

Rüpel
12-03-2002, 03:29 AM
read stukas post again! don't use uninitialized pointers! you mess up your system! probably you write into memory-locations that belong to other programs.

always, always, always use malloc and free!