Memory allocation with void pointer

M

Maedowan

Does this code delete right amount of memory:

struct SAMPLE{
int a,b,c;
char zzz;
};

void* ptr;
int main()
{
ptr=(void*) new[SAMPLE];
delete ptr; //delete void pointer without casting it
}
 
J

John Harrison

Maedowan said:
Does this code delete right amount of memory:

struct SAMPLE{
int a,b,c;
char zzz;
};

void* ptr;
int main()
{
ptr=(void*) new[SAMPLE];
delete ptr; //delete void pointer without casting it
}

No, the behaviour of this program is completely undefined by the C++
standard. It might be OK, it might not.

john
 
M

Maedowan

John said:
Maedowan said:
Does this code delete right amount of memory:

struct SAMPLE{
int a,b,c;
char zzz;
};

void* ptr;
int main()
{
ptr=(void*) new[SAMPLE];
delete ptr; //delete void pointer without casting it
}

No, the behaviour of this program is completely undefined by the C++
standard. It might be OK, it might not.

but what about free and malloc, they use void* pointers and does it right.
 
M

Maedowan

John said:
free and malloc would be fine. There's no reason you can't use them in a
C++ program.

becouse i don't want to include stdlib, it will incrase size of my
executable. Is this code proper in c++ :

void* ptr=new SAMPLE;
delete (SAMPLE*) ptr; //casting ptr to proper type.
 
J

John Harrison

Maedowan said:
John said:
Maedowan said:
Does this code delete right amount of memory:

struct SAMPLE{
int a,b,c;
char zzz;
};

void* ptr;
int main()
{
ptr=(void*) new[SAMPLE];
delete ptr; //delete void pointer without casting it
}

No, the behaviour of this program is completely undefined by the C++
standard. It might be OK, it might not.

but what about free and malloc, they use void* pointers and does it right.

free and malloc would be fine. There's no reason you can't use them in a C++
program.

john
 
G

Gianni Mariani

Maedowan said:
John Harrison wrote:




becouse i don't want to include stdlib, it will incrase size of my
executable. ...

It is unlikely that including stdlib will increase the size of your
executable at all.

... Is this code proper in c++ :

void* ptr=new SAMPLE;
delete (SAMPLE*) ptr; //casting ptr to proper type.

The snipped above would work fine.

You can also use "operator new".

int main()
{
void * x = operator new( 50 );

operator delete( x );
}
 
J

John Harrison

Maedowan said:
becouse i don't want to include stdlib, it will incrase size of my
executable. Is this code proper in c++ :

void* ptr=new SAMPLE;
delete (SAMPLE*) ptr; //casting ptr to proper type.

That's fine.

Remember that if you allocate an array then you must use delete[] when you
deallocate (this was the other thing wrong with your original code)

void* ptr=new SAMPLE[10];
delete[] (SAMPLE*) ptr; // use delete[] for arrays

john
 
M

Maedowan

John Harrison wrote:

Remember that if you allocate an array then you must use delete[] when you
deallocate (this was the other thing wrong with your original code)

void* ptr=new SAMPLE[10];
delete[] (SAMPLE*) ptr; // use delete[] for arrays

my orginal code wouldn't even compile becouse i wrote like this:
new[SAMPLE], i don't now why i wrote like this but i did it
 
M

Maedowan

Gianni Mariani wrote:

The snipped above would work fine.

You can also use "operator new".

int main()
{
void * x = operator new( 50 );

operator delete( x );
}

that is interesting, doesn't delete use operator delete(), if it calls this
operator i think it's also possible to use delete sample;
 
G

Gianni Mariani

Maedowan wrote:
....
that is interesting, doesn't delete use operator delete(), if it calls this
operator i think it's also possible to use delete sample;

Be more clear about your question - post a code snippet of what you mean
you think you can do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top