473,396 Members | 2,111 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.

Questions on dynamic allocation

In this code:

void f()
{
char* s1 = new char;
s1 = "fred";

char* s2 = new char[4];
s2 = "fred";
}

What is the difference between s1 and s2?
When I should use new[]?
Why if I append "delete[] s1" I get: "*** glibc detected *** ./foo:
free(): invalid pointer: 0x0804874c ***" ?

I know that seems stupid, but it's not clear for me.
Thanks.

s.

Feb 7 '07 #1
3 1530
gallows wrote:
In this code:

void f()
{
char* s1 = new char;
allocates a single character.
s1 = "fred";
memory leak. You've lost the pointer you allocated with new, s2 now
points to the first character of the string literal "fed".

>
char* s2 = new char[4];
you've allocated four characters.
s2 = "fred";
memory leak. You've lost the pointer you allocated with new, s2 now
points to the first character of the string literal "fred".
}

What is the difference between s1 and s2?
When I should use new[]?
You want to use new[] when you allocate multiple items.
Why if I append "delete[] s1" I get: "*** glibc detected *** ./foo:
free(): invalid pointer: 0x0804874c ***" ?
Because s1 is not allocated with new[], it isn't even allocated anymore,
it's pointing to a string literal.
>
I know that seems stupid, but it's not clear for me.
Thanks.

You're better off not using new and delete for this. Use std::string,
or std::vector.
Feb 7 '07 #2
On 7 Feb, 17:59, red floyd <no.s...@here.dudewrote:
-- snip --
Because s1 is not allocated with new[], it isn't even allocated anymore,
it's pointing to a string literal.
Oh, _now_ it's clear. Thanks.
>

You're better off not using new and delete for this. Use std::string,
or std::vector.
I will do, thanks.

Feb 7 '07 #3
gallows wrote:
On 7 Feb, 17:59, red floyd <no.s...@here.dudewrote:
>-- snip --
Because s1 is not allocated with new[], it isn't even allocated anymore,
it's pointing to a string literal.

Oh, _now_ it's clear. Thanks.
>>
You're better off not using new and delete for this. Use std::string,
or std::vector.

I will do, thanks.
You're welcome. I hope my explanation was moderately clear -- I usually
start out with an idea of what I want to explain, but fail miserably. :)

Feb 7 '07 #4

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

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
23
by: TefJlives | last post by:
Hi all, I'm learning a bit about C, and I have a few questions. I'm not trying to insult C or anything with these questions, they're just honestly things I don't get. It seems like pointers...
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...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.