vector.push_back()报错,求助各位大神

U华哥 2012-06-06 11:09:46
bool CCalutionFactor::ParseFactorName(string FactorFileName)
{
char InFileNameBuf[512];
memset(InFileNameBuf, 0, 512);
strcpy(InFileNameBuf, FactorFileName.c_str());

char* p;

p = strtok(InFileNameBuf, ";");

while(p)
{
string NameBuf(p);
printf("NameBUf %s \r\n", NameBuf.c_str());

InFileName.push_back(NameBuf);
p=strtok(NULL,";");
}
return true;
}
这是我写的一段程序,作用是把string按;拆分,然后push到vector中。
以前好好的,昨天突然报错,
错误提示
Unhandled exception at 0x100303b9 (Calculation_Model.dll) in ExeTest.exe: 0xC0000005: Access violation reading location 0x00000014.
我跟进了一下。错误行在vector文件的
size_type size() const
{ // return length of sequence
return (_Mylast - _Myfirst);
}
就是return行。求助各位,帮帮忙啊。
...全文
827 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]

对象,尤其是STL对象,是需要初始化整个STL运行环境和执行其构造函数之后才可用的。
虽然楼主的问题不一定出自这个原因。
[/Quote]

您的意思是要在构造函数里面对vector进行初始化呗?
赵4老师 2012-06-06
  • 打赏
  • 举报
回复
对象,尤其是STL对象,是需要初始化整个STL运行环境和执行其构造函数之后才可用的。
虽然楼主的问题不一定出自这个原因。
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

vector是对象不是一段内存。
[/Quote]

大哥,您的意思是?
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

dll工程?那么你这个包含vector的对象是外面传给这个dll的?
[/Quote]

这个是dll工程内部的对象。这里所有的东西都是dll工程内部的东西。
cryingbee 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

我在函数内部声明容器push就没问题,而往头文件中声明的容器中push就出现这种异常。郁闷。
还有,可否说一下如何看数组越界呢。小弟比较菜!
[/Quote]
就是看一下FactorFileName的长度是不是超过了512(FactorFileName.size())。不过看你程序的行为也不太像这个问题。
你的对象是从哪里来的?在哪里初始化的?
赵4老师 2012-06-06
  • 打赏
  • 举报
回复
vector是对象不是一段内存。
pengzhixi 2012-06-06
  • 打赏
  • 举报
回复
dll工程?那么你这个包含vector的对象是外面传给这个dll的?
U华哥 2012-06-06
  • 打赏
  • 举报
回复
我把头文件中的声明变成了全局声明,也就是在这个源文件的顶部声明,没问题。
现在的问题就是声明部分在头文件就异常。。。为什么呢。
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

这段代码看不出什么问题。看看其它地方是怎么操作InFileName的。
或者会不会是长度512的临时数组越界了?
[/Quote]

我在函数内部声明容器push就没问题,而往头文件中声明的容器中push就出现这种异常。郁闷。
还有,可否说一下如何看数组越界呢。小弟比较菜!
U华哥 2012-06-06
  • 打赏
  • 举报
回复
还有,我在这个函数内部声明容器再push就没问题,我把头文件中声明的容器push就出现这种异常,上火啊。
开始还好好的呢。。。
cryingbee 2012-06-06
  • 打赏
  • 举报
回复
这段代码看不出什么问题。看看其它地方是怎么操作InFileName的。
或者会不会是长度512的临时数组越界了?
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

你先看看你有没有涉及多线程吧
[/Quote]

没有啊,一个win32的dll工程,没有涉及到多线程的修改啊。
pengzhixi 2012-06-06
  • 打赏
  • 举报
回复
你先看看你有没有涉及多线程吧
U华哥 2012-06-06
  • 打赏
  • 举报
回复
大神们,谁给些指点啊。
搞不过去了呀。
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

应该不会吧,看看是不是别的什么地方出了问题?
[/Quote]

我调试了好久,在push_back的时候那个NameBuf是
NameBuf "D:\C_超维创想\鞍山\测试_流域面积.xml" std::basic_string<char,std::char_traits<char>,std::allocator<char> >
这样的。
U华哥 2012-06-06
  • 打赏
  • 举报
回复
对了,可能也不是内存问题,因为我试着在类的构造函数中对容器设置容量,运行还是异常。
pathuang68 2012-06-06
  • 打赏
  • 举报
回复
应该不会吧,看看是不是别的什么地方出了问题?
U华哥 2012-06-06
  • 打赏
  • 举报
回复
大神们,我在线等呢,拜托了!
U华哥 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 的回复:]

C/C++ code
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
char *string,
const char *control1,//……
[/Quote]

神人也!膜拜啊!
赵4老师 2012-06-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
char *string,
const char *control1,//连续出现时视为中间夹空token
const char *control2 //连续出现时视为中间无空token
)
{
unsigned char *str;
const unsigned char *ctrl1 = (const unsigned char *)control1;
const unsigned char *ctrl2 = (const unsigned char *)control2;
unsigned char map1[32],map2[32];
static char *nextoken;
static char flag=0;
unsigned char c;
int L;

memset(map1,0,32);
memset(map2,0,32);
do {
map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));
} while (*ctrl1++);
do {
map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));
} while (*ctrl2++);

if (string) {
if (control2[0]) {
L=strlen(string);
while (1) {
c=string[L-1];
if (map2[c >> 3] & (1 << (c & 7))) {
L--;
string[L]=0;
} else break;
}
}
if (control1[0]) {
L=strlen(string);
c=string[L-1];
if (map1[c >> 3] & (1 << (c & 7))) {
string[L]=control1[0];
string[L+1]=0;
}
}
str=(unsigned char *)string;
}
else str=(unsigned char *)nextoken;

string=(char *)str;
while (1) {
if (0==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
string++;
str++;
} else {
flag=1;
str++;
}
} else if (1==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=0;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=2;
break;
} else str++;
} else {//2==flag
if (!*str) return NULL;
if (map1[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
flag=0;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
} else {
string=(char *)str;
str++;
flag=1;
}
}
}
nextoken=(char *)str;

if (string==(char *)str) return NULL;
else return string;
}
void main()
{
strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, ");
strcpy(seps1,",\n");strcpy(seps2," \t");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");
strcpy(seps1,"|");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"");strcpy(seps2,"|");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"|");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"a,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,",a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,"a,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,",a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,",,a,,b,,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,",");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,",,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}

strcpy(string,",,,");
strcpy(seps1,",");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
}
//
//[A string of ,,tokens
//
//and some more tokens,]
//Tokens:
// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,
//[1234| LIYI|China | 010 |201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,
//[a]
//Tokens:
// <a>,
//[a,b]
//Tokens:
// <a>, <b>,
//[a,,b]
//Tokens:
// <a>, <>, <b>,
//[,a]
//Tokens:
// <>, <a>,
//[a,]
//Tokens:
// <a>, <>,
//[,a,,b]
//Tokens:
// <>, <a>, <>, <b>,
//[,,a,,b,,]
//Tokens:
// <>, <>, <a>, <>, <b>, <>, <>,
//[,]
//Tokens:
// <>, <>,
//[,,]
//Tokens:
// <>, <>, <>,
//[,,,]
//Tokens:
// <>, <>, <>, <>,
加载更多回复(9)

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧