简体   繁体   English

memset()没有在c中设置内存

[英]memset() not setting memory in c

I apologize if my formatting is incorrect as this is my first post, I couldn't find a post on the site that dealt with the same issue I am running into. 我很抱歉,如果我的格式不正确,因为这是我的第一篇文章,我在网站上找不到处理我遇到的同一问题的帖子。 I'm using plain C on ubuntu 12.04 server. 我在ubuntu 12.04服务器上使用普通的C语言。 I'm trying to concatenate several strings together into a single string, separated by Ns. 我试图将几个字符串连接成一个字符串,用Ns分隔。 The string sizes and space between strings may vary, however. 但是,字符串之间的字符串大小和空格可能不同。 A struct was made to store the positional data as several integers that can be passed to multiple functions: 构造一个结构来将位置数据存储为几个可以传递给多个函数的整数:

typedef struct pseuInts {  
    int pseuStartPos;  
    int pseuPos;  
    int posDiff;  
    int scafStartPos;  
} pseuInts;  

As well as a string struct: 以及字符串结构:

typedef struct string {  
    char *str;  
    int len;  
} myString;

Since there are break conditions for the concatenated string multiple nodes of a dynamically linked list were assembled containing an identifier and the concatenated string: 由于串联字符串存在中断条件,因此组装了动态链接列表的多个节点,其中包含标识符和连接字符串:

typedef struct entry {  
    myString title;  
    myString seq;  
    struct entry *next;  
} entry;  

The memset call is as follows: 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  
}  

These are all being dereferenced as this else if decision is in a function being called by a function called from main, but the changes to the pseuEntry struct need to be updated in main so as to be passed to another function for further processing. 如果决定是在由main调用的函数调用的函数中,则这些都被解除引用,但是对于pseuEntry结构的更改需要在main中更新,以便传递给另一个函数以进行进一步处理。
I've double checked the numbers being used in pseuInts by inserting some printf commands and they are correct in the positioning of how many Ns need to be added, even as they change between different short strings. 我通过插入一些printf命令仔细检查了pseuInts中使用的数字,并且它们在定位需要添加多少N时是正确的,即使它们在不同的短字符串之间发生变化也是如此。 However, when the program is run the memset only inserts Ns the first time it's called. 但是,当程序运行时,memset仅在第一次调用时插入Ns。 IE: IE:

GATTGT and TAATTTGACT are separated by 4 spaces and they become: GATTGT和TAATTTGACT由4个空格分隔,它们变为:
GATTGTNNNNTAATTTGACT GATTGTNNNNTAATTTGACT

The second time it is called on the same concatenated string it doesn't work though. 第二次在同一个连接字符串上调用它时它不起作用。 IE: IE:
TAATTTGACT and TCTCC are separated by 6 spaces so the long string should become: TAATTTGACT和TCTCC之间用6个空格分隔,因此长字符串应该变为:

GATTGTNNNNTAATTTGACTNNNNNNTCTCC GATTGTNNNNTAATTTGACTNNNNNNTCTCC
but it only shows: 但它只显示:
GATTGTNNNNTAATTTGACTTCTCC GATTGTNNNNTAATTTGACTTCTCC

I've added printfs to display the concatenated string immediately before and after the memset and the they are identical in output. 我已经添加了printfs来显示memset之前和之后的连接字符串,它们在输出中是相同的。
Sometimes the insertion is adding extra character spaces, but not initializing them so they print nonsense (as would be expected). 有时插入会添加额外的字符空间,但不会初始化它们,因此它们会打印废话(如预期的那样)。 IE: IE:

GAATAAANNNNNNNNNNNNNNNNN¬GCTAATG GAATAAANNNNNNNNNNNNNNNNN¬GCTAATG
should be 应该
GAATAAANNNNNNNNNNNNNNNNNGCTAATG GAATAAANNNNNNNNNNNNNNNNNGCTAATG

I've switched the memset with a for or a while loop and I get the same result. 我用for或while循环切换了memset,得到了相同的结果。 I used an intermediate char * to realloc and still get the same result. 我使用了一个中间字符*来重新分配并仍然得到相同的结果。 I'm looking for for suggestions as to where I should look to try and detect the error. 我正在寻找关于我应该尝试检测错误的建议。

If you are okay with considering a completely different approach, I would like to offer this: 如果您考虑采用完全不同的方法,我想提供:

I understand your intent to be: Replace existing spaces between two strings with an equal number of "N"s. 我理解你的意图: 用相同数量的“N”替换两个字符串之间的现有空格。 memset() (and associated memory allocations) is the primary method to perform the concatenations. memset() (和相关的内存分配)是执行连接的主要方法。

The problems you have described with your current concatenation attempts are : 您当前连接尝试所描述的问题是:
1) garbage embedded in resulting string. 1)嵌入在结果字符串中的垃圾。
2) writing "N" in some unintended memory locations. 2)在一些非预期的内存位置写“N”。
3) "N" not being written in other intended memory locations. 3) “N”未写入其他预期的存储位置。

Different approach : 不同的方法

First: verify that the memory allocated to the string being modified is sufficient to contain results 第一:验证分配给正在修改的字符串的内存是否足以包含结果
second: verify all strings to be concatenated are \\0 terminated before attempting concatenation. 第二:验证在连接尝试之前,所有要连接的字符串都是\\0终止。
third: use strcat() , and a for(;;) loop to append all "N"s, and eventually, subsequent strings. 第三 strcat() 使用strcat()for(;;)循环追加所有“N”,最后追加后续字符串。

eg. 例如。

for(i=0;i<numNs;i++)//compute numNs with your existing variables 
{
    strcat(firstStr, "N");//Note: "N" is already NULL term. , and strcat() also ensures null term.
}
strcat(firstStr, lastStr); //a null terminated concatenation

I know this approach is vastly different from what you were doing, but it does address at least the issues identified from your problem statement. 我知道这种方法与你所做的大不相同 ,但它至少解决了你的问题陈述中发现的问题。 If this makes no sense, please let me know and I will address questions as I am able to. 如果这没有意义,请告诉我,我会尽我所能解决问题。 (currently have other projects going on) (目前正在进行其他项目)

Looking at your memset: 看看你的memset:

memset (((*pseuEntry)->seq.str + ((*pseuEntry)->seq.len))), ...

That's the destination. 那是目的地。 Shouldn't it be: 不应该是:

(memset (((*pseuEntry)->seq.str + ((*pseuEntry)->seq.len) + ((*pseuEntry)->seq.pseuStartPos))

Otherwise I'm missing the meaninging of pseuInts . 否则我错过了pseuInts的pseuInts

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

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