The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
far pointer useless79 High Level Programming 1 11-08-2007 02:13 AM
A Pointer to non-Virtual Address, and All of my Hard drive xcoder66 High Level Programming 9 12-19-2005 02:22 PM
pointer sarwan High Level Programming 1 11-15-2005 03:41 AM
How to Achive IP address through MAC(Ethernet) address krishnacins IP Networking 3 08-29-2005 05:45 PM
network address and broadcast address? pnxi UNIX for Dummies Questions & Answers 7 11-10-2003 08:29 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8  
Old 08-15-2006
Registered User
 

Join Date: May 2006
Posts: 10
confused?
could you show the entire program?
Reply With Quote
Forum Sponsor
  #9  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
Quote:
Originally Posted by benbenpig
confused?
could you show the entire program?
Code:
char heap[ 134 ];
char *firstFree = heap;

char *allocMem( int size ) {
        if ( size >= 0){
                void *malloc(size_t sizeofint);
                /*allocate space for an array with size number of elements of type int*/
                char * ip = malloc(sizeof(int) * size); 
                if(ip == '\0'){ /*If function is null, memory not allocated so exit*/
                        return '\0';
                }else{ /*Otherwise return address of first free space*/
                        firstFree = ip+1;
                        return firstFree;
                }
                        }
        else {
                printf("ERROR: Cannot allocate negative memory");
        }
        return 0;
}

void dumpHeap( void ) {
        char letter;
        int counter = 0;
        int i;
        for (i=0; i < *firstFree; i++){
                letter = heap[i];
                printf ("%c   ",(char) letter);
                counter++;
                if(counter==10){
                        counter = 0;
                        printf("\n");
                        }
        }
        printf("\n");
}
the program tries to allocate memory then print out the content
Reply With Quote
  #10  
Old 08-15-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
for (i=0; i < 134; i++)
You can define some constants or give the dumpHeap() function the size of the heap. You cannot determine length of the array having only a pointer on it's first element
Reply With Quote
  #11  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
i'm trying to print out the content of the memory from the beginning up till the first free space which i have firstFree to, so i'm kinda trying to print out heap[0] to heap[firstFree address]
Reply With Quote
  #12  
Old 08-15-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
then
for (i=0; heap+i < firstFree; i++)
heap+i == &(heap[i]) -pointer to the memory of element i of the heap, it must be less than firstFree
Reply With Quote
  #13  
Old 08-15-2006
Registered User
 

Join Date: May 2006
Posts: 10
Quote:
Originally Posted by Poison Ivy
i'm trying to print out the content of the memory from the beginning up till the first free space which i have firstFree to, so i'm kinda trying to print out heap[0] to heap[firstFree address]
the problem is how can you know the "firstFree address" ?
*firstFree is only the value of the heap[1];
Reply With Quote
  #14  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
Quote:
Originally Posted by Hitori
then
for (i=0; heap+i < firstFree; i++)
heap+i == &(heap[i]) -pointer to the memory of element i of the heap, it must be less than firstFree
this is still not working i get an empty line
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0