Thread: Dynamic Array Error

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Dynamic Array Error

    Hello, I am trying to export some code over from C# to C++. I am using visual studio 2008 pro. The error occurs at NewData = new char[PacketLength];. I was able to debug and make sure that PacketLength was indeed set to a reasonable number every time such as 9. When my code gets here it gets a segmentation fault every time. I am unfamiliar with using C++ to be honest and unsure what I am doing wrong...If i execute this code in main() it works fine.

    Thanks for any input.

    Code:
    void DataStream::Parse(char * Buffer, int Length)
    {
    	char * NewData = NULL;
    	short PacketLength = *(short *)Buffer;
    	std::cout << "Length: " << Length << " PacketLength: " << PacketLength;
    
    	while(PacketLength < Length)
    	{
    		NewData = new char[PacketLength];
    
    		memcpy(NewData, Buffer, PacketLength);
    		PacketLength = (short)(Buffer + PacketLength);
    		std::cout << PacketLength;
    		
    	}
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, can you post the smallest possible example that demonstrates the error?
    And I hope you're aware that C++ does not have garbage collection, so your new char will result in a memory leak.
    And NewData isn't really used anywhere (you just copy data into, but that's it), so it's kindof useless.
    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.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Contrast the first PacketLength calculation:
    Code:
    short PacketLength = *(short *)Buffer;
    with the second:
    Code:
    PacketLength = (short)(Buffer + PacketLength);
    What's different?

    Also, what do you think happens on the second iteration of the loop?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    This is the smallest possible example...I'm not done porting it over. Im aware of the garbage collection issue. Right now I cant add the rest because of the problem that I originally posted. Im well aware that right now this function does nothing and the loop is incomplete. However, on the first iteration of the loop when say PacketLength = 9, the allocation of memory errors out with "bad pointer" in visual studio 2008 every time. Im not sure why, this is what I am asking.

    NewData = new char[PacketLength]; <-----Error here.

    From looking online and in books this syntax seems perfectly ok...but it still errors.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Elysia View Post
    Well, can you post the smallest possible example that demonstrates the error?
    And I hope you're aware that C++ does not have garbage collection, so your new char will result in a memory leak.
    And NewData isn't really used anywhere (you just copy data into, but that's it), so it's kindof useless.
    The example is in my original post :P.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    PacketLength = (short)(Buffer + PacketLength);
    If I'm not mistaken, this casts an address (not what is located at that address) into a short.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    *sigh * Thanks for the help guys I really appreciate it, but again the problem is on the first iteration of the loop, IE it never gets that far anon...yes I should have that line changed to:

    PacketLength = *(short *)(Buffer + PacketLength);

    But it never gets this far. It is erroring out where I have mentioned a couple times in this thread.

    I get errors on the first allocation attempt of NewData = new char[PacketLength]; And I have verified that PacketLength at this location is what it should be(say 9 bytes). I get a Bad Pointer Error, and my program closes. Using vs 2008 pro.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "Bad Pointer Error" is weird for allocation. Most likely you've got a corrupted heap, and the problem is somewhere else.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Hmmm really? I wonder why...Im only allocating 9 bytes on this test. The strange thing is it doesn't occur in main() when I allocate say, 6k bytes...Only in an external class file when I call this method from main().

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Hmm on further inspection you seem very right. It is now working 20&#37; of the time, and then throwing an error the rest of the time. What is traditionally the cause of this? I am on a modern system with plenty of memory available, and at the moment at most I only allocate 9 bytes to the heap.

    Also an update to my code.

    Code:
    void DataStream::Parse(char * Buffer, int Length)
    {
    	char * NewData = NULL;
    	short PacketLength = *(short *)Buffer;
    	std::cout << "Length: " << Length << " PacketLength: " << PacketLength;
    
    	while(PacketLength < Length)
    	{
    		NewData = new char[PacketLength];
    
    		memcpy(NewData, Buffer, PacketLength);
    		Packet(Buffer, PacketLength);
    		Length -= PacketLength;
    		PacketLength = *(short *)(Buffer + PacketLength);
    		
    	}
    }
    
    void DataStream::Packet(char * Buffer, int length)
    {
    	char PacketType = *(char *)(Buffer + 2);
    	switch(PacketType)
    	{
    	case 1:
    		std::cout << "Got a Chat Packet\n";
    		break;
    	}
    
    	delete Buffer;
    }

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's kind of what CornedBee meant. You likely have a memory error somewhere else in the program. For example, if the class itself wasn't allocated properly, you might not be able to access members of the class without segfaulting. Or you might modify the class members, mess up the internals of your program's memory allocation, and so the next memory allocation call causes a segfault. Who knows.

    You'll probably have to provide more detail, preferably in the form of code, best of all in the form of compilable code.

    [edit] Whoops, a post appeared whilst I was typing this one.

    Code:
    char PacketType = *(char *)(Buffer + 2);
    That is the same as, but a good deal less readable than,
    Code:
    char PacketType = Buffer[2];
    And, likely your real problem:
    Code:
    Packet(Buffer, PacketLength);
    Methinks you're passing (and then freeing) the wrong variable here. Maybe you wanted NewData instead of Buffer? [/edit]
    Last edited by dwks; 10-11-2008 at 04:59 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by valaris View Post
    The example is in my original post :P.
    Well, the problem is that I don't know how to call Parse.
    I tried my substitute code, but couldn't reproduce the error.
    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.

  13. #13
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I call it by passing it a test array or some sort. I had built a packet parser that works perfectly in c#, was just trying to port it over to C++. So I makeup an array with the first two bytes of each packet being a short that describes the lemgth of the packet to be parsed out of the chunk. This length includes the length size itself, so an example would be.
    Code:
    char payload[] = {3, 0, 1, 3, 0, 2, 4, 0, 3, 1};
    parse(payload, sizeof(payload));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM