-3

I wonder that is it possible to check if a variable is still in scope in c or if a pointer points to a variable that is out of scope. What I ultimately want to do is check the pointers that and if they point to a variable that is out of scope then drop the pointer by calling free. so if you guys could help me I would be more than happy. thank you all for you contrubutions.

procoder35
  • 130
  • 6
  • 4
    Calling `free` on a dangling pointer invokes *undefined behavior*. And no, there is no real way to check whether a pointer points to a valid object (or an already freed piece of memory) – UnholySheep Dec 11 '21 at 20:18
  • What do you mean by out of scope exactly? That it's value (address) does not point to an user allocated variable? If you just have a random pointer and want to check if it points to a variable, there is no way to do that unless you manually keep track that it when you assign a variable address to it, eg. structure with the actual pointer variable and a int which only holds 0 or 1 when you assign that pointer an address. – David Hožič Dec 11 '21 at 20:19
  • 1
    If you plan to use `free` then this is allocated memory with `malloc` and it is always "in scope". – i486 Dec 11 '21 at 20:20
  • Answer: No, it's impossible. Period. – Support Ukraine Dec 11 '21 at 20:21
  • 3
    XY problem. Simple program carefully. – 0___________ Dec 11 '21 at 20:28
  • Does this answer your question? [Checking if a pointer is allocated memory or not](https://stackoverflow.com/questions/1576300/checking-if-a-pointer-is-allocated-memory-or-not) – c1moore Dec 11 '21 at 20:32
  • It depends on what you're willing to do. Here's an answer I have on a similar question (https://stackoverflow.com/a/36830470/3263719), along with other suggestions. – c1moore Dec 11 '21 at 20:33

1 Answers1

1

EDIT: You could also skip the structure part and just set the pointer to NULL with macros and then check if it's NULL with a macro;

void some_function(int* input)
{
    if (CHECK_POINTER(input))
    {
        *input = 50;
    }

};


int main()
{
    
    int* point ;
    CLEAR_POINTER(point);
    int a=-1;
    

    some_function(point);
    printf("%d\n", a);

    ASSIGN_POINTER(point, &a);
    some_function(point);
    printf("%d\n", a);

}

OLD: If you are trying to keep track if the pointer is assigned to a certain variable, you could use a structure that contains the pointer variable itself and a variable that is either 0 or 1 when the pointer has been assigned to certain variable.

You can then use macros to assign pointer, clear pointer or check if pointer is assigned a variable address;

#include <stdio.h>

#define DEFINE_POINTER_DATA_STRUCTURE(data_type)\
typedef struct \
{ \
    int is_assigned; \
    data_type *pointer; \
}PDS_##data_type;

#define POINTER_DATA_STRUCTURE(data_type) PDS_##data_type

// The above allows you to have custom types


DEFINE_POINTER_DATA_STRUCTURE(int) // Define a struct of int pointer


#define ASSIGN_POINTER(structure, address) structure.pointer = address; structure.is_assigned = 1;

#define CLEAR_POINTER(structure) structure.pointer = 0x00; structure.is_assigned = 0;

#define CHECK_POINTER(structure)    structure.is_assigned

#define GET_POINTER(structure)  structure.pointer


void some_function(POINTER_DATA_STRUCTURE(int) input)
{
    if (CHECK_POINTER(input))
    {
        *GET_POINTER(input) = 50;
    }

};


int main()
{
    
    POINTER_DATA_STRUCTURE(int) pointer_structure;
    CLEAR_POINTER(pointer_structure);
    int a=-1;
    

    some_function(pointer_structure);
    printf("%d\n", a);

    ASSIGN_POINTER(pointer_structure, &a);
    some_function(pointer_structure);
    printf("%d\n", a);

}
David Hožič
  • 235
  • 2
  • 7