[英]Problems Using memset and memcpy
所以我正在嘗試創建一個內存管理系統。 為了做到這一點,我有一定量的空間(由malloc分配),然后我有一個函數myMalloc,它將基本上返回一個指向分配空間的指針。 由於我們將嘗試釋放它,我們嘗試使用memset將分配空間的標頭設置為分配空間的大小。
memset(memPtr,sizeBytes,sizeof(int));
然后我們需要能夠閱讀這個,以便我們可以看到它的大小。 我們試圖通過使用memcpy並將第一個sizeof(int)字節放入變量來實現此目的。 出於測試目的,我們只是嘗試進行memset,然后立即恢復尺寸。 我已經包含了下面的整個方法,以便您可以查看所有聲明。 任何幫助將不勝感激! 謝謝!
void* FirstFit::memMalloc(int sizeBytes){
node* listPtr = freelist;
void* memPtr;
// Cycle through each node in freelist
while(listPtr != NULL)
{
if(listPtr->size >= sizeBytes)
{
// We found our space
// This is where the new memory allocation begins
memPtr = listPtr->head;
memset(memPtr,sizeBytes,sizeof(int));
void *size;
memcpy(size, memPtr, sizeof(int));
// Now let's shrink freelist
listPtr->size = listPtr->size - sizeBytes;
int *temp = (int*)listPtr->head + (sizeBytes*sizeof(int));
listPtr->head = (int*) temp;
return memPtr;
}
listPtr = listPtr->next;
}
::編輯::抱歉! 運行此命令時,我們在嘗試運行memcpy行時會遇到段錯誤。 過去一小時左右,我們一直在玩不同的想法,老實說,根本不知道錯誤發生在哪里。
:: Edit2 ::我也發布了這個評論,但我想也把它放在這里,所以更容易找到...
我們的問題是我們有一個允許使用的分配空間,由一個malloc調用128MB指定。 我們只能使用它,所以我們不能使用malloc將大小初始化為任何東西。 我想,如果沒有初始化大小,有沒有辦法做到這一點。 如果沒有,無論如何都要在不使用memcpy的情況下獲取標頭設置的int。
memcpy的原型是
void * memcpy ( void * destination, const void * source, size_t num );
問題在於:
void *size; /* you haven't initialized this variable, and then you're writing to what it points to*/
memcpy(size, memPtr, sizeof(memPtr)); /* because size points to uninitialized memory it seg faults*/
編輯1:請查看本教程關於C和C ++的指針除非您理解指針,否則您將無法理解為什么這兩行代碼背靠背是一對糟糕的。
你對memset的使用很奇怪:
memset(memPtr,sizeBytes,sizeof(int));
相當於(假設一個32位整數):
*((char *)memPtr + 0) = (sizeByte & 0xFF);
*((char *)memPtr + 1) = (sizeByte & 0xFF);
*((char *)memPtr + 2) = (sizeByte & 0xFF);
*((char *)memPtr + 3) = (sizeByte & 0xFF);
如您所見,它將每個字節設置為相同的值,即sizeBytes
的低字節。
我不確定你打算做什么,所以我不能提供修復。
void *size;
是一個未初始化的指針,當你嘗試memcpy
進入它時,你的進程將嘗試寫這個無效的位置導致seg錯誤。
如果你在Windows中寫它...你可以使用
IsBadWritePtr
驗證調用進程是否具有對指定內存范圍的寫訪問權。 可能有三個原因
1>指針是垃圾或NULL
2>你要復制的金額太多了。
即復制超過內存塊的末尾。 字符串文字的潛在“向后”副本也會導致此行為
char *s = "Hello";
char t[10];
memcpy(s, t, 6);
你的代碼中有許多錯誤-而不是通過他們去一個接一個,我給你的它應該是什么樣一個注釋版本:
void* FirstFit::memMalloc(size_t sizeBytes) // size_t is the appropriate type for memory sizes
{
node* listPtr = freelist;
void* memPtr;
// The actual allocation needs to be bigger, to have space to hold the size itself.
size_t allocSize = sizeBytes + sizeof allocSize;
// Test to make sure that allocSize didn't wrap around to zero
if (allocSize < sizeBytes)
{
return NULL;
}
// Cycle through each node in freelist
while(listPtr != NULL)
{
if(listPtr->size >= allocSize)
{
// We found our space
// This is where the new memory allocation begins
memPtr = listPtr->head;
// Copy the size to the start of the memory region
memcpy(memPtr, &allocSize, sizeof allocSize);
// Increment the pointer to be returned past the size
char *tempPtr = (char *)memPtr;
memPtr = (void *)(tempPtr + sizeof allocSize);
// Shrink the block
listPtr->size -= allocSize;
tempPtr = (char *)listPtr->head;
listPtr->head = (void *)(tempPtr + allocSize);
// TODO: If the block is now zero-sized, remove it from the linked list
return memPtr;
}
listPtr = listPtr->next;
}
/* No space */
return NULL;
}
在創建自己的內存管理系統時,既然已經了解了memset()的作用和不行,希望已經知道了足夠的低級內容,你知道memcpy()和memmove()之間的區別,你的下一步是了解“對齊”以及malloc()實現的保證,但您的代碼卻沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.