LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2011, 02:49 AM   #1
tarunchawla
Member
 
Registered: Mar 2010
Location: New Delhi,India
Distribution: Ubuntu 14.04
Posts: 117

Rep: Reputation: 3
c++ pointers


I have two questions:

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.
Old 09-07-2011, 05:25 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by tarunchawla View Post
I have two questions:

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.
 
3 members found this post helpful.
Old 09-07-2011, 06:22 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by tarunchawla View Post
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.
That is similar to the following:
Code:
static const char a_static[] = "tarun";
char *a = a_static;
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers Rameses Linux - Newbie 1 07-26-2010 12:03 PM
A few pointers please Olmy Linux - Newbie 4 05-04-2010 07:25 AM
Looking for some pointers... p3ngu!n Linux - Newbie 17 10-30-2003 06:46 AM
pointers in C again h/w Programming 9 10-29-2003 11:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration