c - Memory allocation error (I think?) in a dynamically sized shopping list. -


i've included code below if kind enough complile see problem. i'm trying make dynamically allocated shopping list used pointers , memory allocation.

the entries stored in 1d char array (seperated null pointers). arrays containing location of first character , entry length have been created in order navigate list.

the code works small entries, when use list of large entries (e.g. 'adsjasdkasjdaksdj') code thinks length (strlen(message)) '110040'. me looks memory error i'm no computer scientist (more of amateur coder). tried extending memory allocated using realloc() problem persists.. appreciated!

i've put code in loop take 10 entries testing purposes.

#include <stdio.h> #include <stdlib.h> #include <string.h>  int listentries = 0;  int main(int argc, const char * argv[]) {      //initial definition , allocation of memory.     char *list;     list = (char *)malloc(sizeof(char)); //allocates requested memory , returns pointer it.      int *entrylength; //array hold length of each entry.     entrylength = (int *)malloc(sizeof(int)); //allocates requested memory , returns pointer it.      int *entrystart; //array hold start point of each entry.     entrystart = (int *)malloc(sizeof(int)); //allocates requested memory , returns pointer it.      char *message; //array hold user input.     message = (char *)malloc(sizeof(char)); //allocates requested memory , returns pointer it.      int arraysize = 0; //variable hold size of array.      char input[32]; //char array take user input (copied 'message' later)      for(int i=0;i<10;i++){         //obtain user entry.         printf("insert entry: \n");         scanf("%s",input);      //transfer string pointer contents usable char array of known size.         message = (char *)realloc(message, strlen(input)*sizeof(char));          int i=0;         while(input[i] != '\0'){             message[i] = input[i];             i++;         }         message[i]='\0';          //add entry list.         listentries++;          //find current size of list.         arraysize=0;         for(i=0;i<listentries;i++) {             arraysize+=entrylength[i];             printf("entrylength[%d] = %d\n",i,entrylength[i]);         }          //reallocate memory accomodate new data.         list = (char *)realloc(list, (arraysize+1)*sizeof(char)+2);         entrylength = (int *)realloc(entrylength,(listentries*4)*sizeof(int)+2); //over allocating         entrystart = (int *)realloc(entrystart,(listentries*4)*sizeof(int)+2); //for safety in dev.          //add new entry data.         entrylength[listentries-1]=(int)strlen(message);         printf("entrylength[%d] is: %d",listentries-1,entrylength[listentries-1]);          entrystart[listentries-1]=arraysize;         //printf("start point is: %d",entrystart[listentries-1]);          for(i = 0; < strlen(message); i++){             list[i+arraysize] = message[i];         }          //print new entry         for(int = 0; < arraysize; i++){             printf("%c",list[i]);         }         printf("\n");     }  } 


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -