We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,394 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Byte amounts of variabless

Hello everybody,

I have never done anything with dynamic memory allocation and was trying to allocate enough memory to hold four elements of an array. Each of these elements will hold another array that will hold the actual data.

My questions is how can I check the size of a variable to make sure there is enough memory allocated. I am not talking about using an if statement. I would like the actual value of how much memory the variable uses.

//Declaration of variable
int *matrices = NULL;			//Pointer to the set of matrix registers

matrices = (int*) malloc(4 * sizeof(int));
printf("%d\n", *matrices);

I was thinking that if I use printf statement and the dereferencing operator with the variable then I could see the value 16. But I continue to see the number 4. This occurs if I change sizeof(int) to char, float, double, etc. Any ideas would be helpful.

Thanks!

3
Contributors
3
Replies
24 Minutes
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
DmytriE
Newbie Poster
15 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

If the memory manager can fulfill the malloc request it will return a memory pointer to the allocated memory...if it fails it returns NULL.

If malloc returns a pointer other than NULL then you have your requested memory.

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

The OS will not allocate memory, if it can only allocate a portion of the memory you requested. So if matrices is not a NULL pointer after malloc() then you have your 4 ints.

sergent
Posting Pro
598 posts since Apr 2011
Reputation Points: 70
Solved Threads: 23
Skill Endorsements: 0

Thanks!

DmytriE
Newbie Poster
15 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by sergent and gerard4143

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0708 seconds using 2.74MB