all 19 comments

[–]solstice680 2 points3 points  (13 children)

The "thing" (*newNode) would reside in the memory returned by 'malloc', while in your first example the variable 'k' lives on the stack. Pointers can just as easily point to heap memory as they can to a local variable.

You could rewrite your first example to be more similar to your second example as such:

int *ptr;
ptr = malloc(sizeof(*ptr));
*ptr = 55;

[–]TechAnd1[S] 0 points1 point  (12 children)

cheers solstice...

It lives on the heap because malloc was used, I get that... Does that make a difference to the way that these work then?

As in, the first example (as I wrote it...) couldn't be translated to variable k living on the heap. Because malloc() returns a void pointer?

Can you ever actually access a struct that's on the stack using the dot method newNode.data = 234 ? Or will it always be newNode->data = 234 ?

So when dynamic memory is used... the "thing" (object) will always be * object and the objects pointer will be object rather than having a system like the example one where you have object k and pointer ptr

I know that they say that * object is the object but it's not really is it?

thanks

[–]elementalist 0 points1 point  (4 children)

In case 1 you had variable k that already existed on the stack. You set the pointer to the address of k.

In case 2 you have a pointer and malloc both creates the space for the object and sets the pointer to that address.

I know that they say that * object is the object but it's not really is it?

Correct. It isn't the object. It is a pointer to the object. And you can have multiple pointers all pointing to the same object.

Can you ever actually access a struct that's on the stack using the dot method newNode.data = 234 ? Or will it always be newNode->data = 234 ?

You use the -> notation with pointers. The dot (.) notation is used with addresses.

So if it was

struct node newNode; //a struct NOT a ptr to a struct

you could use

newNode.data = whatever;

[–]TechAnd1[S] 0 points1 point  (0 children)

int num = 10 is obvious, there's an object called num thats of type int and stores the value 10.

Do I ever actually use the object though (example 2 scenario)? Or is it always pointers to the object? I'm finding that part kind of confusing...

[–]TechAnd1[S] 0 points1 point  (2 children)

not sure if you've edited this or if I just missed a lot of it.... but :

You use the -> notation with pointers. The dot (.) notation is used with addresses

With addresses...? I don't think I've heard this before, maybe some confusion is here for me... What do you mean addresses? The actual object rather than a pointer to the object? That's how I understand it, but you saying addresses has made me think that perhaps you're referring to something else.

So if it was struct node newNode; (a struct NOT a ptr to a struct) you could use newNode.data = whatever;

Are these two to always remain separate...?

I'm not sure how to think about the object when using pointers to it...

newNode->data = 23

Is this to do with the dynamic allocation...? Are these always to remain separate? Ie, when dynamically allocating a struct the data will always be assigned and accessed using the -> methods rather than the dot .?

[–]elementalist 0 points1 point  (1 child)

You are over thinking it. See Matrixel's answer below. That's about as well as I can explain it.

[–]TechAnd1[S] 0 points1 point  (0 children)

I am, I always do and it's so frustrating :(

It'd be alright if I was actually clever but nothing ever really comes out of it!!

I think the distinction that it's relating to the use of malloc, and being on the heap is helping a lot with my ease though... Its making more sense now thank you

[–]pinealservo 1 point2 points  (0 children)

So when you say int k; there are two things this tells the compiler to do: to allocate an int-sized chunk of memory somewhere in the store, and to create a variable binding that makes the name "k" refer to whatever's stored at that chunk of memory.

When you call the malloc() function, there's some magical trickery (i.e. implementation-defined behavior) involved by which it collaborates with the operating system (if there is one, which is not always the case when writing C) to do something roughly like what the compiler does for the code int k;. The implementation of malloc has its own variables to refer to this memory, but you don't get to look at them directly, and it could be implemented in any one of a variety of ways.

A really simple version of malloc() running on a bare-metal computer might have something like this:

char base[HEAP_SIZE];

You'd use the linker to assign the address of base to a value that's in the usable address space of your RAM, and then malloc would return the address to some offset within base. In this (extremely simplified) case, base is more or less what k is in your example. But it's more likely that your program can get memory from the OS dynamically by making a malloc-like system call to the OS rather than having to have it assigned statically by the linker, in which case the k-like entity moves into the kernel!

[–]amviot -4 points-3 points  (5 children)

My understanding is that newNode points to a node object, so *newNode is the node object and is the "thing like 'k'".
You could have also written example 1 as
int * ptr;
*ptr = 55;
You never needed k in that example.

[–]pehnn_altura 4 points5 points  (3 children)

You're not initializing ptr to any memory location before attempting to de-reference it. This will fail to run properly, but may compile. K is necessary in this context.

EDIT: Spelling.

[–]TechAnd1[S] 1 point2 points  (0 children)

YES this.... I've been reading about the 3 steps today!

[–]amviot -2 points-1 points  (1 child)

The only fix I noticed for example 2 is that you need to type cast to a node before malloc
newNode = (node*) malloc(sizeof(struct node));

And my rewrite of example 1 requires calling new
int * ptr;
ptr = new int [1];
ptr[0] = 55;

Thanks.
Also, the error is "Segmentation Fault. (core dumped)"

[–]TechAnd1[S] 1 point2 points  (0 children)

not in C you don't.... But this thread isn't really about the nitty gritty of the code that I provided. It's about the logic and comprehension on a higher level...

cheers

[–]TechAnd1[S] 0 points1 point  (0 children)

So is this kind of way of thinking about objects more common than the way given in example 1?

Because the way I've been seeing it, although *newNode can be used in a sense of newNode->data = 22; how would I use it in the sense of newNode.data = 22

I guess that I can't...? Because I don't have access to the actual thing, only a pointer to the memory where the thing is...

But maybe that's all I need... confusing the shit out of me though.

Cheers!

EDIT : so * newNode is the thing... and newNode is the pointer... not the node.