开发者

C programming problem unrelated variable being overwritten after STRCPY is used

The problem is the variable nodeType is being changed after STRCPY finishes running. nodeType is not a variable that is related to any of the other variables being used in the STRCPY call. It is in the same struct though. nodeType is a enum with the int value of 3.

Below if the following information that is probably most relevant. I removed most of the variables from the enum and struct so they aren't visible, I assume they aren't relevant to the problem. I am coding in VS2010 if that makes a difference. This is part of an assignment to create a btree, but the question I have has nothing to do with btrees.

enum NODETYPE
{ 
  ROOTLEAF
};
typedef struct node
{
  char key[MAX_CHILDREN_ROOT][MAX_KEY_LENGTH];
  NODETYPE nodeType;
} nodeT;

... part of the insertElement function

for (int i = 0; i < (b->tempNode->numberOfKeys - b->searchData.position); i++)
{
  strcpy(b->tempNode->key[b->tempNode->numberOfKeys - i],  b->tempNode->key[b->tempNode->numberOfKeys - (i + 1)]);
}

.. the function I called was the to insert an element. I have called the function several times prior to this but this is the first this section of code below is being run. After STRCPY runs nodeType takes the value 1280070990. I set the variable to watch and while its in CXX0017 error, which I assume only means its not in scope.

I looked up the number 1280070990 which spawned a bunch of game related issues from various engines. I'd guess its a memory address problem.

Solution was a provided below. It was a simple error of writing outside of the array boundary. I broke the old code down like this and then walked through it. This allowed me to identify the problem which was me writing outside of the key array key.

        for (int i = 0; i < (b->tempNode->numberOfKeys - b->searchData.position); i++)
    {
        int sourceI = b->tempNode->numberOfKeys - i - 2;
        int destI = b->tempNode->numberOfKeys - i - 1;
        char *Source = b->tempNode->key[sourceI];
        char *Dest = b->tempNode->key[destI];
        strcpy(Dest, Source);
    }

A full look at the struct that both variables were in.

typedef struct node
{
    node *childrenPTR[MAX_CHILDREN_ROOT]; 
    int depth; //Distance from root to node
    char key[MAX_CHILDREN_ROOT][MAX_KEY_LENGTH];
    NODETYPE nodeType;
    int numberOfChildren;
    int numberOfKeys;
    node *parentPTR;
} nodeT;

We can see from the order of the variables in the struct that nodeType follows key. I believe C will allocate the memory in 开发者_运维百科this same order. So I could of looked here as well to identify the problem.


You're copying a string that's too long into key[] somewhere. It's overflowing into nodeType.

Since I'm guessing you're new at this, I'd re-code this for a bit easier reading for you to get a handle on what's going on. Perhaps something like:

for (int i = 0; i < (b->tempNode->numberOfKeys - b->searchData.position); i++)
{
  char *dest = b->tempNode->key[b->tempNode->numberOfKeys - i];
  char *source = b->tempNode->key[b->tempNode->numberOfKeys - (i + 1)];
  strcpy(dest, source);
}

Walk that through a debugger and source will probably be longer than (with the null terminator -- you are null terminating your strings right?) MAX_KEY_LENGTH somewhere in your array. It might even be something crazy not even in your array.

Break it down into something smaller and easier to debug in discreet steps.


This is exactly what happens if you use a str* function, which expects a null terminated string, on something that isn't a null terminated string. Or if the thing you're copying into isn't big enough for the thing you're copying into it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜