473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Memory allocation with void pointer

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
}
Jul 22 '05 #1
9 2201

"Maedowan" <Ma******@gaz.pl> wrote in message
news:ch**********@inews.gazeta.pl...
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
Jul 22 '05 #2
John Harrison wrote:

"Maedowan" <Ma******@gaz.pl> wrote in message
news:ch**********@inews.gazeta.pl...
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.
Jul 22 '05 #3

"Maedowan" <Ma******@gaz.pl> wrote in message
news:ch**********@inews.gazeta.pl...
John Harrison wrote:

"Maedowan" <Ma******@gaz.pl> wrote in message
news:ch**********@inews.gazeta.pl...
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
Jul 22 '05 #4
John Harrison wrote:

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.
Jul 22 '05 #5

"Maedowan" <Ma******@gaz.pl> wrote in message
news:ch**********@inews.gazeta.pl...
John Harrison wrote:

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.


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
Jul 22 '05 #6
Maedowan wrote:
John Harrison wrote:

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. ...


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 );
}
Jul 22 '05 #7
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
Jul 22 '05 #8
Gianni Mariani wrote:

... 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 );
}


that is interesting, doesn't delete use operator delete(), if it calls this
operator i think it's also possible to use delete sample;
Jul 22 '05 #9
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.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: jason | last post by:
i'm a little new to VC++, so i'm curious how to appropriate perform the following task. there is a pointer which i wish you point to a buffer of frequently changing size. i'm wondering what is...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
1
by: Brandon Langley | last post by:
I have this structure that I am using in conjunction with NetLocalGroupAddMembers: public struct LOCALGROUP_MEMBERS_INFO_3 { public string lgrmi3_domainandname; } I am having failures...
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.