extra garbage on strings

Started by
16 comments, last by jflanglois 17 years, 10 months ago
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?
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
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.)
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.
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.
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
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.
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

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))                        {                        }
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
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.
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.
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
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]

This topic is closed to new replies.

Advertisement