Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-13-2008, 07:08 AM   #1
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
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:

#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);
        printf("\nafter allocation ptr: %u",ptr);
        printf("\nsize two : %d",sizeof(ptr));
        printf("\nafter freeing mem. ptr: %u",ptr);
        printf("\nsize three : %d\n\n",sizeof(ptr));
output is as follows:

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:

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, 11:19 AM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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-14-2008, 12:46 AM   #3
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
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.


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

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 07:42 AM
Freeing memory allocated in a function con Programming 4 01-02-2006 04:25 AM
Does Your Old School Have A Web Presence? XavierP General 16 05-24-2005 08:40 AM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 03:56 AM
Boot Hangs with Redhat, GRUB, Total HugeTLB memory allocated, Neochubbz Linux - General 5 08-19-2004 02:27 PM > Forums > Non-*NIX Forums > Programming

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

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