C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 23rd, 2005, 03:16 PM
nevillemehta nevillemehta is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: India
Posts: 64 nevillemehta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 44 m 17 sec
Reputation Power: 6
Understanding malloc...

Hello,

Here are some Q's about memory allocation in C++;

Q1.
Code:
#include<iostream>
using namespace std;
void memAlloc(int *i)
{
i=(int*)malloc(sizeof(int));
}
int main()
{
	int *i;
	memAlloc(i);
	*i = 100;
	cout << *i << endl ;
	return 0;
}


Memory is NOT allocated in this case to i. WHY? Does end of scope of function memAlloc mean deallocation of mem automatically? But i thought when we use malloc we have to explicitly deallocate memory?


Q2.
Code:
#include<iostream>
using namespace std;
void memAlloc(int *i)
{
free(i);
}
int main()
{
	int *i;
	i=(int*)malloc(sizeof(int));
	memAlloc(i);
	*i = 100;
	cout << *i << endl ;
	return 0;
}


Memory doesnt seem to have been freed here cause when i cout *i 100 is sucessfully printed

Thanks
Neville

Reply With Quote
  #2  
Old August 24th, 2005, 02:46 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
Q1: I see no reason why that wouldn't work fine. Are you sure that there isn't any memory being allocated? Perhaps try casting malloc() as a void pointer instead?

Q2: The free() function only deallocates the memory, it doesn't clear or reassign it. Thus, sometimes the freed memory will immediately be filled with values for another process, but sometimes they will remain untouched. Thus, after you deallocate memory, it may or may not retain it's value immediately after being freed.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old August 24th, 2005, 05:26 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
Thumbs up

Q1: A nice C-problem caused by pointers. A never ending story, and probably the reason why other languages does not have pointers at all
The anser is 'easy'. In your function, the variable i is a copy of your pointer. Therefore the memory is allocated and i is set to the pointer. However, since it is a 'copy of' it is not returned to main();

Solution (notice the changes in bold):
Code:
void memAlloc(int **i)
{
  *i=(int*)malloc(sizeof(int));
}
int main()
{
	int *i;
	memAlloc(&i);
	*i = 100;
	cout << *i << endl ;
	return 0;
}


Seems wrong? Just remember how to change an int value in a function:
Code:
void change(int *i)
{
	*i=256;
}
int main()
{
	int i = 3;
	change(&i);
	cout << i << endl ;
	return 0;
}



Offcourse in C++ this is much more readable since you can 'pass by reference':
Code:
void change(int& i)
{
	i=256;
}
int main()
{
	int i = 3;
	change(i);
	cout << i << endl ;
	return 0;
}


And your problem in C++, with some validation (ALWAYS do this in C!!!!!!! Also when you're 100% sure that it would work!!!!):
Code:
void memAlloc(int& *i)
{
  i=(int*)malloc(sizeof(int));
  return i;
}
int main()
{
	int *i=NULL;
	if(memAlloc(i) != NULL)
	{
		*i = 100;
		cout << *i << endl ;
	}
	return 0;
}

Reply With Quote
  #4  
Old August 24th, 2005, 05:32 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
Q2: B-Com is correct on this point. A good debugger environment would give you a message/warning on this point.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Understanding malloc...


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT