Getting undeclared error for the variable which is already declared.

shajabedishajabedi Posts: 8

I have this piece of code to compile with insure++

static int ABC
(
unsigned char *buffer,
int buffer_size,
int num,
)
{

    memset(buffer, '\0', buffer_size);
    void* xyz = #
    memcpy(buffer, xyz, "ABCD");

''''
but after cmpilation, i get this error:
" error: 'xyz' undeclared (first use in this function)"

but it is already declared.

Comments

  • RichRich Posts: 29 ✭✭

    This code is not valid. memcpy takes three arguments:
    void *memcpy(void *dest, void *src, size_t count);

    You have a string literal for the third argument, instead of the count of number of bytes to copy.

    Also, what platform and compiler are you using?

  • shajabedishajabedi Posts: 8

    Yeah, i was wrong it is
    memcpy(buffer, xyz, 5);
    I am in Linux and using gcc version 4.8.5

  • shajabedishajabedi Posts: 8

    the question is xyz is declared but i am getting undeclared error for it in line memecpy.

  • RichRich Posts: 29 ✭✭
    edited December 2020

    I cannot reproduce this behavior with the information that you've given me so far.
    Please contact our technical support department, ([email protected]). Please include a small, compile-able test-case which reproduces this behavior.

    This is my test-case:

    #include <string.h>

    static void ABC (unsigned char *buffer, int buffer_size, int num)
    {
    memset(buffer, '\0', buffer_size);
    void *xyz = &num;
    memcpy(buffer, xyz, 5);
    }

    This compiles just fine with insure.

  • shajabedishajabedi Posts: 8

    I compiled your test case and I got this error:
    error: 'xyz' undeclared (first use in this function)
    void *xyz = &num;

  • Hi @shajabedi ,

    I could confirm, the test case that @Rich provided compiled just fine with Insure++. I suspect someting wrong with either your setup or environment.
    As @Rich wrote, please, create reproducible test case that compiles with your compiler and reproduces the error, and contact our technical support.

Sign In or Register to comment.