简体   繁体   English

分段故障。 错误退出c程序

[英]Segmentation fault. error exits c program

I am developing ac program which connects to a mysql database reads data from aa table of 4 million of data and writes its data to another 100 tables in another database after some calculations. 我正在开发一个连接到mysql数据库的ac程序,从一个包含400万个数据的表中读取数据,并在一些计算后将其数据写入另一个数据库中的另外100个表中。 To make it efficient I tried to use 100 threads to write data for 100 tables and 1 thread to read data from the database and write those in to a buffer. 为了提高效率,我尝试使用100个线程为100个表和1个线程写入数据,以从数据库中读取数据并将其写入缓冲区。 So that the 100 threads would read from the buffers. 这样100个线程就会从缓冲区读取。

But the problem is when I'm making the buffers. 但问题是我正在制作缓冲区。 I used malloc to make the buffers. 我使用malloc来制作缓冲区。 char *** queue; is declared in the header file so that it is global 在头文件中声明,以便它是全局的

int i = 0, j = 0;
queue = (char ***) malloc(100);
int threadRet;

for (i; i < 100; i++) {
    queue[i] = (char **) malloc(2000);
    for (j; j < 2000; j++) {
        queue[i][j] = (char *) malloc(180);
    }
}

and My buffer writing thread as I mentioned before exicutes the function void * thrededChunkPicker(void * parr) 和我之前提到的缓冲区编写线程一样,用于清除函数void * thrededChunkPicker(void * parr)

        sprintf(queue[tableNo][marker[tableNo]], "INSERT INTO usage_summary%s(mobile,`0`,ddate) VALUES('%s',%d,'%s') ON DUPLICATE KEY UPDATE `0` = VALUES(`0`), ddate=VALUES(ddate)", row[0] + 7, row[0], sumOfUsage, row[2]);
        (marker[tableNo])++;

This is how I write to the buffer . 这就是我写入缓冲区的方式。 As I've found out the segmentation fault occurs here. 正如我发现的那样,分段故障发生在这里。

I needed 100 buffers in each of which contains 2000 string arrays of 180. this code compiled successfully.but when runs it gives a segmentation fault. 我需要100个缓冲区,每个缓冲区包含2000个180字符串数组。这段代码编译成功。但是运行时它会产生分段错误。

As far as I can see, all your malloc() calls are problematic. 据我所知,你所有的malloc()调用都是有问题的。

We pass the size in bytes to malloc() . 我们将字节大小传递给malloc() malloc() does not have any idea of the size of the object in which you're going to store the returned pointer, whatsoever. malloc()不知道你将要存储返回指针的对象的大小。 So, it cannot automatically calculate the total size for the memory required. 因此,它无法自动计算所需内存的总大小

We need to calculate the total size required and pass that size to malloc() . 我们需要计算所需的总大小并将该大小传递给malloc()

First to start with, please see this discussion on why not to cast the return value of malloc() and family in C . 首先, 请参阅此讨论,了解为什么不在Cmalloc()和family的返回值。 .

That said, assuming queue is defined as char *** queue; 也就是说,假设queue被定义为char *** queue; , we need to , 我们要

  • queue = malloc(100 * sizeof*queue );
  • queue[i] = malloc(2000 * sizeof(*queue[i]) );

and so on, if required. 等等,如果需要的话。

Finally, always check for the success of malloc() through a NULL check on the returned pointer before using the same. 最后,在使用之前,始终通过对返回指针的NULL检查来检查malloc()是否成功。

The biggest problem is that malloc allocates a number of bytes , not elements in an array. 最大的问题是malloc分配了多个字节 ,而不是数组中的元素。 So you allocate 100 bytes for your three-star variable, which is not enough space for 100 elements. 因此,为三星变量分配100个字节,这对于100个元素来说空间不足。 The same with the next call to malloc , it allocates 2000 bytes not 2000 elements. 与下一次调用malloc ,它分配2000 个字节而不是2000个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM