C++中字符串指针的初始化问题

dragonfever 2011-04-26 05:37:15
程序运行报错“段错误”
程序源代码粘贴如下:
14 #include "sqlite3.h"
15 void teaform::on_okButtonClicked()
16 {
17 sqlite3* db;
18 char *num,*author,*storage;
19 int rc,nRow,nCol;
20 char **dbResult;
21 char *sql,*errmsg=NULL,*str;

22
23 QString nameText=nameEdit->text();
24 QString resultQString,errMsgQString;
25
26 //open the databse:
27 rc=sqlite3_open("library1.db",&db);
28
29 //open failed:
30 if(rc)
31 {
32 errMsgQString.sprintf("cannot open database: %s\n",sqlite3_errmsg(db));
33 resultEdit->setText(errMsgQString);
34 sqlite3_close(db);
35 }
36
37 //open successed, then do querying:
38 else{
39 strcpy(str,nameText.ascii());
40 sprintf(sql,"select * from book where BookName='%s';",str);
41
42 rc=sqlite3_get_table(db,sql,&dbResult,&nRow,&nCol,&errmsg);
43
44 //get table failed:
45 if(rc!=SQLITE_OK)
46 {
47 resultQString.sprintf("cannot open database: %s\n",errmsg);
48 resultEdit->setText(resultQString);
49 sqlite3_free_table(dbResult);
50 sqlite3_close(db);
51 }
52
53 //get table successfully:
54 else
55 {
56 int j=4;
57 while(j!=8)
58 {
59 switch(j)
60 {
61 case 4: strcpy(num,dbResult[j]);
62 case 5:
63 case 6: strcpy(author,dbResult[j]);
64 case 7: strcpy(storage,dbResult[j]);
65 default: break;
66 }
67 j++;
68 }
69
70 resultQString.sprintf("%s,%s,%s\n",num,author,storage);
71 resultEdit->setText(resultQString);
72 sqlite3_free_table(dbResult);
73 sqlite3_close(db);
74 }
75 }
76 }


make时有警告信息:
.ui/../teaform.ui.h: In member function ‘virtual void teaform::on_okButtonClicked()’:
.ui/../teaform.ui.h:21: warning: ‘str’ may be used uninitialized in this function
.ui/../teaform.ui.h:21: warning: ‘sql’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘num’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘author’ may be used uninitialized in this function
.ui/../teaform.ui.h:18: warning: ‘storage’ may be used uninitialized in this function


大概知道是定义的指针变量方面造成了“段错误”,因此对指针做了初始化,修改后的代码如下:

13 #include "sqlite3.h"
14 #include <cstdlib>
15 using namespace std;

16 void teaform::on_okButtonClicked()
17 {
18 sqlite3* db;
19 char *num=(char*)malloc(10);
20 char *author=(char*)malloc(30);
21 char *storage=(char*)malloc(5);

22 int rc,nRow,nCol;
23 char *sql=(char*)malloc(100);
24 char *str=(char*)malloc(20);

25
26 QString nameText=nameEdit->text();
27 QString resultQString,errMsgQString;
28
29 //open the databse:
30 rc=sqlite3_open("library1.db",&db);
31
32 //open failed:
33 if(rc)
34 {
35 errMsgQString.sprintf("cannot open database: %s\n",sqlite3_errmsg(db));
36 resultEdit->setText(errMsgQString);
37 sqlite3_close(db);
38 }
39
40 //open successed, then do querying:
41 else{
42 strcpy(str,nameText.ascii());
43 sprintf(sql,"select * from book where BookName='%s';",str);
44
45 char **dbResult;
46 char *errmsg=(char*)malloc(100);

47 rc=sqlite3_get_table(db,sql,&dbResult,&nRow,&nCol,&errmsg);
48
49 //get table failed:
50 if(rc!=SQLITE_OK)
51 {
52 resultQString.sprintf("cannot open database: %s\n",errmsg);
53 resultEdit->setText(resultQString);
54 sqlite3_free_table(dbResult);
55 sqlite3_close(db);
56 }
57
58 //get table successfully:
59 else
60 {
61 int j=4;
62 while(j!=8)
63 {
64 switch(j)
65 {
66 case 4: strcpy(num,dbResult[j]);
67 case 5:
68 case 6: strcpy(author,dbResult[j]);
69 case 7: strcpy(storage,dbResult[j]);
70 default: break;
71 }
72 j++;
73 }
74
75 resultQString.sprintf("%s,%s,%s\n",num,author,storage);
76 resultEdit->setText(resultQString);
77 sqlite3_free_table(dbResult);
78 sqlite3_close(db);
79 }
80 free(errmsg);
81 }
82 free(num);
83 free(author);
84 free(storage);
85 free(sql);
86 free(str);

87 }

make时是没有警告信息了,但是运行时仍然报“段错误”
该怎么修改呢,大家帮忙看看。。。
...全文
762 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dragonfever 2011-05-10
  • 打赏
  • 举报
回复
还是解决不了啊。。。
dragonfever 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 anyidan 的回复:]

char *strcpy(s,ct)
copy string ct to string s, including '\0'; return s.


char *strncpy(s,ct,n)
copy at most n characters of string ct to s; return s. Pad with '\0''s if ct has fewer than n cha……
[/Quote]
多谢楼上提醒,我会尝试!
但是,当我输入的字串使程序进行到56行就结束的时候,也就是说程序不会执行strcpy(num,dbResult[j])这些语句时,运行仍然提示“段错误”,断定是前面程序中的问题,怎么造成了内存缓冲区溢出呢?找不出错误出在哪里了。。。
AnYidan 2011-04-27
  • 打赏
  • 举报
回复
char *strcpy(s,ct)
copy string ct to string s, including '\0'; return s.


char *strncpy(s,ct,n)
copy at most n characters of string ct to s; return s. Pad with '\0''s if ct has fewer than n characters.

改用 strncpy, 指定 copy 的字符数量试试
dragonfever 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pathuang68 的回复:]

基本可以确定,是内存访问越界造成的。

LZ不妨先单步调式一下,确定出问题的具体位置。然后看看那条语句是否存在内存访问越界的情况。
[/Quote]
这是我在Linux下作的一QT小工程中的一个槽函数文件teaform.ui.h,整个小工程生成一个可执行文件tea,我尝试用gdb调试,可是因为之前没用过gdb,调试不来,调试情况如下:

root@ubuntu:/home/lf/tea# gdb tea
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/lf/tea/tea...(no debugging symbols found)...done.
(gdb) l
没有符号表被读取。请使用 "file" 命令。
(gdb) file tea
Reading symbols from /home/lf/tea/tea...(no debugging symbols found)...done.
(gdb) l
没有符号表被读取。请使用 "file" 命令。
(gdb)


不知道怎么回事?可以不用gdb调试直接从程序中查找到原因吗?
dragonfever 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hhh_hao 的回复:]

调试下,看下strcpy之前的dbResult的值对不对,确定有8个数据.
[/Quote]
这个应该没有问题,sqlite3数据库book表中设计为4列,dbResult的前四个数据存放的是列名,接下来的四个是列值,要取的也是这四个,如果switch语句中没有错误,这里应该没有问题的
dragonfever 2011-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ouyh12345 的回复:]

越界了吧
char *num=(char*)malloc(10 * sizeof(char));
[/Quote]
按你的建议修改后,编译没有问题,运行时仍然提示段错误。。
xspace_time 2011-04-26
  • 打赏
  • 举报
回复
为什么不把初始化数据改大点呢
pathuang68 2011-04-26
  • 打赏
  • 举报
回复
基本可以确定,是内存访问越界造成的。

LZ不妨先单步调式一下,确定出问题的具体位置。然后看看那条语句是否存在内存访问越界的情况。
hhh_hao 2011-04-26
  • 打赏
  • 举报
回复
调试下,看下strcpy之前的dbResult的值对不对,确定有8个数据.
ouyh12345 2011-04-26
  • 打赏
  • 举报
回复
越界了吧
char *num=(char*)malloc(10 * sizeof(char));

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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