How to init an array dynamically on the heap?

This is a discussion on How to init an array dynamically on the heap? within the C Programming forums, part of the General Programming Boards category; Hi! How to init an array dynamically on the heap in C ( in C++ its easy). To init an ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    How to init an array dynamically on the heap?

    Hi!

    How to init an array dynamically on the heap in C ( in C++ its easy). To init an array on the stack, I write:

    int size = 1000;
    int stackarray[size];

    How to do the same on the heap?

    int* heaparray = (int*) malloc (size);

    But: my heaparray pointer doesnot know its a pointer to an array. And I want him to know that, because my Borland VCC6 Debugger should show me the array fields. Is there a way to do that with simple typecasting like

    (int[size]) heaparray

    which doesnot work.

    ???

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You mean this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int *p, i;
      
      p = malloc(10 * sizeof(*p));
      
      if (p != NULL)
      {
        for (i = 0; i < 10; i++)
          p[i] = i;
        for (i = 0; i < 10; i++)
          printf ("p[%d] is %d\n", i, p[i]);
      }
      
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    I thought that any variable declared within a function was placed in the stack, and only global variables were allocated on the heap? Is that wrong?

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I thought that any variable declared within a function was placed in the stack, and only global variables were allocated on the heap?
    It really is better to think of memory as coming in two variants; the first is your McDonald's happy meal prepackaged static memory. The second is "have it your way" Burger King dynamic memory.

    Static memory is named storage controlled by the compiler, like
    Code:
    int a = 10;
    int *b;
    
    b = &a;
    a is static memory, b is a pointer to static memory, behind the pointer there's named storage that the compiler takes care of. Next is dynamic memory, this is anonymous storage that is *only* referenced by a pointer
    Code:
    int *b;
    
    b = malloc(sizeof(int));
    The compiler doesn't do any housekeeping and there's no name behind the pointer, just unnamed memory that you have control over. When all you think about is static and dynamic you have a lot less confusion and you don't run into problems like this
    Code:
    char *f(void)
    {
      char a[100]; /* Static memory! */
    
      fgets(a, 100, stdin);
    
      return a; /* Uh-oh, static memory gets cleaned up, you return garbage */
    }
    *Cela*

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    2
    Hi Hammer,

    not exactly. What I want is a kind of arrayTYPE for heap-allocated memory.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >not exactly. What I want is a kind of arrayTYPE for heap-allocated memory.<

    Which you have given (assuming an int is one byte wide). You should be able to use heaparray as an array. If your debugging enviroment is foobarred, try something else.
    Joe

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655

    Re: How to init an array dynamically on the heap?

    Originally posted by karantsch
    Hi!

    How to init an array dynamically on the heap in C ( in C++ its easy). To init an array on the stack, I write:

    int size = 1000;
    int stackarray[size];

    How to do the same on the heap?

    int* heaparray = (int*) malloc (size);

    But: my heaparray pointer doesnot know its a pointer to an array. And I want him to know that, because my Borland VCC6 Debugger should show me the array fields. Is there a way to do that with simple typecasting like

    (int[size]) heaparray

    which doesnot work.

    ???



    No.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 08:07 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21