0

I couldn't figure out why but this is my code :

    char expression[256] = {};
    cin >> expression;
    cout << endl;
    int** variableTable = NULL;
    int numOfVals = getNumberOfVariables(expression);
    variableTable = static_cast<int**>(calloc(numOfVals, sizeof(int*)));
    for (int i = 0; i < numOfVals; i++)
    {
        variableTable[i] = static_cast<int*>(calloc(2, sizeof(int)));
    }
    fillPromoterTable(expression, variableTable, numOfVals);

this is the fillPromoterTable

void fillPromoterTable(const char* expression, int** variableTable, int numOfVals)
{
    char promoter[15] = {};
    char *token;
    char* expCpy = pcstrdup(expression);
    for (int i = 0; numOfVals; i++)
    {
        token = strtok(expCpy, "+-*/");
        int nLen = istrlen(token);
        for (int j = 0; j < nLen; j++)
        {
            if (isdigit(token[j]))
                promoter[j] = token[j];
            if (isalpha(token[j]))
                break;
        }
        variableTable[i][0] = atoi(promoter);
        memset(promoter, '\0', 15);
        token = strtok(NULL, "+-*/");
    }
    free(token);
    free(expCpy);

}

in this line :

variableTable[i][0] = atoi(promoter);

I get an error saying I'm trying to write to address 0xFDFDFDFD I couldn't figure out why it happens I could use some help.

Tugal.44
  • 153
  • 4
  • 13
  • 2
    This looks more like C than C++, you might want to change the tag. – barak manos Sep 23 '14 at 19:28
  • 1
    0xFDFD is the pattern used by Microsoft C++ debug allocator to mark no-man's land boundaries between allocations. So you are dereferencing beyond the end of an allocation, assuming it's a pointer, and dereferencing that. http://en.wikipedia.org/wiki/Magic_number_(programming) – Adrian McCarthy Sep 23 '14 at 19:31
  • 1
    The value probably indicates for an uninitialized or dangling pointer. – πάντα ῥεῖ Sep 23 '14 at 19:31
  • 2
    @barakmanos: Does it? **This is clearly C++** and not C, please look again: `static_cast`, `cin`, `cout` ... – Deduplicator Sep 23 '14 at 19:31
  • Put [**this question**](http://stackoverflow.com/a/127404/1322972) on your favorites list. – WhozCraig Sep 23 '14 at 19:35
  • @Deduplicator , maybe static_cast and cout <<... but for the rest it is really not standard C++. `free` instead of `delete`, `calloc` instead of `new`, C-style arrays instead of `std::vector`, pointers instead of references. **Clearly not C++ style: C style**. – Stephane Rolland Sep 23 '14 at 19:35
  • 1
    @StephaneRolland more to the point, its not standard C++ - *only* library based. There isn't anything in this code that isn't part of the C++ standard library (but plenty that is *also* in the C standard library, which I curse the "move to c++ at your own pace" pundits for daily). None the less, its still valid (and *language* - standard) C++. – WhozCraig Sep 23 '14 at 19:40
  • @WhozCraig that's why I wrote in bold **":C style"**. It may be opinion based, but I think it's important to say that it is **not C++ style**. – Stephane Rolland Sep 24 '14 at 07:54
  • @StephaneRolland your addendum to your comment of *everything* after "..not standard C++." wasn't there at the time my comment was submitted. It wasn't added until *after* (or at best *during*) my comment post. otherwise I wouldn't have said a thing. – WhozCraig Sep 24 '14 at 07:58

1 Answers1

7

Change the for loop condition in the function fillPromoterTable

for (int i = 0; numOfVals; i++)

To:

for (int i = 0; i < numOfVals; i++)
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
Hemant Gangwar
  • 2,172
  • 15
  • 27