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
} 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
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.
"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
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.
"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
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 );
}
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
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;
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Steven Powers |
last post by:
Imagine the following setup
class Parent
{
virtual void doStuff();
}
class Child : public Parent
{
virtual void doStuff();
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |