Question : memory

Consider the following application in C/C++

......
......
for (i = 0 1 < 10; i++ )
 {
     char *str = (char *)malloc(sizeof(char) * 1024 * 1024);
     ..... // some lines of code...
     .....
    free(str);
}
.......
.......

While the application is running inside the for loop, it allocates 1 MB of data and free it at the end of loop.
Once the control is out of for loop, the memory that was allocated and freed, is that memory re-usable by any other later malloc. Or does it create memory fragment and it is unused during the rest of the execution.

Answer : memory

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
CREATE VIEW TestView
AS
with CTE as (
	SELECT  GCT.GiftCertificateTypeName, 
		DATEPART(day, GCH.GiftHistDateAdded) AS [day], 
		DATEPART(month, GCH.GiftHistDateAdded) AS [month], 
		DATEPART(year, GCH.GiftHistDateAdded) AS [year],
		case when b.GiftCodeID is null then 'New Laird Or Lady' else 'Old Laird or Lady' end as SideName
 
	FROM	dbo.GiftCertificateHistory AS GCH INNER JOIN
                      dbo.GiftCertificate AS GC ON GCH.GiftCodeID = GC.GiftCodeID INNER JOIN
                      dbo.GiftCertificateType AS GCT ON GC.GiftCertificateTypeID = GCT.GiftCertificateTypeID  
	left join (
		SELECT GiftCodeID from GiftCertificateHistory Where STATUSID='CODE_CREATED' AND GIFTHISTDATEADDED <='2010-05-20'
	) b on GCH.GiftCodeID = b.GiftCodeID

	WHERE     (GCH.StatusID = 'CERT_SENT')  AND 
        	  (GCH.GiftCodeID LIKE 'LAIRD%') AND
		(GCH.GiftHistDateAdded >= DATEADD(dd, - 6, CAST(CONVERT(VARCHAR(10), GETDATE(), 120) AS DATETIME))) AND 
		(GCH.GiftHistDateAdded < GETDATE())
)
select 	GiftCertificateTypeName, 
	[day],
	[month],
	[year],
	SideName,	
	count(GiftCertificateTypeName) as NumCertsSent
from CTE
group by	GiftCertificateTypeName, 
		[day],
		[month],
		[year],
		SideName
Random Solutions  
 
programming4us programming4us