C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 30th, 2012, 04:23 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
Question about passing pointer to function

i'm reading about memory allocation, and i saw an example of a wrong way and a right way to allocate memory from functions.

i have a question about it:

why is this wrong:
first way:
Code:
void func(int *p)
{
	p = (int *) malloc(3*sizeof(int));
}

void main()
{
	int *pm = NULL;
	func(pm);
}



and this is right:
second way:
Code:
void func(int **p)
{
	*p = (int *) malloc(3*sizeof(int));
}

void main()
{
	int *pm = NULL;
	func(&pm);
}


why isn't the allocated memory using the first way accessible?
doesn't p hold pm's address?

thanks in advanced!

Reply With Quote
  #2  
Old December 30th, 2012, 07:51 AM
jameer jameer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 145 jameer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 17 m 49 sec
Reputation Power: 4
When you declare local variables in a function, there are two actions performed by the compiler. One is declaring the variable of that type, second one is initializing the value for the variable.

In your example, the first program can't send any values to the function ie (pm) has no value. However, in the second case the address of the pointer is passed to the function and double declared here is initialized.
__________________
ASP.NET Hosting|SQL Server Hosting

Reply With Quote
  #3  
Old December 30th, 2012, 08:11 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,683 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 2 h 49 m 16 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
But in the first example pm does have a value. NULL is a value, not the absence of one.

In the first example pm is a number (currently 0). It represents a location in memory (currently an invalid location) but it is still a number. And the normal rules when calling a function with parameters still apply: each value is a copy of the original. pm in main() will be copied to p in func. Modifying p is useless because you're modifying the copy.

The second example has func() receiving a copy of &pm, but that's fine because that copy has the same value and it's the value we actually care about: a location in memory. The function can then go off to that location and do whatever it wants (such as treat it as a pointer and modify the value being pointed to). There you're not actually modifying the copy itself.

ASCII art.
Code:
First:

       +-----------+
main   | pm = NULL |
       +-----------+
------------ V --------
       +-----------+
func   | p  = NULL |
       +-----------+


Second:

       +-----+    +-----------+
main   | &pm | -> | pm = NULL |
       +-----+    +------------
--------- V ------- ^ ------------
       +-----+      |
func   |  p  | -----+ *p
       +-----+

Reply With Quote
  #4  
Old December 30th, 2012, 09:08 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
great explanation.
thanks!

Reply With Quote
  #5  
Old December 30th, 2012, 09:53 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
and just want to make sure i got it right:



is that pretty much the different?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Question about passing pointer to function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap