Thread: Need help with Dynamic Memory Management

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    8

    Need help with Dynamic Memory Management

    Every time I run this code, my program just ups and crashes. I am just learning C and all this and not sure if it is my computer (least likely) or my code (more likely). Could really use the help in figuring this out. Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    
    
    struct node{
    void * curr;
    struct node * NEXT;
    };
    
    
    
    
    char * FArray(char * array){//fills the array with Random char
        for(int r = 0; r < 999999; r++){
            char n = rand() % 26 + 65; //random generator A - Z
            array[r] = n;
        }
    return array;
    }//FArray
    
    
    char * dequeue (struct node ** front, struct node ** tail){
    //remove the front of the line
        struct node * ShortTemNode = (struct node*)malloc(1000000*sizeof(8)); //node with 1000000 structure
        ShortTemNode -> NEXT = NULL; //next become null
    
    
        if((*front) != NULL){
            //Point to the string being dequeued
            char * TheString = (*front) -> curr;
            ShortTemNode = (*front) -> NEXT;
            //Current line leader is removed from front and next is assigned to the front
            free(*front);
            *front = ShortTemNode;
            return TheString;
        }//if ends here - Not empty? Point to dequeued string
        else{
            return NULL;//else just return NULL
            }//else ends here
    };//dequeue ends here
    
    
    int enqueue (char * string, struct node ** front, struct node ** tail){
        //place at the back of the line
            struct node * ShortTempNode = (struct node*)malloc(1000000*sizeof(8)); //node created
    
    
            ShortTempNode -> NEXT = NULL;
            ShortTempNode -> curr = string;
    
    
                if((*front) == NULL){
                *front = ShortTempNode;
                *tail = ShortTempNode;
                }//if end here
        //adds on to the end of the list
        else{
            (*tail) -> NEXT = ShortTempNode;
            *tail = ShortTempNode;
        }
        return 0;
    };//enqueue end here
    
    
    int main (int argc, const char * argv[]) {
        int TimCount = 0; //count number of dequeues happening
        int r;
        int y;
        int a;
            struct node * front;//front created
            struct node * tail;//tail created
    
    
            for(r = 0; r < 10; r++){
                char array [1000000];//array of 1,000,000
                char * string = FArray(array); //points to string
                enqueue(string, &front, &tail);
            }//for ends here
            //enqueue and dequeue 99990
    
    
            for(y = 0; y < 99990; y++){
            char array [1000000]; //array that stores
                char * string = FArray(array);
    
    
                enqueue(string, &front, &tail);
                dequeue(&front, & tail);
                TimCount++;
            }//for ends here
    
    
            for(a = 0; a < 10; a++){
                dequeue(&front, &tail);
                //count the number of times dequeued
                TimCount++;
                //tail is dequeued 10 times
            }//for ends here
            printf("%d", TimCount); fflush(stdout); //Print out one line of output
    }//main

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Well, you're definitely using sizeof wrong. sizeof gives the size of the type of its argument, so sizeof(8) will give the size of an int, which is probably 4. Your struct contains two pointers, so is either 8 or 16 bytes (on 32 or 64 bits, resp.). It should be sizeof(struct node). You should also probably be initializing front and back to NULL. Also, why do you want to allocate a million nodes at a time?

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Quote Originally Posted by algorism View Post
    Well, you're definitely using sizeof wrong. sizeof gives the size of the type of its argument, so sizeof(8) will give the size of an int, which is probably 4. Your struct contains two pointers, so is either 8 or 16 bytes (on 32 or 64 bits, resp.). It should be sizeof(struct node). You should also probably be initializing front and back to NULL. Also, why do you want to allocate a million nodes at a time?

    Part of what I have to do for the program unfortunately.

    It still crashes upon running it.

    Here is the current code.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    
    
    struct node{
    void * curr;
    struct node * NEXT;
    };
    
    
    char * dequeue (struct node ** front, struct node ** tail){
    //remove the front of the line
    	front = NULL;
    	tail = NULL;
    	struct node * ShortTemNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    	ShortTemNode -> NEXT = NULL; //next become null
    
    
    	if((*front) != NULL){
    		//Point to the string being dequeued
    		char * TheString = (*front) -> curr;
    		ShortTemNode = (*front) -> NEXT;
    		//Current line leader is removed from front and next is assigned to the front
    		free(*front);
    		*front = ShortTemNode;
    		return TheString;
    	}//if ends here
    	else{
    		return NULL;//else return NULL
    		}//else ends here
    };//dequeue ends here
    
    
    int enqueue (char * string, struct node ** front, struct node ** tail){
    	//place at the back of the line
    	front = NULL;
    	tail = NULL;
    		struct node * ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node created
    
    
    		ShortTempNode -> NEXT = NULL;
    		ShortTempNode -> curr = string;
    
    
    			if(*front == NULL){
    			*front = ShortTempNode;
    			*tail = ShortTempNode;
    			}//if end here
    	
    	else{
    		(*tail) -> NEXT = ShortTempNode;
    		*tail = ShortTempNode;
    	}
    	return 0;
    };//enqueue end here
    
    
    int main (int argc, const char * argv[]) {
    	int counter = 0; //count number of dequeues happening
    	int r;
    	int y;
    	int a;
    		struct node * front;//front
    		struct node * tail;//tail
    
    
    		for(r = 0; r < 10; r++){
    			char array[1000000];//array of 1,000,000
    			char * string = (rand() % 26) + 65; 
    			enqueue(string, &front, &tail);
    		}//for ends here
    		//enqueue and dequeue 99990
    
    
    		for(y = 0; y < 99990; y++){
    		char array [1000000]; //array that stores
    			char * string = (rand() % 26) + 65;
    
    
    			enqueue(string, &front, &tail);
    			dequeue(&front, & tail);
    			counter++;
    		}//for ends here
    
    
    		for(a = 0; a < 10; a++){
    			dequeue(&front, &tail);
    			//count the number of times dequeued
    			counter++;
    			//tail is dequeued 10 times
    		}//for ends here
    		printf("%d", counter); fflush(stdout); //Print out one line of output
    }//main
    Last edited by flynctoal; 10-03-2016 at 04:50 PM.

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    So there is a warning I am getting at lines 72 and 79 which says "initialization makes pointer from integer without a cast"

    and at lines 71 and 78 i am getting unused variable 'array'

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,003
    At best I was only average at pointers; but, I think you set front and tail to NULL in the wrong spots!!!

    Your code.
    Code:
            struct node * front;//front
            struct node * tail;//tail
    Where I would set them to NULL; remove the places you set them to NULL!
    Code:
            struct node * front = NULL;
            struct node * tail = NULL;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    At best I was only average at pointers; but, I think you set front and tail to NULL in the wrong spots!!!

    Your code.
    Code:
            struct node * front;//front
            struct node * tail;//tail
    Where I would set them to NULL; remove the places you set them to NULL!
    Code:
            struct node * front = NULL;
            struct node * tail = NULL;
    Tim S.
    did that and still getting the same warnings at those lines. even tried copying and pasting the code into a new file and still does it. this is frustrating because i can't tell if it works or not.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,003
    I see no where that you use "array" variable; so, of course the compiler will give the warning you are NOT using it!

    Edit: You need to post the current code that is crashing.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    I see no where that you use "array" variable; so, of course the compiler will give the warning you are NOT using it!

    Edit: You need to post the current code that is crashing.

    Tim S.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    
    
    struct node{
    void * curr;
    struct node * NEXT;
    };
    
    
    char * dequeue (struct node ** front, struct node ** tail){
    //remove the front of the line
    
    
    	struct node * ShortTemNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    	ShortTemNode -> NEXT = NULL; //next become null
    
    
    	if((*front) != NULL){
    		//Point to the string being dequeued
    		char * TheString = (*front) -> curr;
    		ShortTemNode = (*front) -> NEXT;
    		//Current line leader is removed from front and next is assigned to the front
    		free(*front);
    		*front = ShortTemNode;
    		return TheString;
    	}//if ends here
    
    
    	else{
    		return NULL;//else return NULL
    		}//else ends here
    };//dequeue ends here
    
    
    int enqueue (char * string, struct node ** front, struct node ** tail){
    	//place at the back of the line
    
    
    		struct node * ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node created
    
    
    		ShortTempNode -> NEXT = NULL;
    		ShortTempNode -> curr = string;
    
    
    			if(*front == NULL){
    				*front = ShortTempNode;
    				*tail = ShortTempNode;
    			}//if end here
    
    
    	else{
    			(*tail) -> NEXT = ShortTempNode;
    			*tail = ShortTempNode;
    		}
    	return 0;
    };//enqueue end here
    
    
    int main (int argc, const char * argv[]) {
    	int counter = 0; //count number of dequeues happening
    	int r;
    	int y;
    	int a;
    		struct node * front = NULL;//front
    		struct node * tail = NULL;//tail
    
    
    		for(r = 0; r < 10; r++){
    			char array[1000000];//array of 1,000,000
    			char * string = (rand() % 26) + 65;
    			enqueue(string, &front, &tail);
    		}//for ends here
    		//enqueue and dequeue 99990
    
    
    		for(y = 0; y < 99990; y++){
    			char array [1000000]; //array that stores
    			char * string = (rand() % 26) + 65;
    
    
    			enqueue(string, &front, &tail);
    			dequeue(&front, & tail);
    			counter++;
    		}//for ends here
    
    
    		for(a = 0; a < 10; a++){
    			dequeue(&front, &tail);
    			//count the number of times dequeued
    			counter++;
    			//tail is dequeued 10 times
    		}//for ends here
    		printf("%d", counter); fflush(stdout); //Print out one line of output
    }//main ends here

  9. #9
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    New updated code

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    
    
    struct node{
    	void * curr;
    	struct node * NEXT;
    };
    
    
    
    
    char * dequeue (struct node ** front, struct node ** tail){
    //remove the front of the line
    	struct node* ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    	ShortTempNode -> NEXT = NULL; //next become null
    	char * destring;
    	if((*front) != NULL){
    		//Point to the string being dequeued
    		destring = (*front) -> curr;
    		ShortTempNode = (*front) -> NEXT;
    		//Current line leader is removed from front and next is assigned to the front
    		free(*front);
    		*front = ShortTempNode;
    		return destring;
    	}//if ends here
    
    
    	else{
    		return NULL;//else return NULL
    		}//else ends here
    };//dequeue ends here
    
    
    int enqueue (char * string, struct node ** front, struct node ** tail){
    	//place at the back of the line
    		struct node* ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    		ShortTempNode -> NEXT = NULL;
    		ShortTempNode -> curr = string;
    
    
    			if(*front == NULL){
    				*front = ShortTempNode;
    				*tail = ShortTempNode;
    			}//if end here
    
    
    			else{
    				(*tail) -> NEXT = ShortTempNode;
    				*tail = ShortTempNode;
    			}
    	return 0;
    };//enqueue end here
    
    
    int main (int argc, const char * argv[]) {
    	int counter = 0; //count number of dequeues happening
    	int r;
    	int y;
    	int a;
    	int n;
    	char *ranarray;
    		struct node * front = NULL;//front
    		struct node * tail = NULL;//tail
    
    
    		for(r = 0; r < 10; r++){
    			for(y = 0; y < 11; y++){
    				char n = (rand() % 26) + 65;
    				ranarray[y] = n;
    			}
    			//char array[1000000];//array of 1,000,000
    			char * string = ranarray;
    			enqueue(string, &front, &tail);
    		}//for ends here
    		//enqueue and dequeue 99990
    
    
    		for(a = 0; a < 99990; a++){
    			for(int y = 0; y < 999999; y++){
    					char n = (rand() % 26) + 65;
    					ranarray[y] = n;
    				}
    			//char array [1000000]; //array that stores
    			char * string = ranarray;
    			enqueue(string, &front, &tail);
    			dequeue(&front, & tail);
    			counter++;
    		}//for ends here
    
    
    		for(n = 0; n < 10; n++){
    			dequeue(&front, &tail);
    			//count the number of times dequeued
    			counter++;
    			//tail is dequeued 10 times
    		}//for ends here
    		printf("%d", counter); fflush(stdout); //Print out one line of output
    }//main ends here

  10. #10
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Even more changes as I forgot to initialize my array.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    
    
    struct node{
    	void * curr;
    	struct node * NEXT;
    };
    
    
    
    
    char * dequeue (struct node ** front, struct node ** tail){
    //remove the front of the line
    	struct node* ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    	ShortTempNode -> NEXT = NULL; //next become null
    	char * destring;
    	if((*front) != NULL){
    		//Point to the string being dequeued
    		destring = (*front) -> curr;
    		ShortTempNode = (*front) -> NEXT;
    		//Current line leader is removed from front and next is assigned to the front
    		free(*front);
    		*front = ShortTempNode;
    		return destring;
    	}//if ends here
    
    
    	else{
    		return NULL;//else return NULL
    		}//else ends here
    };//dequeue ends here
    
    
    int enqueue (char * string, struct node ** front, struct node ** tail){
    	//place at the back of the line
    		struct node* ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    		ShortTempNode -> NEXT = NULL;
    		ShortTempNode -> curr = string;
    
    
    			if(*front == NULL){
    				*front = ShortTempNode;
    				*tail = ShortTempNode;
    			}//if end here
    
    
    			else{
    				(*tail) -> NEXT = ShortTempNode;
    				*tail = ShortTempNode;
    			}
    	return 0;
    };//enqueue end here
    
    
    int main (int argc, const char * argv[]) {
    	int counter = 0; //count number of dequeues happening
    	int r;
    	int y;
    	int a;
    	int n;
    	char *ranarray[1000000];
    		struct node * front = NULL;//front
    		struct node * tail = NULL;//tail
    		//first 10
    		for(r = 0; r < 10; r++){
    			for(y = 0; y < 11; y++){
    				char n = (rand() % 26) + 65;
    				ranarray[y] = n;
    			}
    			//char array[1000000];//array of 1,000,000
    			char * string = ranarray;
    			enqueue(string, &front, &tail);
    		}//for ends here
    		//enqueue and dequeue 99990
    		for(a = 0; a < 99990; a++){
    			for(int y = 0; y < 999999; y++){
    					char n = (rand() % 26) + 65;
    					ranarray[y] = n;
    				}
    			//char array [1000000]; //array that stores
    			char * string = ranarray;
    			enqueue(string, &front, &tail);
    			dequeue(&front, & tail);
    			counter++;
    		}//for ends here
    
    
    		for(n = 0; n < 10; n++){
    			dequeue(&front, &tail);
    			//count the number of times dequeued
    			counter++;
    			//tail is dequeued 10 times
    		}//for ends here
    		printf("%d", counter); fflush(stdout); //Print out one line of output
    }//main ends here

  11. #11
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Made some more changes.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <limits.h>
    #include <math.h>
    
    
    struct node{
    	void * curr;
    	struct node * NEXT;
    };
    
    
    
    
    char * dequeue (struct node ** front, struct node ** tail){
    //remove the front of the line
    	struct node* ShortTempNode = *front; //node with 1000000 structure
    	ShortTempNode -> NEXT = NULL; //next become null
    	char * destring;
    	if((*front) != NULL){
    		//Point to the string being dequeued
    		destring = (*front) -> curr;
    		ShortTempNode = (*front) -> NEXT;
    		//Current line leader is removed from front and next is assigned to the front
    		free(*front);
    		*front = ShortTempNode;
    		return destring;
    	}//if ends here
    
    
    	else{
    		return NULL;//else return NULL
    		}//else ends here
    };//dequeue ends here
    
    
    int enqueue (char * string, struct node ** front, struct node ** tail){
    	//place at the back of the line
    		struct node* ShortTempNode = (struct node*)malloc(1000000*sizeof(struct node)); //node with 1000000 structure
    		ShortTempNode -> NEXT = NULL;
    		ShortTempNode -> curr = string;
    
    
    			if(*front == NULL){
    				*front = ShortTempNode;
    				*tail = ShortTempNode;
    			}//if end here
    
    
    			else{
    				(*tail) -> NEXT = ShortTempNode;
    				*tail = ShortTempNode;
    			}
    	return 0;
    };//enqueue end here
    
    
    int main (int argc, const char * argv[]) {
    	int counter = 0; //count number of dequeues happening
    	int r;
    	int y;
    	int a;
    	int n;
    	char ranarray[1000000];
    		struct node * front = NULL;//front
    		struct node * tail = NULL;//tail
    		//first 10
    		for(r = 0; r < 10; r++){
    			for(y = 0; y < 11; y++){
    				char n = (rand() % 26) + 65;
    				//return n;
    				ranarray[y] = n;
    			}
    			//char array[1000000];//array of 1,000,000
    			char* string = ranarray;
    			enqueue(string, &front, &tail);
    		}//for ends here
    		//enqueue and dequeue 99990
    		for(a = 0; a < 99990; a++){
    			for(int y = 0; y < 999999; y++){
    					char n = (rand() % 26) + 65;
    					//return n;
    					ranarray[y] = n;
    				}
    			//char array [1000000]; //array that stores
    			char* string = ranarray;
    			enqueue(string, &front, &tail);
    			dequeue(&front, & tail);
    			counter++;
    		}//for ends here
    
    
    		for(n = 0; n < 10; n++){
    			dequeue(&front, &tail);
    			//count the number of times dequeued
    			counter++;
    			//tail is dequeued 10 times
    		}//for ends here
    		printf("%d", counter); fflush(stdout); //Print out one line of output
    }//main ends here

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,594
    Also posted here.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,003
    I see no where in the code that you allocate space to hold the data.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,003
    FYI: If your data is supposed to be a single char then this is wrong!
    Edit: If it is supposed to be a char array (some times called a C string) then you need to malloc the space for it.

    Code:
    struct node{
        void * curr;
        struct node * NEXT;
    };
    Tim S.
    Last edited by stahta01; 10-04-2016 at 04:48 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management
    By chris.r in forum C Programming
    Replies: 5
    Last Post: 06-08-2010, 07:14 PM
  2. Memory Management
    By black_spot1984 in forum C++ Programming
    Replies: 9
    Last Post: 10-08-2008, 11:25 AM
  3. Memory management - How/why/when
    By micke_b in forum C Programming
    Replies: 29
    Last Post: 11-07-2007, 12:26 PM
  4. Dynamic Object Creation (and Management)...
    By Comrade_Yeti in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2005, 01:44 PM
  5. Memory management
    By CompiledMonkey in forum C++ Programming
    Replies: 9
    Last Post: 12-19-2003, 11:41 AM

Tags for this Thread