Archived

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

Returning char* from a function. Strange.

This topic is 5236 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
Re-read the section on pass-by-value versus pass-by-reference in your C++ manual.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
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