Archived

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

FXO

Parsing Pointers in C++

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
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
The only issue with malloc and calloc (and all low-level memory allocation functions) is that they do not call the default constructors for the objects being created. That''s not a problem with built-in types, of course. But with anything else, that might cause some serious problems, which is why new and delete were devised.

Share this post


Link to post
Share on other sites
btw. Brett, you wanted more links,..
So I thought I''ll send you some of my favourites:

Tomohide Kano has some great technical OpenGL demos at:
http://cgi3.tky.3web.ne.jp/~tkano/

More technical demos:
Objectecture! - http://www.r3.nu/

Great visability testing lib, with demos.
Check out the city demo - 16000 houses & 4000 cars, runs at about 8-16 fps on my machine (p2-400):

http://www.hybrid.fi/umbra/


Hope you all find something you enjoy

Share this post


Link to post
Share on other sites
1) malloc & calloc and free and such don''t necessarily play well with new and delete. Use one or the other - not both.

2) std::string is in #include (no .h)
MSDN should document their implementation of STL pretty well. Yes, its a string handler

3) You''re welcome to post links yourself, its automatic (I just have to tick them off). I''ll come back to these in a bit if you don''t get a chance. No time right now sorry

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

Share this post


Link to post
Share on other sites
You can use malloc (and related functions) and free in the same code that uses new and delete. If you make sure that delete is only used for blocks allocated by new and that free is only used for blocks allocated by malloc, everything is fine. Otherwise, you''ll run into some serious trouble.

So, I do not recommend doing this since it''s all to easy to "forget."

Share this post


Link to post
Share on other sites
merlin9x9 wrote:
The only issue with malloc and calloc (and all low-level memory allocation functions) is that they do not call the default constructors for the objects being created.

Thanks, I didn''t know that, perhaps its time to read up on my C++


Share this post


Link to post
Share on other sites
Merlin,

You are completely correct. However I never tell people what they can do, I tell them what they should do

Another interesting thing, relating to constructors and destructors: don''t delete a casted pointer.

Bad:

void *pObject = new Object(); // works!
delete pObject; // free''s Object, but doesn''t call destructor

instead:

delete (Object) pObject;


This is ok:
  
class B {
virtual ~B() { ... }
}

class A : public B {
virtual ~A() { ... }
}

B *pA = new A(); // success

delete pA; // will call ~B because it is virtual.



So always use virtual destructors (required if you have a virtual function).


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

Share this post


Link to post
Share on other sites
I upgraded to MSVC 6.0, and recompiled my code.
Now the program hangs with a page fault when I call
"strupr()" with static text - ie. strupr("text").

I always thought that when calling a function with a static value, the function would dynamicly create a local copy of the value, is this false?

btw. how do u get those fancy code-like-boxes in =?

/Thanks

Edited by - FXO on August 2, 2001 10:36:35 PM

Share this post


Link to post
Share on other sites
RTFM

strupr will convert the string you give and return a pointer to it. (that''s my impression).
So instead:
char str[256];
strcpy( str, "text" );
str = strupr( str );

But I can''t stress this enough: use std::string if you can. Learn and love STL it''ll save you time. it took me a long time to convince myself to use it, but no turning back. I''ve started converting PortaLib code too.

It''s not the quality of Java''s class libraries (the templates make it a bit more confusing), but it''s much easier than doing a lot of this stuf yourself.



The funny blocks: ubb tags. they are wrapped in [ ] and the good ones are : url, source, code. There are instructions somewhere on this site...

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

Share this post


Link to post
Share on other sites
quote:

strupr will convert the string you give and return a pointer to it. (that's my impression).



Thanks, that must be it.
I'm a little surprised that I't compiled and ran on VC 5.0 though.

I can't think of any other way it would work, except if the func would return a char[1024] or similar, but then the length of the string would be limited.



I appreciate the tip about STL, and I'd like to try it out,
but I can't find the file "no.h".

I've searched my "Vc98" dir for files containing "std::string",
perhaps my compiler doesn't support it.


Edited by - FXO on August 4, 2001 11:43:55 AM

Share this post


Link to post
Share on other sites
no.h? Not sure what that is. Never heard of it.

std::string is in . I'm pretty sure VC5 supports it, but I may be wrong.

Borland 5 and GCC both do, both of which are free. (They're implementation is a little different to Microsoft's though... but easy to get aronud in a portable way).

VC98... isn't that VC6? That definitely supports it.

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


Edited by - brettporter on August 5, 2001 4:54:16 AM

Share this post


Link to post
Share on other sites