【发布时间】:2013-11-25 20:52:37
【问题描述】:
如果我的格式不正确,我深表歉意,因为这是我的第一篇文章,我在网站上找不到处理我遇到的相同问题的帖子。我在 ubuntu 12.04 服务器上使用纯 C。我试图将几个字符串连接成一个字符串,由 Ns 分隔。但是,字符串大小和字符串之间的间距可能会有所不同。制作了一个结构来将位置数据存储为可以传递给多个函数的几个整数:
typedef struct pseuInts {
int pseuStartPos;
int pseuPos;
int posDiff;
int scafStartPos;
} pseuInts;
还有一个字符串结构:
typedef struct string {
char *str;
int len;
} myString;
由于连接字符串存在中断条件,因此将动态链接列表的多个节点组装在一起,其中包含标识符和连接字符串:
typedef struct entry {
myString title;
myString seq;
struct entry *next;
} entry;
memset调用如下:
} else if ((*pseuInts)->pseuPos != (*pseuInts)->scafStartPos) {
(*pseuEntry)->seq.str = realloc ((*pseuEntry)->seq.str, (((*pseuEntry)->seq.len) + (((*pseuInts)->scafStartPos) - ((*pseuInts)->pseuPos)))); //realloc the string being extended to account for the Ns
memset (((*pseuEntry)->seq.str + ((*pseuEntry)->seq.len)), 'N', (((*pseuInts)->scafStartPos) - ((*pseuInts)->pseuPos))); //insert the correct number of Ns
(*pseuEntry)->seq.len += (((*pseuInts)->scafStartPos) - ((*pseuInts)->pseuPos)); //Update the length of the now extended string
(*pseuInts)->pseuPos += (((*pseuInts)->scafStartPos) - ((*pseuInts)->pseuPos)); //update the position values
}
如果决策是在由 main 调用的函数调用的函数中,则这些都被取消引用,但是对 pseuEntry 结构的更改需要在 main 中更新,以便传递给另一个函数以进行进一步处理.
我通过插入一些 printf 命令仔细检查了 pseuInts 中使用的数字,它们在需要添加多少 N 的位置上是正确的,即使它们在不同的短字符串之间发生变化。但是,当程序运行时,memset 仅在第一次调用时插入 Ns。即:
GATTGT 和 TAATTTGACT 用 4 个空格隔开,它们变成:
GATTGTNNNNTAATTTGACT
第二次在相同的连接字符串上调用它却不起作用。即:
TAATTTGACT 和 TCTCC 用 6 个空格隔开,所以长字符串应该变成:
GATTGTNNNNTAATTTGACTNNNNNNTCTCC
但它只显示:
GATTGTNNNNTAATTTGACTTCTCC
我添加了 printfs 以在 memset 之前和之后显示连接的字符串,并且它们在输出中是相同的。
有时插入会添加额外的字符空间,但不会初始化它们,因此它们会打印出废话(正如预期的那样)。即:
GAATAAANNNNNNNNNNNNNNNN-GCTAATG
应该是
GAATAAANNNNNNNNNNNNNNNNNGCTAATG
我已经用 for 或 while 循环切换了 memset,我得到了相同的结果。我使用了一个中间 char * 来重新分配,仍然得到相同的结果。我正在寻找有关我应该在哪里尝试检测错误的建议。
【问题讨论】:
-
很好地描述了问题,只是很难看出所有取消引用的问题。如果您可以提供足够少的代码以便有人可以编译、构建、运行它,那么可能更容易排除故障?更容易看到发生了什么。 sscce
-
这不可能编译。
pseuInts是第一个代码块中的类型,最后一个代码块中的变量。 -
很好奇 - 使用
pseuInts.pseuPos而不是(*pseuInts)->pseuPos会有什么问题? (等人) -
由于我只使用一组通过执行不断更新的 pseuInts,我将其标记为:pseuInts *curPseuInts = malloc (sizeof (*curPseuInts));当它第一次出现时,然后将其传递给这些函数: void updatePseuEntryPosition (entry **pseuEntry, blastnEntry *blastnEntry, string *curChr, pseuInts **pseuInts); void endPseuEntry (entry **entry, string *chr, pseuInts *pseuInts);所以它既是类型又是变量。 (抱歉,我无法让格式适用于评论)
-
当
memset被调用时,scafStartPos and pseuPos的值是多少?