CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2009
    Posts
    23

    get command problem...

    Code:
    string get(char buffer[],int from, int until, int len)
    {
        return string(buffer,from,until);
    }
    It gives me some weird handling exception: std:ut_of_memory!?

    I thought strings can take up to 2GB of stuff...

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: get command problem...

    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);
    }

  3. #3
    Join Date
    Jul 2009
    Posts
    23

    Re: get command problem...

    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

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: get command problem...

    ...is because the characters i receive are for example the char of 0.
    You are receiving binary data... you can't treat that like a string.

  5. #5
    Join Date
    Jul 2009
    Posts
    23

    Re: get command problem...

    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!
    Last edited by HouseMD93; August 26th, 2009 at 08:11 AM.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: get command problem...

    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:

    Code:
    return string(buffer,from,until);
    There is no contructor that takes a char* as the first parameter
    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 );

  7. #7
    Join Date
    Jul 2009
    Posts
    23

    Re: get command problem...

    crash using 4), std::bad_alloc -_-

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: get command problem...

    what are the values of from and until ?

  9. #9
    Join Date
    Jul 2009
    Posts
    23

    Re: get command problem...

    I edited the script a little so it either chooses len or until.
    Code:
    string get(char buffer[],int from, int until, int len)
    {
    	int end = min(until,len-from);
    return string( buffer+from , buffer+end );
    }
    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.

    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)
    Last edited by HouseMD93; August 26th, 2009 at 09:51 AM.

  10. #10
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: get command problem...

    Your whole approach is wrong.

    (1) As I pointed out in my first post and as Philip Nicoletti said, when you call:
    Code:
    string(buffer,from,until);
    there's no template that takes char* pointer followed by an int, so your code is technically truncated to:
    Code:
    string(buffer);
    and 'buffer' is treated as a NULL terminated string, so if it isn't it can easily cause a memory fault.

    (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:
    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'
    }
    (3) And use debugger to see what's going on with your code.

  11. #11
    Join Date
    Jul 2009
    Posts
    23

    Re: get command problem...

    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:
    Code:
    read(Sock,(char*)&string,len,0)
    No?

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured