initialize null pointer

Jan 18, 2011 at 6:17pm
the following code doesnt work

1
2
3
4
5
6
7
8
9
10
11
12
void alpha(int *p){
*p=5;
}
int main (void)
{
int *p;
alpha(p);
cout<<*p;

//cout<<"3";
getchar();
}


but when i write

1
2
3
4
5
6
7
8
int main (void)
{
int *p;

cout<<p;

getchar();
}


i get a value CCCCCCCC
so pointer has a value
Jan 18, 2011 at 6:19pm
When you write int *p it creates a pointer pointing to somewhere (you have no idea where, and it is probably not valid). Thus, it has a value, it just is pointing to memory that isn't yours most likely.
Jan 18, 2011 at 6:29pm
He is obviously using a microsoft product.
values like CCCCCCCC and CDCDCDCD are used by MSVC to signify an 'unitialised' variable.
Jan 18, 2011 at 6:36pm

This is your initial code, fixed. It does work; it outputs "5".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>


void alpha(int *p){
*p=5;
}
int main (void)
{
int *p;
alpha(p);
std::cout<<*p;

int temp;
std::cin >> temp;
}
Jan 18, 2011 at 6:42pm
Erm, that code is a segfault waiting to happen, you realize?
Jan 18, 2011 at 6:52pm
Yup. It does, however, run, and it would seem I don't keep anything important in the vicinity of.... 0x7fffa6e090c0 :)
Jan 18, 2011 at 6:53pm
yes i run visual studio
ok thanks
so i have to initialize pointers..
Jan 18, 2011 at 6:55pm
so i have to initialize pointers..


It's generally considered good practice to initialise them to NULL, if you don't set them on creation to point at some memory belonging to you.
Jan 18, 2011 at 7:41pm
Yup. It does, however, run, and it would seem I don't keep anything important in the vicinity of.... 0x7fffa6e090c0 :)


Yes, but what happens if that memory *is* being used by something?
Jan 18, 2011 at 7:43pm
If I'm lucky, it'll segfault. If I'm unlucky, I'll trash something and won't find out until much, much later, and debugging it will be horrific. Once I had a memset trashing the stack, but the variable it trashed was held in a register thanks to optimisation, so it didn't actually cause a problem until something else went into that register and it had to be read back again from the stack; of course, this looked like the variable was being trashed in the stack simply by the act of reading a different variable entirely.

It was both miserable, and brilliant.
Last edited on Jan 18, 2011 at 7:46pm
Jan 18, 2011 at 8:00pm
Um... no, you won't "trash something" not on a modern OS. The address you are seeing is relative to the memory space allocated to that instance of that program on the GDT\LDT, and it usually stays inside of it's own page unless you specifically tell it AND allow it to use interporcess communication methods (or you are doing something boneheaded like using an unchecked JMP command).

firedraco was talking about a local segfault where your program overwrites it's own file handle or some other piece of data it needs to exist.

Didn't your classes cover Memory Pages? I'm mostly self taught with the exception of the stuff I learn from people here. But I've run across this stuff a couple of times in my reading.

EDIT: When I say JMP command I'm refering to a Long Jump. They're interesting little things...
Last edited on Jan 18, 2011 at 8:01pm
Jan 18, 2011 at 8:30pm
Thanks for the words, Computer Geek. At risk of sounding harsh, what exactly do you think the difference between "trashing something" and "overwrites its own file handle or some other piece of data" is? Here's a clue; it starts with "n", and ends with "othing".

As for the memset piece, I described the real events of actual experience of seeing exactly what I described. We had to drop to the assembly to work out what was going on, which is how I found the register optimisation that explained the whole thing.

I shall of course disregard the actual evidence of my own experience, though, in favour of your self-taught theory. My classes restricted themselves to physics, as that's what I read at university. All my programming knowledge comes from miserable experience.

Whilst we're on the subject of classes, you should go back to basic English; "it's" means "it is".

Hopefully that's enough sniping at each other, but if you want to keep going, shall we take it to PM? I won't be reading them, but that way we won't force everyone else to see our childish squabbling.
Last edited on Jan 18, 2011 at 8:36pm
Jan 18, 2011 at 8:35pm
You see to me, "Trash Something" implies something important, something that people would notice. No one cares if your command line app failes, but people would notice if it shifts the MBR around because that litteral address overran the pagefile and went into the boot loader.

EDIT: My origional text implied that I knew what that address resolves to, I have no idea so I changed my wording.

REEDIT: The event you were describing is hardley something new. In fact this is the kind of thing that stack tracing was invented for.
Last edited on Jan 18, 2011 at 8:39pm
Jan 18, 2011 at 8:40pm
closed account (z05DSL3A)
All my programming knowledge comes from a decade plus of miserable experience.

and what, you want to pass on that miserable experience rather than helping people avoid it.
Topic archived. No new replies allowed.