"dynamic array"

This is a discussion on "dynamic array" within the C Programming forums, part of the General Programming Boards category; hi i need to allocate a "dynamic array". the user can enter the size of it (eg: myprog 100) and ...

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    "dynamic array"

    hi

    i need to allocate a "dynamic array". the user can enter the size of it (eg: myprog 100) and this array will be generatet and worked with as a range checker (i'm checking numbers in files and if they alreadi ocured, i'll set array[i]. so if the number comes twice, i can abort). this array is in a function, which will be called several times. si i thought, the folowing thing will do:
    Code:
    static int initstat;
    static int *colision=NULL;
    colision=(int *)malloc(max_testnr*sizeof(int));
    
    //initialise
    if(initstat == FALSE){
    	while(i<=max_testnr)
    		colision[i++] = 0;
    }
    
    //some code
    
    //checker routine
    if(curent < max_testnr && curent >= 0){		//check range
    			if(colision[curent] != 0)						/* check if already exists */
    				fputc('*',f2);						//mark
    			else {
    				fputc(' ',f2);
    				colision[curent] = 1;
    			}
    		}
    		else
    			fprintf(f2,"AOR: ");
    		
    		fputs(nr,f2);
    //more code
    however, it seems that colision[i] does not work. if i chose a static array everything is fine. what's wrong?

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    this array is in a function, which will be called several times
    Code:
    static int *colision=NULL;
    colision=(int *)malloc(max_testnr*sizeof(int));
    You do realize that if this function is "called several times" you'll be have a HUGE memory leak here. Every single time the function is called, you're mallocing another chunk of data. You never free it. But still, every time it's called, you malloc up another block of memory. This is a BadThing(TM).

    What do you mean by "it doesn't work"? Surely it does something. Does it not compile? Ok, what's the error you get? Does it crash, have random values? What exactly? Don't just say something "doesn't work".

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    ok, bad error. the malloc has to be in the if statement. and it'll be freed at termination of programm, becaus it's needed till then.

    the programm does compile. it seems like accessing the elemts of the array does not work, because it reports a colision even if there's non (means if(colision[curent]!=0) fails)

    but why?

  4. #4
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    i did initialize it, but did another stupid error. i had a statement from a former version, that used to work with a pointer with the same name. so the prog didn't crash, but not do what i did suspect.

    it's too early in the morning

    thanks

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    You cannot access an array element through a pointer with "pointer[index]", you must do this: "*(pointer + index * sizeof (datatype))". Here's how this works: "pointer", without the * operator, is the address of the variable it's pointing to, and in an allocated array, all elements are in succesion(sp?). So, we take that value, and add the element you want to access times the size (in bytes) of the data type of the array (notice the higher precedence of the multiplication operator). So, all that put in parenthesis is the address of the variable we want to access, and the * (pointer operator? I'm not sure what it's called) makes this block of code the variable we want to access.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocation from "static array"
    By Petike in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 03:17 AM
  2. what's a "dynamic buffer"?
    By MK27 in forum C Programming
    Replies: 22
    Last Post: 08-08-2008, 08:48 AM
  3. how can i pass by reference by "malloc 2d array"?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 05-22-2005, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21