×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do I perform basic memory management

How do I perform basic memory management

How do I perform basic memory management

(OP)
Hi,

Having been programming in the sheltered word of .NET for a while, I appear to have forgotten how to perform the most basic of tasks in C and desperately require help.

I want to store a list of words in memory that I can access quickly. Because all of the words will be of different lengths, I thought that I would store all of the words on one heap (the "word" heap) and then store a pointer to each word in the "word" heap in the "dictionary" heap.

My logic was that the "dictionary" heap would store 32 bit memory addresses that I could then 'dereference' to retrieve the corresponding word on the "word" heap. Because all the values on dictionary heap would be the same size, it should be very easy to step through (rather than dealing wih strings of different lengths).

However, I have been plagued by problems. The program reads the words from a file. Small files were OK, but I noticed that over a certin size strange and/or fatal errors began occurring. Therefore, I have stripped the file IO from the program completely and put the code that would usually execute for each word in the file in a FOR loop so that I can check at which point in the loop things go awry.

When the upper bound of the loop is set to a value over 279, the "word heap" address returned from the dictionary heap is incorrect and when the loop upper bound is set to a value over 15895 the program crashes.

I think that I am doing something fundamentally wrong and would appreciate any help.

I have tidied the code up as best I can but please forgive the state of it as it is most definately a "work in progress".

Thanks in advance,

Simon
(code attached)

#include <windows.h>
#include <stdio.h>

//Heap related vatriables :
//-------------------------
HANDLE  wordHeap; //Stores words/strings
HANDLE  dictionaryHeap; //Stores pointers to the word heap
char ** dictionary; //Block allocated from dictionary heap
char ** words;//Bloack allocated from word heap

//My test word
char *  WORD_TO_STORE  = "Whywontthiswork";

//Function
char * storeWord (char *);

int main() {
    
    //VARIABLES USED TO OUTPUT WORDS
    char ** dicPtr; //Pointer to the address on the "dictionary" heap
    char ** wordPtr; // Pointer to the address on the "word" heap
    ////////////////////////////////
    
    // VARIABLES USED TO STORE WORDS
    long i; // Loop counter
    int firstBlock; // Pointer to first block of memory allocated on "dictionary" heap
    char ** dictionaryWalker; // Used to traverse "dictionary" heap
    
    //ALLOCATE MEMORY
    dictionaryHeap = HeapCreate(0,0,0);
    wordHeap = HeapCreate (0,0,0);
    dictionaryWalker = malloc(sizeof(char*));

    // Set flag to rmember to store "first block"
    firstBlock = 1;
    
    printf ("Storing words.......\n\n");

    //If the loop upper bound is > 279 then the "word ptr" will return an incorrect (odd) address
    //If the loop upper bound is > 15895 then we crash :-(
    for (i=0; i < 279; i++) {
        

            dictionary = HeapAlloc(dictionaryHeap,0,sizeof(char*));
            if (firstBlock == 1) {
                
                memcpy(dictionaryWalker,&dictionary,sizeof(char*));
                firstBlock =0;
            }
                        
            memcpy(dictionary,storeWord(WORD_TO_STORE),sizeof(char *));
                    
    }
    
    //Output last word pointer
    printf ("\tLast Word ptr      : %x\n", *dictionary);
    
    printf ("\n\nRetrieving words.......\n\n");
    //This loop should output every address on the "word" heap
    while (*dictionaryWalker <= dictionary) {
                        

        dicPtr =  (char**)dictionaryWalker;
        wordPtr = malloc(sizeof(char*));
        
        //THIS IS THE OFFENDING LINE :
        memcpy(wordPtr,*dicPtr,sizeof(char*));
                    
        *dictionaryWalker += 16; //Minimum heap allocation unit
        
        // If this is the last pointer then dump the word pointer
        if (*dictionaryWalker > dictionary) {            
            printf ("\tLast Word ptr       : %x\n",*wordPtr);
        }
    
        free(wordPtr);// Cleanup
    }
        
    return 0;
}


// Stores a word in the "word" heap and returns a pointer to that
// word
char * storeWord (char * word) {
    
    words = HeapAlloc(wordHeap,0,strlen(word) + 1);
    memcpy(words,word,strlen(word) +1);
    return &words;    
}

RE: How do I perform basic memory management

First of all:
Have a look at the STL vector class. It will do what you want, is a lot easier to use (since someone else already wrote it for you) and these template classes are hard to outperform (they say) since they are already optimized....

Second:
If you insist on reinventing the wheel then openup your memory debug window and see what happens in memory with the two heaps while looping.

Greetings,  
Rick

RE: How do I perform basic memory management

(OP)
Hi Rick,

Thanks for your reply.

My reasoning for using C instead of C++ was that I would get greater speed (I do appreciate that I am reinventing the wheel ). Do you feel that the difference in performance is negligible? (I am certainly no expert on performance - I am just going on heresay).

Cheers,

Simon

RE: How do I perform basic memory management

There is no reason why well written c++ should be slower than c. Using things like the stl vector class is going to be about as fast as youll be able to write (highly optimised).

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close