LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-13-2008, 06:08 AM   #1
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 202

Rep: Reputation: 32
confirmaion for the allocated memory presence


I am running some code which will start two threads. Both the threads will be working on same list of nodes. First thread will be dynamically inserting and deleting. Second thread will deleting basing on certain parameters I will be providing.

As there are chances to delete the same node twice(once by each thread), I adopted the following procedure:
  1. obtain the lock
  2. ensure that the node(memory) is still existing which is being pointed by the present thread
  3. delete the node.
  4. release the lock

here the complication is in the second line. I thought sizeof operator will be help me. But It is not helping me. I tried the following code:

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        char *ptr=NULL;
        printf("\nsize one : %d",sizeof(ptr));
        printf("\nbefore allocation ptr: %u",ptr);
        ptr=(char*)malloc(10);
        printf("\nafter allocation ptr: %u",ptr);
        printf("\nsize two : %d",sizeof(ptr));
        free(ptr);
        printf("\nafter freeing mem. ptr: %u",ptr);
        printf("\nsize three : %d\n\n",sizeof(ptr));
        return(0);
}
output is as follows:

Quote:
size one : 4
before allocation ptr: 0
after allocation ptr: 153280520
size two : 4
after freeing mem. ptr: 153280520
size three : 4
I want the following output:

Quote:
size one : 0
before allocation ptr: 0
after allocation ptr: 153280520
size two : 10
after freeing mem. ptr: 153280520
size three : 0
Can any body please help me by giving the relevent function call which will serve my purpose.
 
Old 08-13-2008, 10:19 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Why not use shared memory - shmget instead of malloc? - then pass a struct to the threads which has the return value of shmget - a shmid - and also has the size memory allocated - usually one page of memory. Then all of this mess goes away.
 
Old 08-13-2008, 11:46 PM   #3
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 202

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by jim mcnamara View Post
- usually one page of memory.
I have to maintain one lakh nodes.
To me it may NOT be feasible to create one lakh nodes(pages).
Definitely, performance degrades which I can't bare.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RHEL AS4 Update 4 Non release of allocated memory chirantha Linux - Enterprise 3 11-02-2007 06:42 AM
Freeing memory allocated in a function con Programming 4 01-02-2006 03:25 AM
Does Your Old School Have A Web Presence? XavierP General 16 05-24-2005 07:40 AM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 02:56 AM
Boot Hangs with Redhat, GRUB, Total HugeTLB memory allocated, Neochubbz Linux - General 5 08-19-2004 01:27 PM


All times are GMT -5. The time now is 04:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration