Archived

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

xyz

strcat stopped working...

Recommended Posts

xyz    100
Hi, For some time now, I haven''t been able to use strcat, and when I debug the error, I get an ''Object reference not set to an instance of an object.'', inside strcat.asm:
main_loop:                          ; edx contains first dword of sorc string
        mov     [edi],edx           ; store one more dword (I GET THE ERROR IN THIS LINE)
        add     edi,4               ; kick dest pointer
main_loop_entrance:
Any help? Or... could you show me your strcat.asm to see if, by mistake, something was changed...? Thanks

Share this post


Link to post
Share on other sites
BriTeg    168
That code from strcat.asm is correct. I suspect it''s not strcat''s problem, but a mistake in how you''re calling it or what you''re passing to it. Post some of that code instead.

Share this post


Link to post
Share on other sites
xyz    100
I''m sure something happened but it is neither from my code nor from strcat.asm, if you say so, because I had this program compiled a week ago using strcat, I recompiled it again yesterday without changing a thing and using the same compiler, and I get the error... its weird!

Share this post


Link to post
Share on other sites
xyz    100
still get the same error... and made the simplest program possible:


char *path = "Hello";
cout << path << endl;
strcat(path, ", World");
cout << path << endl;



AHHHRRRGGHHH!

Share this post


Link to post
Share on other sites
BriTeg    168
Your "path" variable is just a pointer. By pointing it to "Hello", you're pointing it to memory that the compiler sets up as 6 bytes ("hello" plus a nul char) and who knows what the compiler put in memory after the "Hello". By strcatting to it, you're basically overwritting that memory, hammering memory you don't own, and thus bad things happen.

Change

char *path = "Hello";

to

char path[100] = "Hello";

or

char *path;
path = new char [100];
strcpy(path, "Hello");
// don't forget to delete [] path; after you're totally done with path

Try that. Should work.



[edited by - BriTeg on March 27, 2004 2:32:55 PM]

Share this post


Link to post
Share on other sites
Mastaba    761
I''d recommend you drop the C style strings altogether and use the string template class in the STL. This wouldn''t have even been a problem then.

Share this post


Link to post
Share on other sites
Or of course you could do:

char* hello = "Hello";
char* world = ", World";
char* printout = new char[strlen(hello) + strlen(world) + 1];
strcpy(printout, hello);
strcat(printout, world);
cout << printout << endl;
//As BriTag said, remember delete[]printout.

//You should NOT delete hello or world.


You have to create a variable array large enough to fit the strings + a NULL. You then have to copy the first section into the new array and THEN append the second section within that new array. As was said before, "something" is a constant value in your program, not allocated at run time.

And Mastaba: You don''t learn anything about how strings work if you use std::string. True, it works, but if you use it without having worked on it yourself, you''ve robbed yourself of some programming experiance. That''s MY opinion at least, you''re free to feel and say that using std::string is better.

Share this post


Link to post
Share on other sites