segfault error on memcpy

Sample code:

Constructor & Destructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BipDB::BipDB(u8* framePtr, u32 frameSize): dlBip(0), framePtr(framePtr), frameSize(frameSize)
{
	dlBip = new dlFrame(frameSize);
	memset(dlBip, 0, sizeof(dlFrame));
}

BipDB::~BipDB()
{
	if(dlBip)
	{
		delete dlBip;
		dlBip = 0;
	}

}


dlFrame structure
1
2
3
4
5
6
7
8
9
10
11
12
struct dlFrame
{
	u8 * payload;
	dlFrame(u32 size)
	{
		payload = new u8[size];
	}
	~dlFrame()
	{
		delete [] payload;
	}
};


A segfault is triggered when I try to use memcpy
 
	memcpy(dlBip->payload, getFramePtr(), getFrameSize());


framePtr's size is frameSize. Can anyone tell me what's wrong?
Don't use memcpy with new/delete: It will almost never work. Anything with constructors normally isn't contiguous in memory, the alignment from new and malloc is different, and they don't necessarily even access the same heap.

Basically, use new and delete, and avoid the use of memset and memcpy, instead doing it properly (i.e. do proper intialization and properly overload copy constructors and assignment operators to perform deep copies).
Topic archived. No new replies allowed.