Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BlackSheep

Returning char* from a function. Strange.

This topic is 5454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

graphics->GetRendererInfo(output_string);
log->WriteNoEcho("Renderer : %s",output_string);
...

void CRenderer::GetRendererInfo(char *str)
{
	str = (char*)glGetString(GL_VENDOR);
-x-
}
Please help me figure out why this crashes. The compiler likes it, but when I go to run it, things go a bit icky. If I check the contents of ''str'' at point ''-x-'', I get ''ATI Technologies, INC.'' as expected, but it crashes in the loh-> line above. My basic string handling is very poor, and I''m still trial-and-error-ing my way through pointers, so I''m sure that''s the problem. None of my books have exactly the same problem in, and this code is identical to some I found doing a forum search through these pages.

Share this post


Link to post
Share on other sites
Advertisement
It is crashing because output_string is passed BY VALUE to the GetRendererInfo() function. That means a copy of the output_string pointer value is sent to the function. In GetRendererInfo() you are changing the value of the LOCAL COPY of output_string (str) to be equal to the pointer returned by glGetString, but this does not modify the original pointer (output_string).

A better method would be to pass a pointer to output_string (char **str) to GetRendererInfo(), and change the line to *str=(char *)glGetString(GL_VENDOR).


void CRenderer::GetRendererInfo(char **str)
{
*str=(char *)glGetString(GL_VENDOR);
}

graphics->GetRendererInfo(&output_string);
log->WriteNoEcho("Renderer : %s", output_string);


Hope this helps


Josh
vertexnormal AT linuxmail DOT org


Check out Golem at:
My cheapass website

Share this post


Link to post
Share on other sites
Josh, you sir, are my new best friend! Have a cookie!

It works perfectly now, thank you. I was screwed up on the ''copy of the pointer'' bit. I thought I was going in the right direction by considering ''&str'', but couldn''t get it right.

Happy now! Thanks again!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!