It gives me some weird handling exception: std::out_of_memory!?Code:string get(char buffer[],int from, int until, int len)
{
return string(buffer,from,until);
}
I thought strings can take up to 2GB of stuff...
Printable View
It gives me some weird handling exception: std::out_of_memory!?Code:string get(char buffer[],int from, int until, int len)
{
return string(buffer,from,until);
}
I thought strings can take up to 2GB of stuff...
2 Gigs is a lot of memory and the memory management function may issue an exception if your system has less than that, but I'd still venture to say that your problem is in one of the following:
(1) The 'buffer' is either not large enough as specified by 'from' or 'until', or it is not NULL-terminated
(2) Does string() actually need 'from' and 'until'?
(3) 'from' or 'until' is out of range the way you intended them.
Do the following checks (if you're using MFC):
Code:string get(char buffer[],int from, int until, int len /*????*/)
{
ASSERT(buffer);
ASSERT(until >= 0);
ASSERT(from >= 0 && from < until);
ASSERT(!::IsBadStringPtr(&buffer[from], until - from));
return string(buffer,from,until);
}
I am using sockets to receive, so len is len the real length. The reason why i use len in the first place, is because the characters i receive are for example the char of 0. I shall try again but i am not sure. Thanks for your help :)
You are receiving binary data... you can't treat that like a string.Quote:
...is because the characters i receive are for example the char of 0.
What? Binary data? What can i use then (You do realize after the chars of 0 1 2 3 4 etc) i do get letters so what do i use? Even appending correct letters 1 at a time doesn't work :( What the hell!
1) You pass a "len", but do not use it.
2) std::string can handle any valid char (including NULL) ... but you need to
be careful at times (see next note for example).
3) The following line can be probematic:
There is no contructor that takes a char* as the first parameterCode:return string(buffer,from,until);
followed by two int variables. What I think is happening is buffer
is being converted to a std:string using the constructor that stops
at the first NULL. So if you have embedded NULLs, you will not
get all the data you want and if it does not contain embbeded NULLs,
it will keep adding to the string until it reaches a NULL.
4) maybe you can try:
Code:return string( buffer+from , buffer+until+1 );
crash using 4), std::bad_alloc -_-
what are the values of from and until ?
I edited the script a little so it either chooses len or until.
also i am not sure what the values are, they are found by a command i call find. It looks for a specific character and then returns if its there. Also I added a so it would take the 2nd or 3rd of the same character if i search for it.Code:string get(char buffer[],int from, int until, int len)
{
int end = min(until,len-from);
return string( buffer+from , buffer+end );
}
I am using the command like this: get(buffer,b+8,found - b - 10,len)
b is the number that is returned by find. (It worked fine until i switched from char* to string as i didn't realize it caused a serious memory leak xD)
Your whole approach is wrong.
(1) As I pointed out in my first post and as Philip Nicoletti said, when you call:
there's no template that takes char* pointer followed by an int, so your code is technically truncated to:Code:string(buffer,from,until);
and 'buffer' is treated as a NULL terminated string, so if it isn't it can easily cause a memory fault.Code:string(buffer);
(2) Transmitting data through a socket is not done as a continuous stream of char's. You need to create a custom type, put it into a header file shared by both sender and receiver and validate it before you attempt reading a 'char' string from it:
(3) And use debugger to see what's going on with your code.Code:struct MY_SOCKET_DATA{
DWORD dwStructID; //Special ID to identify this struct
int ncbSzTotal; //Total size of transmitted data in bytes
int nSzString; //Size of string in char's
char String[0]; //Beginning of transmitted ANSI string
};
//Validation
MY_SOCKET_DATA* pSD = (MY_SOCKET_DATA*)pSocketRawData;
if(pSD->dwStructID == UNIQUE_TRANSMISSION_ID &&
pSD->ncbSzTotal == ncbSocketRawDataLength)
{
//Now you can read 'pSD->nSzString' number of chars from 'pSD->String'
}
I understand then that the method i've been using leads me nowhere. But what do you mean by 2) ?? I use this usually char buffer[100] but i can read it into a string anyway with this:
No?Code:read(Sock,(char*)&string,len,0)
Besides it isn't really solving my problem with get. If i cannot do that, what should i do. I've tried to append it and then use string(appended,from,until) which gives the exact same error.
Also, would using Constant array's also cause a memory leak? (e.g. buffer[100]) would i need to use delete on that too?