# Thread: Help allocating and reallocating memory

1. ## 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 element should be this "magic number" we have. That's fine. array 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!

Code:
```#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 ){

free(*ft);
free(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;
**(ft+index+2)=item;
}
else if (index < (**(ft+1)) ){
**(ft+index+2)=item;
}
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. 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
Code:
`long *ptrone;`
The same thing using malloc() to allocate the 10 pointers looks like this
Code:
```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
Code:
```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
{"ft_lang_label":"__label__en","ft_lang_prob":0.8363995,"math_prob":0.957261,"size":2304,"snap":"2020-34-2020-40","text_gpt3_token_len":665,"char_repetition_ratio":0.11652174,"word_repetition_ratio":0.0053475937,"special_character_ratio":0.32769096,"punctuation_ratio":0.15824176,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9838,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-30T07:03:02Z\",\"WARC-Record-ID\":\"<urn:uuid:978825fb-5198-4ec1-b413-6e3dbd86479b>\",\"Content-Length\":\"46584\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:05b729cb-96b1-47d5-9f61-3ba9ea85ead5>\",\"WARC-Concurrent-To\":\"<urn:uuid:c0977a0a-aaf9-47d6-aada-7af104698b42>\",\"WARC-IP-Address\":\"198.46.93.160\",\"WARC-Target-URI\":\"https://cboard.cprogramming.com/c-programming/76845-help-allocating-reallocating-memory.html?s=e49040c84f49331a6bab6a5cb11ba413\",\"WARC-Payload-Digest\":\"sha1:XI3FWBMRRG2JJCKVO7HPCE26XQA7RTBZ\",\"WARC-Block-Digest\":\"sha1:WO72IFSN6NWPTYQV2ZIDNPYMNO4CP3J4\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600402118004.92_warc_CC-MAIN-20200930044533-20200930074533-00037.warc.gz\"}"}