DEV Community

Discussion on: Double Pointers in C/C++

Collapse
 
amexboy profile image
Amanu • Edited

Couldn't getline have returned a struct? Why is an output parameter necessary to begin with?

I think it's obvious I'm not a C guy, get over it ;-)

Collapse
 
noah11012 profile image
Noah11012

Maybe, but the POSIX people decided to use an output parameter.

Collapse
 
amexboy profile image
Amanu

That's not a maybe ;-)

I was asking about the design decision itself.

Thread Thread
 
noah11012 profile image
Noah11012

The return value of getline() is the number of characters read. Because this return value is already in use, they decided an output parameter for the contents would be sufficient.

Thread Thread
 
amexboy profile image
Amanu • Edited

A comment above made sense. It was in use doesn't justify the design.

A struct is several bytes long, and you'd need to add in the cost of getline's return value as well. These values have to be pushed onto the stack, and then pulled back off and copied again into the receiving struct. That costs valuable processor cycles, but also it's a hidden cost that's not intuitive from reading the code, and therefore goes against C's design.

Collapse
 
dwd profile image
Dave Cridland

A struct is several bytes long, and you'd need to add in the cost of getline's return value as well. These values have to be pushed onto the stack, and then pulled back off and copied again into the receiving struct. That costs valuable processor cycles, but also it's a hidden cost that's not intuitive from reading the code, and therefore goes against C's design.

What might have been better is you have passed in a pointer to a struct, which would have involved fewer pushes and pops to stack - though with modern calling conventions the function arguments and return are in registers anyway, I think.

Collapse
 
amexboy profile image
Amanu

The cost is not intuitive, I see.