-3

I am writing some simple Stack operations with my data structure being an Array.

#define DEFAULT_VAL 10        //in a separate Header file
int *stacky = (int*) malloc (default_size * sizeof(int));

The objective is to write a function to dynamically set the size of the Stack while ensuring that the elements are not lost.

Here is what I have so far:

void Sizer( int size)
{
  #undef DEFAULT_VAL
  #define DEFAULT_VAL size
  maxSize = size;
  int *newbuffer = (int*) realloc (stacky, size);
  if(newbuffer == NULL) //checking if the 'realloc' was successful :)
    {
      printf("PROBLEM HERE :)");              
    }
  else
    {
      stacky = newbuffer;     
    }
}

In my main() function:

int main()
{
  int i;
  for( i=1; i<15; i++) 
   {
     push(i);
   }
  Sizer(9);
  displayStack();
  Sizer(17);
  displayStack();
}

The Output is:

DEFAULT_VAL is now: 9
        9. 9
        8. 8
        7. 7869816
        6. 7877384
        5. 17278
        4. 385207786
        3. 3
        2. 2
        1. 1

DEFAULT_VAL is now: 17
        9. 9
        8. 8
        7. 7869816
        6. 7877384
        5. 17278
        4. 50331651
        3. 3
        2. 2
        1. 1

Any advice is appreciated! Thanks

Claudio Cortese
  • 1,372
  • 2
  • 10
  • 21
Arjun
  • 817
  • 3
  • 16
  • 28
  • You can't use `#define`'s the way you're trying to use them (as variables). – tkausl Jan 29 '17 at 07:46
  • It's not 100% clear where you're defining `stacky`, but in C, you cannot write an initializer like the one shown when the variable is outside a function. – Jonathan Leffler Jan 29 '17 at 07:53
  • 1
    Note that in the code `void Sizer( int size) { /* newline */ #undef DEFAULT_VAL /* newline */ #define DEFAULT_VAL size /* newline */ maxSize = size;`, you don't actually use `DEFAULT_VAL`. The definition as `size` is odd. Remember, the preprocessor would do a text substitution at compile time. The code could work — but using the preprocessor to do what you seem to be trying to do is perverse, especially since the macro isn't actually used. – Jonathan Leffler Jan 29 '17 at 07:55
  • Thank you for pointing that out! I am aware of the preprocessor's role but I seem to understand my mistake only now. I removed the lines `#undef DEFAULT_VAL` and `#define DEFAULT_VAL size` – Arjun Jan 29 '17 at 08:06
  • Read also about [flexible array members](https://en.wikipedia.org/wiki/Flexible_array_member). You might find that *very* useful – Basile Starynkevitch Jan 29 '17 at 08:13

1 Answers1

2

From the man page:

The realloc() function changes the size of the memory block pointed to by ptr to size bytes.

So instead of:

int *newbuffer = (int*) realloc (stacky, size);

you probably want

int *newbuffer = (int*) realloc (stacky, size * sizeof(int));

BTW: No need for cast when using malloc and friends. See Do I cast the result of malloc?

Community
  • 1
  • 1
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Appreciate it! After removing the cast, I get the error - invalid conversion from `void*` to `int*`. – Arjun Jan 29 '17 at 08:02
  • @Arjun - Sounds strange with `invalid conversion` - are you using a c++ compiler instead of c compiler? – Support Ukraine Jan 29 '17 at 08:13
  • I'm using Dev-C++! – Arjun Jan 29 '17 at 08:20
  • @Arjun - So you probably compiles your c code with a c++ compiler. Consequently the rules of c++ applies and with c++ you have to cast. Remember that c and c++ are two different languages. Very similar for many basic things but not identical. If you want to write c code, make sure to compile it as c code. – Support Ukraine Jan 29 '17 at 08:25
  • @Arjun - BTW: The casting was not the core of my answer. The important thing was that the `realloc` call was using the wrong size. – Support Ukraine Jan 29 '17 at 08:27
  • Got it! Is my logic correct though for the `Sizer()` function? – Arjun Jan 29 '17 at 08:31
  • @Arjun - I'm not sure what you are trying to do with the first 2 lines - they seem strange. The third line, i.e. `maxSize = size;` also seems a bit strange - is `maxSize` a global variable? – Support Ukraine Jan 29 '17 at 08:41
  • Yes, `maxSize` is a variable which holds the maximum size of my Stack! I removed the first two lines.. (They can be ignored) – Arjun Jan 29 '17 at 09:16
  • @Arjun - The rest of the code looks fine. Some improvements you could consider: 1) let the function return an `int` to tell whether the resize succeeded or failed. 2) Avoid global variables, i.e. pass `stacky` ans `maxSize` as pointer arguments. – Support Ukraine Jan 29 '17 at 09:43
  • Thanks for the tips! :) – Arjun Jan 29 '17 at 10:50