c - 尝试填充结构数组时发生访问冲突

标签 c arrays struct

指定核心问题的原始代码注释:

The error I am getting is while iterating through the while loop, memory out of range or something... resizing to 300 ... Access violation writing location that's the exact Fraze...

我正在尝试实现更快的.Net List<T>相当于 C 语言。

我在 C# 中使用 blittable 数据类型。

在下面的代码中,我将一个函数体移动到主函数中,只是为了在我无法理解我错在哪里之后进行测试。

问题似乎出在 while循环 UntArr不增加。 我做错了什么?

typedef struct {
    int Id;
    char *StrVal;
}Unit; // a data unit as element of an array

unsigned int startTimer(unsigned int start);
unsigned int stopTimer(unsigned int start);

int main(){

Unit *UntArr= {NULL};
//Unit test[30000];

//decelerations comes first..
char *dummyStringDataObject;
int adummyNum,requestedMasterArrLength,requestedStringSize,MasterDataArrObjectMemorySize,elmsz;
int TestsTotalRounds, TestRoundsCounter,ccountr;
unsigned int start, stop, mar;

//Data Settings (manually for now)
requestedMasterArrLength=300;
requestedStringSize = 15;

//timings
start=0;stop=0;
//data sizes varies (x86/x64) compilation according to fastest results
MasterDataArrObjectMemorySize = sizeof(UntArr);
elmsz= sizeof(UntArr[0]);

TestRoundsCounter=-1;
start = startTimer(start);

while(++TestRoundsCounter<requestedMasterArrLength){
    int count;
    count=-1;
    //allocate memory for the "Master Arr"
    UntArr = (Unit *)malloc(sizeof(Unit)*requestedMasterArrLength);
    dummyStringDataObject = (char*)malloc(sizeof(char)*requestedStringSize);
    dummyStringDataObject = "abcdefgHijkLmNo";
    while (++count<requestedMasterArrLength)
    {
        dummyStringDataObject[requestedStringSize-1]=count+'0';
        puts(dummyStringDataObject);

        ccountr=-1;
        // tried
        UntArr[count].Id = count;
        UntArr[count].StrVal = (char*)malloc(sizeof(char)*requestedStringSize);
        UntArr[count].StrVal = dummyStringDataObject;// as a whole 
        //while(++ccountr<15)// one by one cause a whole won't work ?
        //UntArr[count].StrVal[ccountr] = dummyStringDataObject[ccountr];

    }

free(UntArr);free(dummyStringDataObject);

}
stop = startTimer(start);
mar = stop - start;


MasterDataArrObjectMemorySize = sizeof(UntArr)/1024;
printf("Time taken in millisecond: %d ( %d sec)\r\n size: %d kb\r\n", mar,(mar/1000),MasterDataArrObjectMemorySize);
printf("UntArr.StrVal: %s",UntArr[7].StrVal);

getchar();

return 0;
}



 unsigned int startTimer(unsigned int start){
     start = clock();
     return start;
 }
 unsigned int stopTimer(unsigned int start){
     start = clock()-start;
     return start;
 }

逐一测试代码而不是在 while 循环内按预期工作

//allocate memory for the "Master Arr"
UntArr = (Unit *)malloc(sizeof(Unit)*requestedMasterArrLength);
UntArr[0].Id = 0;
dummyStringDataObject = (char*)malloc(sizeof(char)*requestedStringSize);

dummyStringDataObject = "abcdefgHijkLmNo";

////allocate memory for the string object
UntArr[0].StrVal = (char*)malloc(sizeof(char)*requestedStringSize);
////test string manipulation
adummyNum=5;
UntArr[0].StrVal= dummyStringDataObject;
//
UntArr[0].StrVal[14] = adummyNum+'0';
////test is fine

最佳答案

当它发生时,因为我是指针新手,所以在调试时我没有意识到 我不会像以前那样看到给定的指向数组的指针的元素 使用正常的 Array[] ,但循环遍历结果,我什至没有尝试,因为当我在 while 循环内悬停在 Array* 上方时,期望看到元素,就像在普通数组:

Data[] DataArr = new Data[1000] <- 我希望在循环和填充 Data* 时实际看到数组的主体,但没有意识到它不是一个实际的数组,而是一个指向数组的指针,因此您看不到元素/主体。

解决方案是通过现在按照最初计划的函数:

void dodata(int requestedMasterArrLength,int requestedStringSize){

    int ccountr,count;
    count=0;
    UntArr=NULL;
    UntArr = (Unit *)malloc(sizeof(Unit)*requestedMasterArrLength);

        while(count!=requestedMasterArrLength)
        {
                char dummyStringDataObject[]= "abcdefgHi";

                UntArr[count].StrVal=NULL;  

                dummyStringDataObject[requestedStringSize-1] = count+'0';
                UntArr[count].Id= count;
                ccountr=0;
                UntArr[count].StrVal= (char*)malloc(sizeof(char)*requestedStringSize);
                    while(ccountr!=requestedStringSize){
                        UntArr[count].StrVal[ccountr] = dummyStringDataObject[ccountr];
                        ++ccountr;
                    }
            ++count;
        }


}

一般来说,x86 编译会为当前任务获得更好的性能,填充结构体数组。 所以我也在 c++c# 中编译了它。

C#C++中执行类似的代码

c# 测量的最短时间约为 3,100 毫秒。

此代码中测得的最短时间 - C ~ 1700 毫秒。

C++ 测量的最短时间约为 900 毫秒。

我很惊讶地看到最后的结果 c++ 是获胜者,但为什么呢? 我以为c更接近系统级别,CPU,内存...

关于c - 尝试填充结构数组时发生访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33177418/

相关文章:

Javascript 将数据总数放入列表中

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

在 C 的 typedef 结构中调用函数

ios - fatal error : Index out of range for in loops swift 3

c - Libcurl:为什么 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, obj) 传递我的 writefunction 指针,它不等于 obj?

c - int * ( * ) ( int * ) 这个类型在c语言中代表什么?

C - 使用 sin_addr 获取 IP 地址字符串

c - Qsort() 不适用于结构

c++ - 返回其自身类型的结构的结构属性

java - 如何解决这个难题?