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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.