Jump to content
  • Advertisement

Archived

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

FXO

Parsing Pointers in C++

This topic is 6191 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

Hey there! Perhaps this is not the right forum to post this topic, but I'll give it a try Anyway, Im having some trouble parsing a local pointer to memory to another function - when looking at the pointer from the other function it's empty (should be a string)...
    
//***************************

//        PSEUDOCODE:


void func1(void)
{
 char* string;

 string = malloc( 1024 );
 memcpy( string, "hello world\n", 12);

 func2( string );
 free( string );

}

void func2(char* string)
{
 printf("%s\n", string); // Prints ""

}
     
I can't figure out what's wrong, any help would be appreciated! /Thanks in advance! /Fredrik Olsson Edited by - FXO on August 4, 2001 11:27:36 AM Edited by - FXO on August 4, 2001 11:31:09 AM Edited by - FXO on August 4, 2001 11:38:59 AM

Share this post


Link to post
Share on other sites
Advertisement
You have to type cast the memory that you allocate for the string. I actually got a compiler error when I didn''t type case it. The malloc line should read:

string = (char *)malloc(1024);

However, you print this tring out you will still get a bunch of garbage because your string does not take up 1024 bytes. Its better to have somethine like sizeof("Hello World\n") to calculate the size. Then you don''t waste any memory.

Share this post


Link to post
Share on other sites
Thanks for your input.

Sorry about the poor description of my problem,
but the code was only "pseudocode" ie. not exact code.

My code compiles and runs ok, but the problem is that when the string-pointer is parsed to "func2()" it doesn''t contain the
appropriate string.

I''ve debuged it and looked at the parsed string.


/Fredrik Olsson

Share this post


Link to post
Share on other sites
That''s strange, I tried it and everything works fine. What compiler are you using?

>Thanks for your input.

>Sorry about the poor description of my problem,
>but the code was only "pseudocode" ie. not exact code.

>My code compiles and runs ok, but the problem is that when the >string-pointer is parsed to "func2()" it doesn''t contain the
>appropriate string.

>I''ve debuged it and looked at the parsed string.


>/Fredrik Olsson

Share this post


Link to post
Share on other sites
One problem I see:

quote:
memcpy( string, "hello world\n", 12);



This will copy "hello world\n" to the pointer string, but it needs to end in a NULL character (''\0''). What you should have used is strcpy(string, "hello world\n");

Besides casting the malloc to a (char *), the rest should work.

Nutts

Share this post


Link to post
Share on other sites
I''m using "MSVC++ 5.0 Enterprice Edition" (Dev Studio ''97).

I left out the code where I add a NULL char to the last position of the string...

Im starting to wonder if there is a problem with the compiler,
course I''ve got other strange errors that I''ve documented:

1)
- ERR_GEN -
Caused a pagefault when temp1[] was declared next to temp[].

Desc: When declaring temp1[] in _model::_LoadASCModel()
the program caused a page fault at first frame.

Due: MSVC++ might recognice the digit after the variable name as something else (?).

Reg: Declare "temp1[] = {f,f,f,f}" next to "temp[] = {f,f,f,f}" in r_model.cpp.

Fix: Don''t use name "temp1" for variablename.


2)
- ERR_DEB -
Caused a pagefault when debugging.

Desc: When running program no problem was issued, only when debugging
a pagefault would generate.

Due: R_Win32.cpp; CreateGLWindow(); wc.hCursor = LoadCursor(NULL, NULL); // Load The Arrow Pointer

Reg: Set "wc.hCursor" to NULL using "wc.hCursor = LoadCursor(NULL, NULL);" then debug the application.

Fix: Changed "wc.hCursor = LoadCursor(NULL, NULL)" to "wc.hCursor = LoadCursor(NULL, IDC_ARROW)"

/Fredrik Olsson

Share this post


Link to post
Share on other sites
There''s nothing wrong with using memcpy to copy a string, instead of using strcpy, but you do need to copy the null. You had 12 as your length, with a \n on the end. Either just change the \n to \0, or change the 12 to 13.

G''luck,
-Alamar

Share this post


Link to post
Share on other sites
If you are using C++, you should be using new and delete for one, and you should probably consider std::string in the STL. All decent compilers support it now.

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL

Share this post


Link to post
Share on other sites
Shouldn''t I be able to use malloc() & calloc() as well?

I haven''t heard of "std::string" is it a class for handling strings?

I''ve planned on implementing such a class myself,..

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!