Hi,
How is memory allocated handled in C++ in following case:
int main ()
{
char * s = "Hello"; //line 1
s= "World"; //line 2
s = new char[10]; //line 3
delete[] s; //line 4
}
After assigning "Hello" to s in first line when s is reassigned with "World"
1. is this like a memory leak?
2.In third line when s is dynamically allocated by new then what happens to the memory allocated in second line (s="world";)
Thanks for your help.
4 1902
Hi,
How is memory allocated handled in C++ in following case:
int main ()
{
char * s = "Hello"; //line 1
s= "World"; //line 2
s = new char[10]; //line 3
delete[] s; //line 4
}
After assigning "Hello" to s in first line when s is reassigned with "World"
1. is this like a memory leak?
2.In third line when s is dynamically allocated by new then what happens to the memory allocated in second line (s="world";)
Thanks for your help.
You shouldn't use a pointer unless memory has been allocated. I think when you assign "Hello" and "World" to s, the compiler treats them as memory addresses, so s points to "Hello", not good. New returns a pointer (an address), so, just like before, you assign an address, but the proper one.
You shouldn't use a pointer unless memory has been allocated. I think when you assign "Hello" and "World" to s, the compiler treats them as memory addresses, so s points to "Hello", not good. New returns a pointer (an address), so, just like before, you assign an address, but the proper one.
I don't understand why it would be A Bad Thing (tm) to point to a literal string.
Granted, they're not writable (as per the Standard), but just *pointing* to a
simple literal isn't that bad is it?
kind regards,
Jos
Yes Jos I agree and often I use the literals..that is the reason I want to know how and when the literals are cleaned up or they simply keep filling data segment??
Thanks
Sam
Yes Jos I agree and often I use the literals..that is the reason I want to know how and when the literals are cleaned up or they simply keep filling data segment??
Thanks
Sam
I have my doubts. Look at this program: -
#include <iostream>
-
using namespace std;
-
-
int main()
-
{
-
char s[] = "Hello";
-
cout << s << endl;
-
strcpy(s, "World");
-
cout << s << endl;
-
cin.get();
-
return 0;
-
}
-
It works, but this, does not: -
#include <iostream>
-
using namespace std;
-
-
int main()
-
{
-
char *s = "Hello";
-
cout << s << endl;
-
strcpy(s, "World");
-
cout << s << endl;
-
cin.get();
-
return 0;
-
}
-
I may be wrong, but i think the difference is that in one program memory is allocated while in the other it's not. This works: -
#include <iostream>
-
using namespace std;
-
-
int main()
-
{
-
char *s = new char [10];
-
strcpy(s, "Hello");
-
cout << s << endl;
-
strcpy(s, "World");
-
cout << s << endl;
-
cin.get();
-
return 0;
-
}
-
Could someone give a better explanation?
P.S. Of course, all these problems can be solved by using a std::string.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Greg Baker |
last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to
post code and ask for help? I hope so.. :)
Here's my problem: I am trying problem 127 of the valladolid online...
|
by: kk |
last post by:
Hi all,
i didn't get output in the following code while compiling and executing
with g++ version 3.2.3 it doesn't allocate memory to pointer varaible
(x) in class B. and it gives correct output...
|
by: sangeetha_b |
last post by:
Hello,
I've writen one simple program to automate some manual process. I've
written that in c program. It works fine so far no problem reported on
this. Last week, i get chance to run my program...
|
by: fatted |
last post by:
I'm trying to write a function which splits a string (possibly multiple
times) on a particular character and returns the strings which has been
split. What I have below is kind of (oh dear!)...
|
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: dtschoepe |
last post by:
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable...
|
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: krishna81m |
last post by:
In the following code, I am trying to return a char, a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at...
|
by: cham |
last post by:
Hi,
I am working on c++ in a linux system ( Fedora core 4 ),
kernel version - 2.6.11-1.1369_FC4
gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )
In my code i am creating a vector to store...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |