Thread: Help allocating and reallocating memory

  1. #1
    Registered User
    Join Date
    Mar 2006

    Help allocating and reallocating memory

    Hello there, long time visitor first time poster.

    I have a problem with the following code... it segfaults as soon as my setFT function is called. I must not be allocating the memory correctly, but what's strange is my temp self-testing code (that **(ptrtwo+5000) = 88 part) actually prints out 88 when I ask it what that particular element is. How is that possible if I haven't allocated that much memory for it?

    My goal is to create a pointer to a pointer to an array of longs. Initially, the array[0] element should be this "magic number" we have. That's fine. array[1] should be the size of our "flexible table" which is initialized at 0 until the user uses setFT to add elements.

    The ultimate goal is have this dynamic array of elements that allocates, deallocated, and reallocates necessary memory. I just get seg faults

    I guess my main question(s) is why am I seg faulting, and if I'm seg faulting, how come I can still put long's into my array?! Any suggestions or thoughts would be greatly appreciated. I'm not sure how clear I have been, but if you need clarification on exactly what the heck I'm talking about, I'll try to do just that. Thank you very much!

    #include <stdio.h>
    #include <stdlib.h>
    #include "FlexTab.h"
    FlexTab newFT(){
    	long **ptrone;
    	long *ptrtwo;
    	ptrone = malloc(sizeof(long));
    	*ptrone = malloc(sizeof(long));
    	**ptrone = (long)calloc(2, sizeof(long));
    	ptrtwo = malloc(sizeof(long *));
    	**(ptrone) = FT_MAGIC;
    	ptrtwo = *ptrone;
    	*(ptrtwo+1) = 0;
    	*(ptrtwo+5000) = 88; //just a tester
    	printf("*(ptrtwo+1) is %ld\n", *(ptrtwo+1));
    	printf("**ptrone is %ld\n",**ptrone);
    	printf("*ptrtwo is %ld\n",*ptrtwo);
    	printf("Ptrtwo @ element 5000 is %ld\n", *(ptrtwo+5000)); //it works?!
    	return ptrone;
    int disposeFT( FlexTab ft ){
    		ft = NULL;
    		return 0;
    int setFT( FlexTab ft, const int index, const long item ){
    	if (index > (**(ft+1)) ){
    		**ft = (long)realloc(*ft, (2 * sizeof(long)) + (index * sizeof(long)) );
    		**(ft+1) = index;
    	else if (index < (**(ft+1)) ){
    		return **(ft+1);
    int getFT( FlexTab ft, const int index, long *const itemp ){
    	if (**ft == FT_MAGIC && *ft != NULL && ft != NULL){
    	*itemp = **(ft + index + 2);
    	return ( (sizeof(ft)) / (sizeof(long)) );
    	else if (index >= FT_MAX){
    		return -2;
    	return 88;

  2. #2
    Registered User
    Join Date
    Aug 2005
    ptrone is a 2 dimensional array of pointers to longs. The first dimension indicates the number of pointers to longs that you want -- for example is you want an array of 10 pointers then you could write it like this
    long *ptrone[10];
    The same thing using malloc() to allocate the 10 pointers looks like this
    long **ptrone;
    ptrone = malloc( 10 * sizeof(long*));
    Once you have that, you can allocate each of the 10 arrays. For example, if each array element is a pointer to 15 longs
    int i,j;
    for(i = 0; i < 10; i++)
       ptrone[i] = malloc( 15 * sizeof(long) );
    //or if you prefer to use calloc
    for(i = 0; i < 10; i++)
       ptrone[i] = calloc( 15, sizeof(long) );
    I'm supprised your compiler didn't complain about the return value of function newFT().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function returning malloc'ed memory.
    By samus250 in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 01:33 PM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  3. reallocating structures dynamically in functions
    By cezaryn in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 09:54 PM