• Advertisement
Sign in to follow this  

extra garbage on strings

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
So do I have to convert it to a string stream first because it doesn't work with a file stream.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by etsuja
So do I have to convert it to a string stream first because it doesn't work with a file stream.


What do you mean it doesn't work? Of course it works if your stream is derived from istream... like std::ifstream or std::fstream is. It won't work with C-style FILE* if that's what you're trying to do.

Share this post


Link to post
Share on other sites
I guess I wasn't including the right header file because it wasn't working before but it works after including <sstream>

Share this post


Link to post
Share on other sites
Are you certain you included <string>? <sstream> is not necessary for inserting and extracting strings from a stream, but it will include <string> itself, which defines those operations.

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


jfl.

Share this post


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

  • Advertisement