doesnt return allocated memory

This is a discussion on doesnt return allocated memory within the C Programming forums, part of the General Programming Boards category; hello i m supposed to make a program that should allocate according to va_start then allocate according to num of ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    doesnt return allocated memory

    hello i m supposed to make a program that should allocate according to va_start then allocate according to num of parameter specified by first parameter i did everything according but i only got first 2 members of the arguments
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
    #define ParseSize(x) (x) + (sizeof x/sizeof x[0])
    int * new_i_arrays(int lim,...)
    {
    
        va_list ap;                   // declare object to hold arguments
    
        int *arr;
    
        int i;
    
        va_start(ap, lim);            // initialize ap to argument list
        arr=(int*)calloc(lim,sizeof(int));
    
        for (i = 0; i < lim; i++)
    
           arr[i] = va_arg(ap, int); // access each item in argument list
    
        va_end(ap);                   // clean up
    
        return arr;
    
    }
    
    
    void ShowArr(int *arr,int *Size) {
        while( arr < Size )
            printf("%d\n",*arr++);
        putchar('\n');
    }
    int main(void)
    {
        int *p1;
        int *p2;
        p1=new_i_arrays(5,1,2,3,5,7);
        p2=new_i_arrays(3,1,2,3);
        ShowArr(p1,ParseSize(p1));
        ShowArr(p2,ParseSize(p2));
        free(p1);//free allocated memory
        free(p2);//free alocated memory
        return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> #define ParseSize(x) (x) + (sizeof x/sizeof x[0])

    That only works for stack-declared arrays, and even then only if referred to directly by name (eg: you couldn't form a pointer to it and then pass it to the macro, nor could you pass it to a function and obtain the correct calculation). Sorry, that's just how C works - you'll need to store the size somewhere.

    >> void ShowArr(int *arr,int *Size) {

    Really bad choice of a variable name there. I would think it was a pointer to a variable containing the size information, considering it's name. Maybe something like "arr_end" would be more appropriate.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yes i totally forgot i declared it in the heap
    but shouldnt it work since i already allocated it on the heap ? then it will be like a array since i alrdy allocated it ?
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
    
    int * new_i_arrays(int lim,...) {
    
        va_list ap;                   // declare object to hold arguments
        int *arr;
        int i;
    
        va_start(ap, lim);            // initialize ap to argument list
    
        arr=(int*)calloc(lim,sizeof(int));
        for (i = 0; i < lim; i++)
           arr[i] = va_arg(ap, int); // access each item in argument list
    
        va_end(ap);                   // clean up
        return arr;
    
    }
    
    
    void ShowArr(int *arr,int arr_end) {
        int i=0;
        while( i < arr_end )
            printf("%d\n",arr[i++]);
        putchar('\n');
    }
    int main(void)
    {
        int *p1;
        int *p2;
        p1=new_i_arrays(5,1,2,3,5,7);
        p2=new_i_arrays(3,1,2,3);
        ShowArr(p1,5);
        ShowArr(p2,3);
        free(p1);//free allocated memory
        free(p2);//free alocated memory
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    as for var names i not that great in doing names :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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