ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In c++ pointer is of 4 bytes,then how can we access the whole 4gb Ram in linux,I mean how it is mapped to the physical address.
Second is when I write a statement like this:
char *a="tarun";
as "tarun" is temporary ,then how memory is allocated to 'a'.It runs fine when I cout<<a; it prints tarun,just clear me the facts.
Click here to see the post LQ members have rated as the most helpful post in this thread.
In c++ pointer is of 4 bytes,then how can we access the whole 4gb Ram in linux,I mean how it is mapped to the physical address.
Second is when I write a statement like this:
char *a="tarun";
as "tarun" is temporary ,then how memory is allocated to 'a'.It runs fine when I cout<<a; it prints tarun,just clear me the facts.
A pointer occupies 4-bytes on a 32-bit architecture, but 8-bytes on a 64-bit architecture. The kernel takes care of mapping virtual memory to physical memory. AFAIK, when running a user application, physical memory is never addressed directly; the app uses virtual memory. Here's some more info on this topic: http://en.wikipedia.org/wiki/Virtual_memory
As for your second question, the hard-coded string "tarun" is not temporary. It resides in the program space (memory) for the entirety of the life-time of the program. The variable 'a' merely points to this location in memory. And since it appears that you are developing in C++, you can avoid compiler warnings by declaring 'a' as a const char* type. The const is necessary because the "tarun" string cannot be modified.
Last edited by dwhitney67; 09-07-2011 at 05:28 AM.
Second is when I write a statement like this: char *a="tarun";
as "tarun" is temporary ,then how memory is allocated to 'a'.It runs fine when I cout<<a; it prints tarun,just clear me the facts.
The problem with what you wrote is that a points to to a non-const char, which means the string might inadvertently be changed. That would be "undefined behavior", possibly having negative side-effects. You should either use const char *a = "tarun"; or char a[] = "tarun";. The second makes a copy on the stack.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.