- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 build 一棵垃圾树。示例:键 AAAA1.ETR,值 1
。
我正在读取具有以下结构的文件:
DataLength Block-SequenceNummer FLag Start Data Ende
4 Bytes 8 Bytes 1 Byte S Datalength E
数据可以压缩或解压缩(这保存在标志中)。数据包含更多消息。
我收到此错误:
*** glibc detected *** /home/frog/reader/unzipper: free(): invalid pointer: 0x00007ffff4c55108 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7ffff758cb96]
/home/frog/reader/unzipper[0x401f2f]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7ffff752f76d]
/home/frog/reader/unzipper[0x400f59]
回溯:
(gdb) backtrace
#0 0x00007ffff7544425 in __GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1 0x00007ffff7547b8b in __GI_abort () at abort.c:91
#2 0x00007ffff758239e in __libc_message (do_abort=2, fmt=0x7ffff768c748 "*** glibc detected *** %s: %s: 0x%s ***\n") at ../sysdeps/unix/sysv/linux/libc_fatal.c:201
#3 0x00007ffff758cb96 in malloc_printerr (action=3, str=0x7ffff7689053 "free(): invalid pointer", ptr=<optimized out>) at malloc.c:5039
#4 0x0000000000401f1a in main () at u.c:373 (which is datap)
这是我迄今为止尝试过的:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "zlib.h"
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <glib.h>
#include <sys/stat.h>
#define MYMAXSIZE 10000000
#define MAXFILESIZE 6000000
#define OBJNAME "8"
#define MSGTYPE "1"
const uint8_t broadcast[] = { 0x36, 0x33 };
const uint8_t status[] = { 0x34 };
const char dir0[]= "/home/frog/reader/feed.0/";
const char dir1[]= "/home/frog/reader/feed.1/";
const char dir2[]= "/home/frog/reader/feed.2/";
const char dir3[]= "/home/frog/reader/feed.3/";
typedef struct mtmheader_t
{
unsigned char objName[20]; //8
unsigned char msgType[3]; //1
}mtmheader_t;
typedef struct analyzer_t
{
unsigned char *buff;
size_t s;
}analyzer_t;
typedef struct analyzers_t
{
analyzer_t anal1;
analyzer_t anal2;
analyzer_t anal3;
unsigned char* seq;
}analyzers_t;
int readFilec(GTree *tree)
{
FILE * fp = fopen("cfg/InstrumentList_FULL.csv", "rb" );
char * line = NULL;
size_t len = 0;
ssize_t read;
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
char *p1;
int *p2 = malloc(sizeof (int));
printf("%s", line);
p1 = strtok(line, "|");
*p2 = atoi(strtok(NULL, "|"));
g_tree_insert(tree, (gpointer) strdup( g_strdup(p1)), (gpointer)p2);
//TrieAdd(&root, strdup(p1), p2);
printf("-%s%d ", p1, *p2);
}
//exit(EXIT_SUCCESS);
}
int readFile( char *name,unsigned char *buffp)
{
int fileSize = 0;
//int n, i, j;
printf("Opening file: \"%s\"\n", name);
FILE *pFile = fopen(name, "rb");
if(pFile == NULL)
{
printf("error: fopen()");
return -1;
}
fseek(pFile, 0, SEEK_END);
fileSize = ftell(pFile);
printf ("%d\n", fileSize);
rewind(pFile);
//unsigned char *data = (unsigned char*) calloc(sizeof(unsigned char), fileSize + 20);
int bytes = fread(buffp, 1, fileSize, pFile);
if(ferror(pFile))
{
printf("error: fread()");
return -1;
}
return bytes;
}
int processMTMHeader(unsigned char *datap, mtmheader_t *h, unsigned char *endmmsgp)
{
unsigned char *nextFsp;
unsigned char *nextRsp;
//nextRs
size_t size = endmmsgp - datap;
//Den Header komplett abarbeiten (bis GS (x1D) oder ETX (x03) kommt)
//Byte lesen = RS (x1E)
while ( (datap = (memchr(datap, 0x1E, size))) != NULL)//datap < endmmsgp)//= strchr(datap, '\x1E') != NULL )
{
datap++;
//next Fs
//Byte lesen = FS (x1C)
nextFsp = memchr(datap, 0x1C,size);
//Byte lesen = RS (x1C)
nextRsp = memchr(datap, 0x1E, size);
size = endmmsgp - nextRsp;
if ( nextFsp - datap != 1)continue;
if( *datap == 0x38 )
{
//Objectnamen generieren() (Nur die ersten beiden Felder des Objectnamens verwenden)
memcpy (h->objName, nextFsp+1, nextRsp - ( nextFsp+1) );
h->objName[ nextRsp - ( nextFsp+1)]='\0';
//Abbruch der Schleife da ich jetzt den Objectnamen habe (Den Header komplett abarbeiten )
return 1;
}
else if ( *datap == 0x31 )
{
memcpy (h->msgType, nextFsp+1, nextRsp - ( nextFsp+1) );
h->msgType[nextRsp - ( nextFsp+1)]='\0';
if( (memcmp(h->msgType, broadcast, sizeof broadcast) != 0) && (memcmp(h->msgType,status, sizeof( status))!=0 )) return 2;
}
}
return -1;
}
int processData(unsigned char *datap, size_t size, GTree* t, analyzers_t *anals)
{
int headerOk=0;
unsigned char *nextmtmmsgp;
unsigned char *endmmsgp ;
int * qsp;
int s;
while ( (datap = memchr (datap, 0x02, size))!= NULL )
{
//datap++;
//printf("data\n%s", datap);
endmmsgp = memchr (datap, 0x03, size);
mtmheader_t h;
headerOk = processMTMHeader(datap, &h, endmmsgp );
int headersSize1 = 10;
int headersSize2 = 10;
int headersSize3 = 10;
if (headerOk == 1)
{
if ((qsp = (int *) g_tree_lookup(t, h.objName)) != NULL )
{
s = (endmmsgp - datap )+ 1;
printf("found: %s %d\n", h.objName, *qsp);
switch (*qsp)
{
case 1:
memcpy(anals->anal1.buff + anals->anal1.s + headersSize1, datap, s);
anals->anal1.s = s + headersSize1 + anals->anal1.s ;
headersSize1=0;
break;
case 2:
memcpy(anals->anal2.buff + anals->anal2.s + headersSize2, datap, s);
anals->anal2.s= s + headersSize2 + anals->anal2.s ;
headersSize2=0;
break;
case 3:
memcpy(anals->anal3.buff + anals->anal1.s + headersSize3, datap, s);
anals->anal3.s= s + headersSize3 + anals->anal3.s ;
headersSize3=0;
break;
}
}
else if (headerOk == 2)
{
memcpy(anals->anal1.buff + anals->anal1.s + headersSize1, datap, s);
anals->anal1.s = s + headersSize1 + anals->anal1.s ;
headersSize1=0;
memcpy(anals->anal2.buff + anals->anal2.s + headersSize2, datap, s);
anals->anal2.s= s + headersSize2 + anals->anal2.s ;
headersSize2=0;
memcpy(anals->anal3.buff + anals->anal1.s + headersSize3, datap, s);
anals->anal3.s= s + headersSize3 + anals->anal3.s ;
headersSize3=0;
}
}
datap++;
printf("Name, type: %s %s\n",(char *) &h.objName,(char *) &h.msgType);
}
return -1;
}
gboolean iter_all(gpointer key, gpointer value, gpointer data) {
int *s = (int *)value;
printf("\n%s%d \n", (char *)key, *s);
return FALSE;
}
int writeFile(char *name, unsigned char *buff, size_t *size, char *dir )
{
FILE * pFile;
char fullName[50];
//fullName[0]='\0';
//strcat(fullName, dir);
//strcat(fullName, name);
chdir (dir);
pFile = fopen ( name, "wb");
//pFile = fopen (strcat ( strcat( strcat("feed.",anal), "/") , name ), "wb");
fwrite (buff , sizeof(unsigned char), *size, pFile);
fclose (pFile);
}
int writeFiles(char *name, analyzers_t *anals )
{
if (anals->anal1.s > 10)writeFile(name, anals->anal1.buff, &anals->anal1.s, dir1);
if (anals->anal2.s > 10)writeFile(name, anals->anal2.buff, &anals->anal2.s, dir2);
if (anals->anal3.s > 10)writeFile(name, anals->anal3.buff, &anals->anal3.s, dir3);
}
int addHeader(size_t *start_size, size_t *end_size,unsigned char *buff, unsigned char *seq)
{
*(buff+*start_size+ 9) ='S';
uint32_t s = ((*end_size - *start_size) - 10);
//size_t s_be = htonl(s);
memcpy(buff+*start_size, &s, sizeof(s));
memcpy(buff+4+*start_size, seq, 8 );
buff[*start_size+8] = '\x00';
*(buff + *end_size ) = 'E';
}
int main()
{
analyzers_t anals;
anals.anal1.buff = malloc(MYMAXSIZE);
anals.anal2.buff = malloc(MYMAXSIZE);
anals.anal3.buff = malloc(MYMAXSIZE);
unsigned char *bytesp = malloc (MAXFILESIZE);
unsigned char *datap = malloc (MYMAXSIZE);
//char dir[] = "feed.0/";
DIR *dirp;
char currDir[]=".";
char parentDir[]="..";
GTree* t = g_tree_new((GCompareFunc)g_ascii_strcasecmp);
readFilec(t);
g_tree_foreach(t, (GTraverseFunc)iter_all, NULL);
struct dirent *direntp;
if ((dirp = opendir(dir0)) == NULL)
{
perror("opendir");
return 0;
}
struct stat status;
chdir(dir0);
int pos=0;
uint32_t seq;
size_t size;
int flag;
if (datap == NULL)return -1;
unsigned char *datapor = datap;
int compressOk=0;
int loop =0;
size_t oldSize1=0;
size_t oldSize2=0;
size_t oldSize3=0;
while( (direntp = readdir(dirp)) != NULL)
{
if(strcmp(direntp->d_name, currDir) == 0)continue;
if(strcmp(direntp->d_name, parentDir) == 0)continue;
lstat(direntp->d_name, &status);
if(S_ISDIR(status.st_mode))continue;
compressOk=0;
loop =0;
pos=0;
anals.anal1.s = 0;
anals.anal2.s = 0;
anals.anal3.s = 0;
oldSize1 = 0;
oldSize2 = 0;
oldSize3 = 0;
anals.anal1.buff[0]='\0';//anal1or;
anals.anal2.buff[0]='\0';//anal2or;
anals.anal3.buff[0]='\0';//al3or;
readFile(direntp->d_name, bytesp);
while(bytesp[pos+9] == 'S')
{
datap = datapor;
printf("----------------------------\n");
printf("| LOOP %d, POSITION %d |\n", loop, pos);
printf("----------------------------\n\n");
size = ntohl(bytesp[pos+0]<<24| bytesp[pos+1]<<16| bytesp[pos+2]<<8| bytesp[pos+3]);
if (bytesp[pos+10+ size]!='E')
{
pos = pos + 11 + size;
continue;
}
seq = ntohl(bytesp[pos+4]<<24| bytesp[pos+5]<<16| bytesp[pos+6]<<8| bytesp[pos+7]);
anals.seq = bytesp +pos+4;
//Flag lesen
flag = bytesp[pos+8];
if( bytesp[pos+8]=='\x01')
{
size_t size_uncompress = MYMAXSIZE * sizeof(unsigned char);
compressOk = uncompress(datap, &size_uncompress, &bytesp[pos+10], size);
datap[size_uncompress]='\0';
printf ("%zu %zu :\n", size, size_uncompress) ;
}
else
{
datap = &bytesp[pos+10];
//datap[pos+10+ size] ='\0';
}
printf("---------------------------- \n");
printf("| Message as String: |\n");
printf("---------------------------- \n");
printf("%s\n\n\n", datap);
processData(datap, size, t, &anals);
pos = pos + 11 + size;
if (anals.anal1.s > oldSize1 )
{
addHeader( &oldSize1,&anals.anal1.s, anals.anal1.buff, anals.seq );
anals.anal1.s++;
oldSize1 = anals.anal1.s;
}
if (anals.anal2.s > oldSize2 )
{
addHeader( &oldSize2,&anals.anal2.s, anals.anal2.buff, anals.seq );
anals.anal2.s++;
oldSize2 = anals.anal2.s;
}
if (anals.anal3.s > oldSize3 )
{
addHeader( &oldSize3,&anals.anal1.s, anals.anal3.buff, anals.seq );
anals.anal3.s++;
oldSize3 = anals.anal1.s;
}
loop++;
}
writeFiles(direntp->d_name, &anals);
}
free(datap);
free(bytesp);
free(anals.anal1.buff);
free(anals.anal2.buff);
free(anals.anal3.buff);
}
最佳答案
在函数 readFilec()
中,free(line);
您已动态分配内存给指针 p2
并尝试释放一个 char指针 line
这是不正确的。
关于c - 由于无效指针而出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838175/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!