Skip to main content
gamedev.net
🔒 Locked

extra garbage on strings

Started by etsuja May 28, 2006 at 10:14 PM 16 replies 1.3k views
Original Post
etsuja
etsuja
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)
Oluseyi
Oluseyi
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.)
janta
janta
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.
etsuja
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.
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
MaulingMonkey
MaulingMonkey
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.
janta
janta
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

etsuja
etsuja
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)
KulSeran
KulSeran
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.
jflanglois
jflanglois
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.
Verg
Verg
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
Will F
Will F
Another vote for std::string.

You can also access elements like this
#include <string>#include <iostream>int main(){    std::string foo("abc");    std::cout << foo[1] << "\n";}


which will output:
b
etsuja
etsuja
Quote:
Original post by Verg


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





yes, but it will work with one element of the string which is what I was using it for...

Quote:
Original post by Verg
"strcmp" doesn't work like that. It works like this:


I believe it does work the way I was using it...

!0 evauluates to TRUE and I was doing !strcmp wich is the same thing if the strings matched.

Anyways I guess I will switch to std::string. I never really used it and didn't know the benefiets of it.

[Edited by - etsuja on May 29, 2006 4:07:24 AM]
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
etsuja
etsuja
alright, well I'm trying to switch over to string but I'm having some problems. first off it doesn't seem like I can write to a string from a file string the typical way like

File >> String

and secondly, how do I compare two strings to see if they're equal?

EDIT: I found the solution to the compare thing but still would like to know how to read data from a file into a string.

[Edited by - etsuja on May 29, 2006 3:31:54 AM]
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Enigma
Enigma
stream >> string; will copy characters from the file into the string until the first whitespace character or end-of-file is encountered.

std::getline(stream, string); will copy characters from the file into the string until the first newline character ('\n') or end-of-file is encountered.

std::getline(stream, string, character) will copy characters from the file into the string until the first occurance of character or end-of-file is encountered.

Σnigma
etsuja
etsuja
So do I have to convert it to a string stream first because it doesn't work with a file stream.
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
etsuja
etsuja
I guess I wasn't including the right header file because it wasn't working before but it works after including
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
jflanglois
jflanglois
Are you certain you included ? is not necessary for inserting and extracting strings from a stream, but it will include itself, which defines those operations.

defines std::stringstream, which allows you to process your string as if it were a stream.


jfl.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.