Maybe you could help me with the following code...

int main()
{
    char foo = '?';    // foo is created on the stack.
    char* bar = "Hello World";   // where is bar created?

    // will the following result as a NOP or undefined behavior?
    free(bar);

    return 0;
}

What about this:

struct Foo
{
    char* value;
};

struct Foo a = {"Hello from variable a"};  // are a and b created in the .data segment of the asm?
struct Foo b = {"Hello from variable b"};

int main()
{
    // what will happen here:
    struct Foo* ptr = &a;
    free(ptr);   // error?

    // what about this:
    struct Foo* bar = (struct Foo*)malloc(sizeof(struct Foo));
    bar = &b;
    
    bar->value = (char*)malloc(sizeof(char)*2);  // what happened to the the original contents of bar->value?
    bar->value[0] = 'H';
    bar->value[1] = 'i';

    free(bar->value);
    free(bar);  // is this an error?

    return 0;
}

Maybe you could help me with the following code...

int main()
{
    char foo = '?';    // foo is created on the stack.
    char* bar = "Hello World";   // where is bar created?

    // will the following result as a NOP or undefined behavior?
    free(bar);

    return 0;
}

The pointer char* bar is created on the stack...."Hello World" is a literal so its probably created in the read only data section...

As for the behavior...Try it and see what happens.

> As for the behavior...Try it and see what happens.
Bad idea - they might think it works.

@OP
if you didn't call malloc(), then DON'T call free().
if you did call malloc(), then call free() exactly once.

struct Foo
{
    char* value;
};

 // are a and b created in the data segment of the asm. The data is probably is the code segment
struct Foo a = {"Hello from variable a"}; 
struct Foo b = {"Hello from variable b"};

int main()
{
    // what will happen here:
    struct Foo* ptr = &a;
    free(ptr);   // This is will give you an error. The reasons are explained above

    // In the malloc call you allot some memory. The address of this memory is stored in bar
    struct Foo* bar = (struct Foo*)malloc(sizeof(struct Foo));

// Now you store the address of the variable b in the variable bar. The address of the previous memory is lost
    bar = &b;
    
// what happened to the the original contents of bar->value?They got lost
    bar->value = (char*)malloc(sizeof(char)*2);  
    bar->value[0] = 'H';
    bar->value[1] = 'i';

    free(bar->value);
    free(bar); 
 // This is an error because you are trying to free the memory in which the global variable b is stored. As explained above free should only be called when memory is obtained dynamically. You got memory dynamically but lost the address of that memory. So free will give you an error

    return 0;
}

Thanks for your replies guys. Pointers can get confusing for a newbie like myself :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.