dynamic allocation from 1 instead of zero

This is a discussion on dynamic allocation from 1 instead of zero within the C Programming forums, part of the General Programming Boards category; Also spotted another problem: if (!mat) inside the for-loop should be if(!mat [i] ). Freeing should just take a pointer ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also spotted another problem: if (!mat) inside the for-loop should be if(!mat[i]).

    Freeing should just take a pointer - and of course you should free in reverse order, so start with the loop, and finally free the mat pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Ops, my distreaction about [i].

    Here I post my "free" function. From what you mentioned it should be correct.

    Code:
    void free_darray2d(double **mat, size_t nrows, size_t ncols, int index)
    {
    	//nrows = number of rows
    	//ncols = ncolumns
    	//index = 1 if you want allocation in the fortran way starting from 1
    	//index = 0 if you want the standard C allocation way starting from 0
    	
    	long i;
    	
    	for(i=index; i<nrows+index; i++)
    		free(mat[i]);
    		
    	free(mat);
    
    return;
    }

    On the other hand, for a 1D array, by analogy, there is no need to set a vect[0] = NULL since I allocate it as I did for the row pointer of the 2D matrix; am I saying it right?

    Code:
    //Double 1d array
    void darray1d(double *vect, size_t nrows, int index)
    {
    	//nrows = number of rows
    	//index = 1 if you want allocation in the fortran way starting from 1
    	//index = 0 if you want the standard C allocation way starting from 0
    	
    	long i;
    
    	//Malloc pointers to rows
    	vect = malloc((nrows+index)*sizeof(double*));
    	if (!vect){
    		printf("Matrix MAT could not be allocted");
    		exit(1);
    	}
    	
    return;
    }
    Here I post the 1d array al;location as well. This is my last question!
    Thank you very much
    Last edited by cfdprogrammer; 04-28-2009 at 04:24 AM.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so I just spotted another mistake: you are comparing i <= nrows (in both allocation and freeing). That should be "i < nrows+index", and i = index should be your starting point. [Actually your free code is CORRECT, but if you use <, then you don't need the -1, which makes the code simpler to understand].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Right; writing on a forum gets me quite distracted apparently!

    thanks for spotting them out.
    It should work now; I will post something if I get errors in compilations

    Thank you again
    cfd

  5. #20
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi again,

    I unfortunately get a BUS error with the function that I posted.
    I then tried to modify it by making it a pointer to function since I need to return the matrix that I want to allocate, and instead of a BUS error, I now get a segmentation fault

    what could that be?

    thank you in advance
    best,

    Code:
    int **iarray2d(size_t nrows, size_t ncols, int index)
    {
    	//nrows = number of rows
    	//ncols = ncolumns
    	//index = 1 if you want allocation in the fortran way starting from 1
    	//index = 0 if you want the standard C allocation way starting from 0
    	
    	long i;
    	int **mat;
    
    	//Malloc pointers to rows
    	mat= malloc((nrows+index+1)*sizeof(int*));
    	if (!mat){
    		printf("Matrix MAT could not be allocted");
    		exit(1);
    	}
    	
    	mat[0] = NULL;
    
    	for(i=index; i<nrows+index+1; i++){
    		mat[i] = malloc((ncols+index)*sizeof(int*));
    		if (!mat[i]){
    			printf("Matrix MAT could not be allocted\n The exec. will Exit now\n");
    			exit(1);
    		}
    	}
    	
    return mat;
    }

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by cfdprogrammer View Post
    Hi again,

    ... instead of a BUS error, I now get a segmentation fault
    Apologies, I solved the segmentation fault. I was freeing the memory twice!
    So, to sum it up: I get a bus error with the original version of the function, but it works fine when I use a pointer to function instead.
    What would be the cause to it? Are they not equivalent?

    thanks

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, yes, to modify a pointer to pointer, you need a pointer to pointer to pointer (so int ***mat, and use *mat inside the function - it may be better to use a new variable inside the function, and before the function returns, you set the *mat = tmpMat or whatever you call it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by matsp View Post
    Ah, yes, to modify a pointer to pointer, you need a pointer to pointer to pointer (so int ***mat, and use *mat inside the function - it may be better to use a new variable inside the function, and before the function returns, you set the *mat = tmpMat or whatever you call it).

    --
    Mats
    Great, thank you very much.
    I'll use the pointer to function now since it works well as it is.

    Regards,
    cfd

  9. #24
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    I now get a new error in execution:

    this line repeated many many times
    Code:
    meteoIni.a(32753) malloc: *** error for object 0x30303130: 
    Non-aligned pointer being freed (2)
    does anyone know what is happening?
    thanks

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably that you are freeing something that you didn't allocate.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by matsp View Post
    Probably that you are freeing something that you didn't allocate.

    --
    Mats
    Hi,

    that is the strange thing, since I am only freeing what I am allocating.
    I am allocating 6 different variables with this new function, and what is strange about the error that I get, is that it is only happening when I allocate ONE of the six variables, and this variable is not even the last to be allocated and freed

    thank you again

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are probably writing to some memory that you didn't intend to write to, or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Thank you, I'll figure it out although I can't explain it. It worked with no problem when I was allocating it with the NR library with the same range
    strange

    best

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  2. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 04:10 PM
  5. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM

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