Jump to content
  • Advertisement
Sign in to follow this  
etsuja

extra garbage on strings

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

when I try to allocate some memory for a string I'm getting extra garbage added on to my strings like this Etsujaýýýý««««««««îþîþîþ I'm allocating them like this String = new char[StringLength]; Anyone know how to solve this?

Share this post


Link to post
Share on other sites
Advertisement
Allocation does not zero out the memory. You're seeing whatever the contents of that portion of memory was at the point of allocation, interpreted as a string.

Nothing's wrong.

Of course, if you're seeing garbage like that when you print the string, then the problem is likely that you're forgetting to null terminate your string. Of course, the real problem is your insistence on using an array of character data as an analogue for a string, when C++ has a real string type, std::string. (The preceding assumes you're using C++ and not C.)

Share this post


Link to post
Share on other sites
At least two posibilities.

Either memset you array to 0: memset(String, 0, StringLength * sizeof(char));

Or, assuming you're always using it as a properly NULL-terminated string (and that requires extra-care), just place a null terminator at the begining and you're done: String[0] = '\0'; or *String = 0; All function using a null-terminated array of char will consider your string is empty, eventhoug it is still full of garbage but that doesnt matter. Just be aware of it.

Share this post


Link to post
Share on other sites
I tried putting the null terminator at the beginning before copying a string into it and I still get the garbage and I tried memset but when I copy the string to the allocated array from the pointer I still get tyhe garbage on the end of my string and it matters because I need to compare this string to another and they don't match with the extra garbage.

Share this post


Link to post
Share on other sites
It'll be easier for us to point out exactly what you're doing wrong if you post the source code. Using std::string instead of const char * is also probably a good idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by etsuja
I tried putting the null terminator at the beginning before copying a string into it and I still get the garbage and I tried memset but when I copy the string to the allocated array from the pointer I still get tyhe garbage on the end of my string and it matters because I need to compare this string to another and they don't match with the extra garbage.


What is your method to compare char[] ? Beacause basically, when comparing two char[], only what is before eatch char[]'s NULL terminator matters. So there is definitely somethimg you're doing wrong. How do you copy char[] to each other ?

What do you mean: "I tried memset but when I copy the string to the allocated array from the pointer I still get tyhe garbage on the end of my string" ?

Or take the easy path if you can: use std::string (it is not always better, though, depending on what you're doing)

And yeah, show your code :)

Regards, Janta

Share this post


Link to post
Share on other sites
Here's what I'm doing. Manually assigning the InBuffer for testing.
This Doesn't work whether I have the null terminator at the end or not. The if strcmp never evaluates to true. And I don't think I can use std::string because I need to access different indexes in the buffer. I could but I'd have to change this all around.


InBuffer[0] = '\0';

strcpy(InBuffer,"2Etsuja\n");

if(atoi(&InBuffer[0]) == PCKT_LOGIN)
{


char* ATempBuf = NULL;
char* ALoginName;
size_t StrLen;

ACNTSTRUCT* ThisAccount = FirstAccount;
char* ThisAccountName = NULL;

ATempBuf = InBuffer + 1;
StrLen = strlen(ATempBuf);
ALoginName = new char[StrLen];
memset(ALoginName,0,StrLen * sizeof(char));
memcpy(ALoginName,ATempBuf,StrLen);

while(ThisAccount != NULL)
{
ThisAccountName = ThisAccount->AcntLoginName;
if(!strcmp(ThisAccountName,ALoginName))
{
}

Share this post


Link to post
Share on other sites
strlen() returns the length of the string minus the NULL.
You need to make your new array [StrLen+1] elements
and clear StrLen+1 elements with memset(or just make the last element 0).

But you really should just switch to std::string.

Share this post


Link to post
Share on other sites
Understand that strlen will give you the number of characters before the null-terminator. This means that when you allocate ALoginName, you are probably allocating one too little characters.

[edit] Eh, beaten.

Quote:
Of course, the real problem is your insistence on using an array of character data as an analogue for a string, when C++ has a real string type, std::string. (The preceding assumes you're using C++ and not C.)

Agreed, and since he's using new, he's probably using C++. Please, and I've said this in another thread of yours, use std::string. You can access elements of the string via iterators.


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by etsuja
The if strcmp never evaluates to true.


"strcmp" doesn't work like that. It works like this:


if(strcmp(string_1, string_2) == 0) // the strings are equivalent

...

if(strcmp(string_1, string_2) < 0) // string_1 < string_2

...

if(strcmp(string_1, string_2) > 0) // string_1 > string_2


Also "2Etsuja\n" isn't going to work with "atoi"

And... you have to memset your buffer with zeroes after allocation, and leave enough room for a zero terminator for your "char" string to work out o.k.

If your buffer is the exact length of the string you're copying over, it's too short... because you need an extra byte for the null terminator.

...

All this said... it probably would behoove you to look at std::string as the other posters suggest (unless you're programming in strict 'C'). std::string manages all the allocation and deallocation... and will not produce the "garbage" that you describe at the end of your string.



Chad

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!