Showing posts with label memory-management. Show all posts
Showing posts with label memory-management. Show all posts

12 September 2011

How to determine the amount of memory required by my char* in struct?

Suppose you have the following C struct:
  1. typedef struct
  2. {
  3.     char* name;
  4. }Emp;
I asked in SO on how to determine the amount of memory to allocate to the char* struct member. and the answer was to use lazy/delayed allocation as of:
  1. void setName(Emp* emp, char* newName)
  2. {
  3.     free(emp->name);
  4.     emp->name = malloc(strlen(newName) + 1);
  5.     strcpy(emp->name, newName);
  6. }
(It seems to me to be the same idea used for Objective-C, the setter is release the previous pointer and then retain (in C to copy) the parameter pointer. So, the whole program will be:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. 
  5. typedef struct
  6. {
  7.     char* name;
  8. }Emp;
  9. 
 10. void init(Emp** emp)
 11. {
 12.     *emp = malloc(sizeof(Emp));
 13.     (*emp)->name = NULL;
 14.     (*emp)->name = malloc(sizeof(char*));
 15. }
 16. 
 17. void release(Emp** emp)
 18. {
 19.     free((*emp)->name);
 20.     free(*emp);
 21. }
 22. 
 23. void setName(Emp* emp, char* newName)
 24. {
 25.     free(emp->name);
 26.     emp->name = malloc(strlen(newName) + 1);
 27.     strcpy(emp->name, newName);
 28. }
 29. char* getName(Emp* emp)
 30. {
 31.     return emp->name;
 32. }
 33. 
 34. int main(void)
 35. {
 36.     Emp* emp;
 37.     init(&emp);
 38.     setName(emp, "Muhammad                              Abdullah");
 39.     printf("%s", getName(emp));
 40.     release(&emp);
 41.     
 42.     return 0;
 43. }

11 September 2011

What if you forgot to deallocate reserved memory in your objective-c (c/c++) programs?

The worst thing ever that I usually do is to forget to deallocates memory for objects I create when working with non-garbage collector languages (like C/C++/Objective C) So, That If I didn't de-allocate the memory .. what in that?? Actually it is a disaster ... Watch this example:
  1. #import <Foundation/Foundation.h>
  2. 
  3. int main(void)
  4. {
  5.     long long i;
  6.     for (i=0; i < 100000000000000; i++)
  7.     {
  8.         NSString* string = [[NSString alloc] initWithString :@"string"];
  9.         //[string release];
 10.     }
 11. 
 12.     [NSThread sleepForTimeInterval: 5];
 13.     
 14.     return 0;
 15. }
Then compile and run with GNUstep, the memory used by the program reaches 2 GB (I've 4 GB of RAM), and then the program terminates printing:
Virtual memory exhausted

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Remember, each time you enter a block of code (method for example) and allocate the miss to deallocate the memory you allocate will add this to the RAM and will not remove..finally you may find your program get crashed because it uses too much memory !