[algogeeks] Re: memory allocation question
p is a pointer to an array of 4 integers. So when you do (int(*) [col])malloc(row*sizeof(*p)) total of 48 bytes is allocated as sizeof(*p) is 12 bytes. On Sep 3, 4:14 pm, rohit rajuljain...@gmail.com wrote: how many bytes are allocated by following code? #includealloc.h #define col 4 #define row 3 int main() { int(*p)[col]; p=(int(*)[col])malloc(row*sizeof(*p)); return 0; } please explain answer? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: memory allocation question
+1 On Sat, Sep 3, 2011 at 8:57 PM, Ankuj Gupta ankuj2...@gmail.com wrote: p is a pointer to an array of 4 integers. So when you do (int(*) [col])malloc(row*sizeof(*p)) total of 48 bytes is allocated as sizeof(*p) is 12 bytes. On Sep 3, 4:14 pm, rohit rajuljain...@gmail.com wrote: how many bytes are allocated by following code? #includealloc.h #define col 4 #define row 3 int main() { int(*p)[col]; p=(int(*)[col])malloc(row*sizeof(*p)); return 0; } please explain answer? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- U.D.I.T Sent by Nokia OVI (c) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: memory allocation question
no. of rows is 3 size of (*p) is 12 then how can 48 be the no. of bytes ?? On Sat, Sep 3, 2011 at 8:32 AM, Deepak Garg deepakgarg...@gmail.com wrote: +1 On Sat, Sep 3, 2011 at 8:57 PM, Ankuj Gupta ankuj2...@gmail.com wrote: p is a pointer to an array of 4 integers. So when you do (int(*) [col])malloc(row*sizeof(*p)) total of 48 bytes is allocated as sizeof(*p) is 12 bytes. On Sep 3, 4:14 pm, rohit rajuljain...@gmail.com wrote: how many bytes are allocated by following code? #includealloc.h #define col 4 #define row 3 int main() { int(*p)[col]; p=(int(*)[col])malloc(row*sizeof(*p)); return 0; } please explain answer? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- U.D.I.T Sent by Nokia OVI (c) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Piyush Agarwal Final Year Undergraduate Department of Computer Engineering Malaviya National Institute of Technology Jaipur -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: memory allocation question
int(*p)[col]; Here, p is a pointer to an array of 4 integers. Size of *p is 4X4 = 16. p=(int(*)[col])malloc(row*sizeof(*p)); here clearly 3X 16 = 48 bytes of memory is being allocated. On Sat, Sep 3, 2011 at 9:14 PM, piyush agarwal pshagl...@gmail.com wrote: no. of rows is 3 size of (*p) is 12 then how can 48 be the no. of bytes ?? On Sat, Sep 3, 2011 at 8:32 AM, Deepak Garg deepakgarg...@gmail.comwrote: +1 On Sat, Sep 3, 2011 at 8:57 PM, Ankuj Gupta ankuj2...@gmail.com wrote: p is a pointer to an array of 4 integers. So when you do (int(*) [col])malloc(row*sizeof(*p)) total of 48 bytes is allocated as sizeof(*p) is 12 bytes. On Sep 3, 4:14 pm, rohit rajuljain...@gmail.com wrote: how many bytes are allocated by following code? #includealloc.h #define col 4 #define row 3 int main() { int(*p)[col]; p=(int(*)[col])malloc(row*sizeof(*p)); return 0; } please explain answer? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- U.D.I.T Sent by Nokia OVI (c) -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Piyush Agarwal Final Year Undergraduate Department of Computer Engineering Malaviya National Institute of Technology Jaipur -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Nitin Garg Personality can open doors... but only Character can keep them open -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: memory allocation
heap memory is allocated using new/malloc. memory from stack is allocated for local variables/objects for a function. Correct me if I am wrong. On Aug 31, 6:37 pm, ravi maggon maggonr...@gmail.com wrote: What memory is allocated to a function, to a variable, to a object created at run time using new or malloc and to a function containing a object with run time memory allocation. -- Regards Ravi Maggon Final Year, B.E. CSE Thapar University -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: memory allocation
new/malloc/calloc : Heap Memory. Sanju :) On Wed, Aug 31, 2011 at 7:00 AM, Abhishek Mallick abhishek.mallick2...@gmail.com wrote: heap memory is allocated using new/malloc. memory from stack is allocated for local variables/objects for a function. Correct me if I am wrong. On Aug 31, 6:37 pm, ravi maggon maggonr...@gmail.com wrote: What memory is allocated to a function, to a variable, to a object created at run time using new or malloc and to a function containing a object with run time memory allocation. -- Regards Ravi Maggon Final Year, B.E. CSE Thapar University -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: memory allocation
Create your own malloc() free() functions. when you allocate memory, allocate a little bit extra memory to add a token of your choosing. When free'ing the memory, check that the token is still there. You are going to want to add the token to the beginning of the memory, and not at the end for 2 reasons: 1. When free'ing, you don't know how much memory was allocated, so it would be difficult to find the end of the memory. 2. If there was a buffer overflow error in the application, it would likely overwrite your token if it were at the end. The other thing to consider is that depending on your compiler flags, memory allocations typically occur at word boundaries to help improve speed. Therefore, it would be helpful for the token added to be a boundary supported by your compiler. It is still possible that if there is a buffer overflow error in your application, the token can still be wiped out. Below are some sample functions that could implement this algorithm. I used the address of the memory allocated as my token. I take the MAX between sizeof(void*) and sizeof(long long) to ensure that the token size will be compatible with the alignment of most architectures. However, you might want to tune this to work better (or to work at all) for your architecture. #define MAX(a,b) (a b ? a : b) #define TOKEN_LEN MAX(sizeof(void*), sizeof(long long)) void *my_malloc(size_t size) { void *p = malloc(size + TOKEN_LEN); if(p != NULL) { ((void*)*p) = p; p += TOKEN_LEN; } return p; } void *my_free(void *p) { if(p!=NULL) { p -= TOKEN_LEN; if( ((void*)*p) != p ) // ERROR: Trying to free memory not allocated by my_malloc() -OR- there was a buffer overflow error else free(p); } else { // ERROR: Trying to free NULL } } On Jul 24, 2:08 pm, dreamer igolalal...@gmail.com wrote: Write a code that will check whether the memory allotted to the program at the initial and the memory returned to the system is same or not. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.