473,324 Members | 1,646 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,324 software developers and data experts.

Memory allocation to a char *

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.
Sep 15 '07 #1
4 1902
ilikepython
844 Expert 512MB
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.
Sep 15 '07 #2
JosAH
11,448 Expert 8TB
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
Sep 15 '07 #3
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
Sep 16 '07 #4
ilikepython
844 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     char s[] = "Hello";
  7.     cout << s << endl;
  8.     strcpy(s, "World");
  9.     cout << s << endl;
  10.     cin.get();
  11.     return 0;
  12. }
  13.  
It works, but this, does not:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     char *s = "Hello";
  7.     cout << s << endl;
  8.     strcpy(s, "World");
  9.     cout << s << endl;
  10.     cin.get();
  11.     return 0;
  12. }
  13.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     char *s = new char [10];
  7.     strcpy(s, "Hello");
  8.     cout << s << endl;
  9.     strcpy(s, "World");
  10.     cout << s << endl;
  11.     cin.get();
  12.     return 0;
  13. }
  14.  
Could someone give a better explanation?

P.S. Of course, all these problems can be solved by using a std::string.
Sep 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
4
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...
6
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...
5
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!)...
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...
17
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...
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...
1
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...
5
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
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...
0
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...
1
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)...
1
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...
0
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

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.