1

how can i define an array in c which works like vector? This array should take any amount of values. It can take 0 values or 10 values or 100 values.

The code below works but gives me a runtime error that stack was corrupted.

int i = 0;
    int* aPtr = &i;
    int* head = aPtr;
    for(i=0;i<6;i++){
        (*aPtr)=i;
        aPtr++;
    }

Similarly how can i use char* str to take any amount of characters followed by null character in end to make a string?

Practice for interviews :)

Mat
  • 202,337
  • 40
  • 393
  • 406
rajya vardhan
  • 1,121
  • 4
  • 16
  • 29
  • If your code gives you runtime errors, it doesn't work. All you're trying to do is write past the allocated memory of `i`, which will result in segmentation faults. C++ vectors are basically dynamic arrays with niceties around it. If you're trying to replicate a vector in C, there's a lot more to it than what you're doing, but I don't know how much you're trying to do. – wkl Apr 24 '12 at 17:53
  • true, that's why i want to know the correct way – rajya vardhan Apr 24 '12 at 17:55
  • 3
    Well for one thing, what about C++ `std::vector` are you trying to do? Everything? The basic is to implement a struct, which has, perhaps, a pointer and an `int` defining the size, then writing a bunch of functions that take such structs and operate on them, and the pointer is your dynamic array. If you just want a dynamic array, learn to use [C memory allocation](http://en.wikipedia.org/wiki/C_dynamic_memory_allocation). – wkl Apr 24 '12 at 17:56
  • 1
    Is it C or C++? You've tagged both, but your main text mentions only C. – octopusgrabbus Apr 24 '12 at 17:57
  • 1
    You might get some general guidance from one of my [older answers](http://stackoverflow.com/a/10082335/179910). The big change would be that where it removes the oldest object in the buffer, you'd want to allocate more buffer space, copy the existing data to the new space, add the new item, and delete the old space. – Jerry Coffin Apr 24 '12 at 18:02
  • thnks Jerry, it answers my Question :) – rajya vardhan Apr 24 '12 at 18:11

3 Answers3

2

There are many ways to do this in C, depending on your requirements, but you said "any number of values" (which usually means as many as will fit in memory). That's commonly done using realloc to grow the size of an array dynamically. You'll need to keep some bookkeeping information too on the size of the array as it grows.

void
store (vector_t * v, int idx, int value)
{
  if (v->size < idx) {
     v->size = idx * 2;
     v->data = realloc(v->data, v->size);
  }
  v->data[idx] = value;
}

This being tagged "homework", I've left some details to fill in such as the definition of vector_t.

gcbenison
  • 11,723
  • 4
  • 44
  • 82
1

In Your for loop , after the first iteration, you are trying to access aPtr which points to a memory location which was not declared or reserved before. In the first iteration, the int i did the memory allocation for you.

What you could do though would be to initally allocate the memory required using malloc . Once this memory is allocated , and if you walk through only the allocated stack space, you wont come across a run time error.

PS:Your code does not work if it just compiles. Any program may contain run time as well as compile time errors. Your code sample is a very common example of run-time error.

woodstok
  • 2,704
  • 3
  • 34
  • 50
1

This isn't too difficult. The important thing to remember is that you will need to initially allocate memory for your array using malloc(...) or calloc(...). After that you can easily allocate (or deallocate) memory as items are added or removed. The method for dynamically adding or removing memory (which is used to store the items in the array) is realloc(...). The wiki page for C Dynamic Memory Allocation is actually pretty informative. I've provided an example below showing how to initially allocate a char* array, then increase the size and decrease the size.

#include "stdio.h"
#include "stdlib.h"

int main()
{
  char *myDynamicString;

  /* allocate initial memory */
  myDynamicString = (char *)malloc(sizeof(char) * 2);
  myDynamicString[1] = '\0';

  /* set values */
  myDynamicString[0] = 'A';

  /* prints: A */
  printf("String: %s\n", myDynamicString);

  /* make string bigger */
  myDynamicString = (char *)realloc(myDynamicString, sizeof(char) * 6);
  myDynamicString[5] = '\0';

  /* set values */
  myDynamicString[1] = 'P';
  myDynamicString[2] = 'P';
  myDynamicString[3] = 'L';
  myDynamicString[4] = 'E';

  /* prints: APPLE */
  printf("Bigger String: %s\n", myDynamicString);

  /* make string smaller */
  myDynamicString = (char *)realloc(myDynamicString, sizeof(char) * 3);
  myDynamicString[2] = '\0';

  /* set values */
  myDynamicString[1] = 'Z';

  /* prints: AZ */
  printf("Smaller String: %s\n", myDynamicString);

  /* don't forget to release the memory */
  free(myDynamicString);

  return 0;
}
flukey
  • 187
  • 2
  • 8