0

I keep getting the above error message for blocks of code that use the assert function and I cannot see why this is. I have searched the Internet for potential answers to this error...without any success.

Below is the aforementioned block of code that throws up this error:

#include <stdio.h>
#include <assert.h>

typedef struct WORD
{
    char *Word;
    size_t Count;
    struct WORD *Left;
    struct WORD *Right;
} WORD;

#define SUCCESS                      0
#define NO_MEMORY_FOR_WORDNODE       3
#define NO_MEMORY_FOR_WORD           4

int AddToTree(WORD **DestTree, size_t *Treecount, char *Word)
{
    int Status = SUCCESS;
    int CompResult = 0;

    /* safety check */
    assert(NULL != DestTree);
    assert(NULL != Treecount);
    assert(NULL != Word);

    /* DestTree is NULL or it isn't */
    if(NULL == *DestTree){ /* this is the place to add it then */
        *DestTree = malloc(sizeof **DestTree);
        if(NULL == *DestTree){
            /* out of memory */
            Status = NO_MEMORY_FOR_WORDNODE;
        }
        else {
            (*DestTree)->Left = NULL;
            (*DestTree)->Right = NULL;
            (*DestTree)->Count = 1;
            (*DestTree)->Word = dupstr(Word);
            if(NULL == (*DestTree)->Word){
                /* out of memory in the middle */
                Status = NO_MEMORY_FOR_WORD;
                free(*DestTree);
                *DestTree = NULL;
            }
            else {
                /* everything worked, add one to the tree nodes count */
                ++*Treecount;
            }
        }
    }
    else { /* we need to make a decision */
        CompResult = strcmp(Word, (*DestTree)->Word);
        if(0 < CompResult){
            Status = AddToTree(&(*DestTree)->Left, Treecount, Word);
        }
        else if(0 > CompResult){
            Status = AddToTree(&(*DestTree)->Left, Treecount, Word);
        }
        else {
            /* add one to the count - this is the same node */
            ++(*DestTree)->Count;
        }
    }  /* end of else we need to make a decision */
    return Status;
}

Please note that each one of the assert lines throws up the same error.

I took alk's advice to check the preprocessor outputs and this is what I get:

# 2 "ex6-4.c" 2
# 1 "c:\\mingw\\include\\assert.h" 1 3
# 38 "c:\\mingw\\include\\assert.h" 3
void __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) _assert (const ch
ar*, const char*, int) __attribute__ ((__noreturn__));
# 16 "ex6-4.c" 2`

I have to admit that this makes as much sense to me as the error in question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JoeDoe
  • 11
  • 3
  • 3
    1) What is `SUCCESS`? 2) Are you sure this is the _exact_ code? – Sourav Ghosh May 24 '16 at 14:49
  • 1
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – R Sahu May 24 '16 at 14:50
  • Hi R Sahu, I am new to this site. Please can you elaborate on what a minimal, complete and verifiable example would look like? – JoeDoe May 24 '16 at 14:56
  • @JoeDoe read the link. Basically we need to be able to cut n paste a basic example that will compile on our systems we we can reproduce the problem – Vorsprung May 24 '16 at 15:00
  • Sourav, SUCCESS is one of the macro used to flag the status of the progress being made when building the tree. Yes it is. But, please find below the full code block, if it helps. – JoeDoe May 24 '16 at 15:03
  • int AddToTree(WORD **DestTree, size_t *Treecount, char *Word) { int Status = SUCCESS; int CompResult = 0; /* safety check */ assert(NULL != DestTree); assert(NULL != Treecount); assert(NULL != Word); /* ok, either *DestTree is NULL or it isn't (deep huh?) */ if(NULL == *DestTree){ /* this is the place to add it then */ *DestTree = malloc(sizeof **DestTree); if(NULL == *DestTree){ /* horrible - we're out of memory */ Status = NO_MEMORY_FOR_WORDNODE; } – JoeDoe May 24 '16 at 15:07
  • Correction: A bit of more of the code block, as it exceeds the allocated character count! – JoeDoe May 24 '16 at 15:07
  • @JoeDoe Don't put that much code in a comment - update your question with the minimal, complete verifiable example. – Random Davis May 24 '16 at 15:08
  • R Sahu, I feel that my question meets each requirement to an extent ... although it may not meet them completely. – JoeDoe May 24 '16 at 15:12
  • Thanks @Random Davis. Will do that. Still getting accustomed to this site. – JoeDoe May 24 '16 at 15:12
  • @Vosprung, that's for the clarification. It's very much appreciated. My programming skills is still in its formative stage and I'm by no means, an expert. The code that I'm having this issue with is not my own, but one that I'm trying to get to work. However, I'd like to think that one day I'd be able to take someone else's code and create a basic example of it. – JoeDoe May 24 '16 at 15:17
  • You get this error message three times, one time for each `assert`? – alk May 24 '16 at 16:52
  • You want to look at the pre-processor output. – alk May 24 '16 at 16:53
  • Yes, that is correct @alk – JoeDoe May 25 '16 at 13:32
  • Thanks for the suggestion @alk. Took your advice and this is what I get: – JoeDoe May 25 '16 at 13:37
  • You mostly likely got much more from the pre-processor than what you show. The interesting part would be the lines following `int CompResult = 0;`. The latter is expected to be some where in `cpp`'s output. – alk May 25 '16 at 13:59
  • That is true @alk. But, I wasn't sure which part of the output has the most relevance. That was my initial instinct; but I've included the block of code now to hopefully paint a clearer picture. Your last comment is interesting though, because I'm trying to compile it as a c code! – JoeDoe May 25 '16 at 14:18
  • 1
    `cpp` is the C-PreProcessor, not the CPlusPlus compiler ... ;-) – alk May 25 '16 at 14:24
  • Oops @alk. However, I do not see those line in the cpp's output, aside from the duplication of the code that follows afterwards. – JoeDoe May 25 '16 at 15:24
  • Make a new project, add an emoty main function, add a single call to assert to it. Type it all afresh, don't copy and paste anything. Does the error reproduce? – n. m. could be an AI May 25 '16 at 15:34
  • @n.m. Tried what you suggested and it also reproduced the same error! – JoeDoe May 26 '16 at 06:00
  • OK so now you really need to look at the preprocessed file (the new small .c file), but this time at the entire file, not just the beginning. Perhaps your mingw installation is broken and you need to reinstall. – n. m. could be an AI May 26 '16 at 06:44
  • "`stray '#'`" is a common error (associated with using macros(?). Perhaps the macro for "assert" in this case?) What is the canonical question? Sample: *[error: stray '\' in program in macro definition](https://stackoverflow.com/questions/23166975/)* (also has "`error: stray '#' in program`"). Related: *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332/)*. A candidate: *[How can I make a char string from a C macro's value?](https://stackoverflow.com/questions/195975/)* (has *"The syntax error ... the problem is a 'stray #' in the source"*) – Peter Mortensen May 20 '23 at 13:11
  • What happens if you swap the condition round and do “destTree != NULL”? – Neil Townsend May 20 '23 at 13:59
  • 1
    I took the exact code above and (having corrected the missing includes and changed dupstr -> strdup) ran it on rextester.com where it compiled absolutely fine. – Neil Townsend May 20 '23 at 14:26

0 Answers0