Thread: stack error for a char array

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    14

    stack error for a char array

    Hi:

    I have a socket client running which has a char array of size 512 mentioned. I have a loop to receive the message from the server, when the loop exits I am getting the following error.

    Environment:
    Windows 64bit.
    VS 2010

    Run-Time check failure #2 - Stack around the variable 'recMessage' was corrupted.

    Code:
    int STRLEN = 512;
    char recMessage[STRLEN] = "0";
    ClientSocket sockClient;
    
    sockClient.ConnectToServer( ipAddress.c_str(), port );
    sockClient.SendData("L|100=some_value;101=some_value\n");
    sockClient.RecvData( recMessage, STRLEN );
    
    int counter = 0
    while(counter <= 10){
    
    	sockClient.RecvData( recMessage, STRLEN );
    	cout << recMessage << endl;
    	counter++;
    }
    
    memset(recMessage,0,sizeof(char)*STRLEN);
    sockClient.CloseConnection();

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    14
    just wanted to confirm that the message I am receiving from the server is less than 200 characters.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You would need to post the code for sockClient.RecvData as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int STRLEN = 512;
    char recMessage[STRLEN] = "0";
    This is not valid C++ code.

    Also, the error message says you have a buffer overrun.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. Compiler error due to char array
    By Godders_2k in forum C Programming
    Replies: 13
    Last Post: 11-09-2007, 07:04 AM
  3. Char array display error...
    By Karpaty in forum C Programming
    Replies: 23
    Last Post: 10-31-2007, 07:17 AM
  4. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM
  5. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM