0

Hi

I was goinf through the GTK code and saw that GTK code uses pointer for structs and not normal instances of the same structs...

why could this be so?
My take is that they wanted to use just one variable name and they can easily initialise the pointer to that struct type .

whereas if they used just an instance of the struct, they wd have to create a new instance evry time they needed a new variable ...


am i on the right direction?

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by ~s.o.s~
0

I haven't looked at any GTK code. But the main reason for using a pointer maybe that the structure is too large or a lot of instances of that structure is created. By using a pointer, you can create the instance in the heap memory, rather than the limited stack memory. Also, when you pass data around, passing only the pointer saves memory rather than passing the whole variable.

0

And.. thats why most of the new languages make the pointer mechanism transparent to the user by allowing the user to ONLY pass reference variables to the functions or let the user create only reference variables which occupy on the free store rather than on the limited space of stack. Allocating the space for the var created on the stack is a pracitce generally avoided nowadays.

Examples include Java and so on where the object being passed around or a new instance variable created is a reference variable which has to be allocated space using the class constructor.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.