-3

Consider a dynamic table with the following properties:

  • Elements are stored in a dynamic array
  • Capacity is the size of the dynamic array
  • Size is defined is the number of elements stored in the array

Insert elements into this dynamic table. Double capacity if size is equal to capacity before push_back()

Don't use malloc or calloc functions.

Input: (n, elements)
9
6 7 8 12 4 10 11 1 15
Output:
capacity = 1; size = 1; elements = 6
capacity = 2; size = 2; elements = 6 7
capacity = 4; size = 3; elements = 6 7 8
capacity = 4; size = 4; elements = 6 7 8 12
capacity = 8; size = 5; elements = 6 7 8 12 4
capacity = 8; size = 6; elements = 6 7 8 12 4 10
capacity = 8; size = 7; elements = 6 7 8 12 4 10 11
capacity = 8; size = 8; elements = 6 7 8 12 4 10 11 1
capacity = 16; size = 9; elements = 6 7 8 12 4 10 11 1 15

#include <stdio.h>
int size=0;
int capacity=0;

int * double_capacity(int *a) {
    int l=0;
    if(capacity==0) capacity++;
    capacity=capacity*2;
    int b[capacity];
    for(l;l<size;l++){
        //printf("%d : %d \n",l,a[l]);
        b[l]=a[l];
    }
    return b;
}

int * push_back(int *a,int j) {
    if(size==capacity)
        a=double_capacity(a);
    a[size]=j;
    size++;
    int k=0;
    printf("capacity = %d; size = %d; elements = ",capacity,size);
    for(k=0;k<size;k++) {
        printf(" %d",*(a+k));
    }
    printf("\n");
    return a;
}


main() {
    int *a;
    int n,i,j,k,l;
    scanf("%d",&n);
    int temp[n];
    for(i=0; i<n; i++) {
        scanf("%d",&temp[i]);
    }

    for(i=0; i<n; i++) {
        a=push_back(a,temp[i]);
    }
}

it is showing compile error like
\temp.c: In function ‘double_capacity’:
temp.c:16:2: warning: function returns address of local variable [-Wreturn-local-addr]
return b;
^


even though i run this

when i gave input
3 3 2 1
output
capacity = 1; size = 1; elements = 3
capacity = 2; size = 2; elements = 3 2
capacity = 4; size = 3; elements = 3 2 1

when i gave input
5 5 4 3 2 1
output
capacity = 1; size = 1; elements = 5
capacity = 2; size = 2; elements = 5 4
capacity = 4; size = 3; elements = 5 4 3
capacity = 4; size = 4; elements = 0 0 -2128976676 2
capacity = 8; size = 5; elements = 0 0 -2128976676 32524 1

user5240221
  • 41
  • 2
  • 6
  • 3
    First mistake is you did not even try to format it properly. This would be good start if you want someone to read this. – too honest for this site Sep 01 '15 at 20:07
  • `int b[capacity];` is local scope. `return b;` can't use return value(`b`) at caller. – BLUEPIXY Sep 01 '15 at 20:12
  • Please indicate what problems you are having (compile errors, runtime segfaults, etc.); if you are just interested in a code review, that's a separate stack exchange – Foon Sep 01 '15 at 20:41
  • 1
    Is this homework help? Please post what you've tried and why it's not working. – Andy Hoffner Sep 01 '15 at 20:43
  • **if this code is working as intended** and you want it reviewed for e.g. readability, best practices etc. Feel free to bring it over to http://codereview.stackechange.com – Kaz Sep 01 '15 at 20:45
  • Sorry for mistakes this is my first post. I don't know how to use stake over flow perfectly. Next time i won't repeat this. – user5240221 Sep 02 '15 at 02:54

1 Answers1

1

This won't work the way you want it to:

int * double_capacity(int *a) 
{
  int l=0;

  if(capacity==0) 
    capacity++;

  capacity=capacity*2;
  int b[capacity];
  for(l;l<size;l++)
  {
    //printf("%d : %d \n",l,a[l]);
    b[l]=a[l];
  }
  return b;
}

The array b only exists for the lifetime of the double_capacity function; once the function exits, b no longer exists, and any pointer to it is now invalid.

You can't use VLAs this way.

I notice your instructions said nothing about the realloc function...

EDIT

If you cannot use any of the standard memory management functions (malloc, calloc, or realloc), then the only real alternative I can think of is to create your own memory pool and allocate your table elements from it.

Basically, you'd declare a large array of unsigned char at file scope like so:

#define HEAP_SIZE 8192 // 8 KB heap, use whatever size you need
static unsigned char heap[HEAP_SIZE];

then you'd grab chunks of this array to build your table. You'll need to do some bookkeeping to manage available and allocated blocks, their addresses and sizes within that array, etc. If you know what you're doing, this is a day or so of solid effort depending on how robust you want it to be.

Then again, there may be something a little more straightforward that I'm forgetting about. I just can't think of it at the moment.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • thank you @Johan Bode . is there any another method for VLA, without using realloc also. when i run this code in my system, for 3 numbers it is giving correct output but not for more. – user5240221 Sep 02 '15 at 02:39
  • @user5240221: The problem with VLAs is that their lengths are fixed once they have been defined; "variable length" only means that their sizes can be determined at *run time*, not that their lengths can change during their lifetime. See my edit for one possible idea. – John Bode Sep 02 '15 at 18:26