Thread: declare pointer and allocate memory for an array typedef

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Ota-ku, Tokyo, Japan, Japan
    Posts
    1

    Exclamation declare pointer and allocate memory for an array typedef

    Hi, In my attempt to use a library called PJSIP, I came across a definiton for a typedef as following,

    typedef long some_type[64];

    Now, I want to declare a pointer and allocate memory for this in a thread.
    I DONT want to declare and automatically allocate memory like this,

    some_type a;

    Rather I want to do something like this,

    some_type* a = malloc(sizeof(*a));

    and pass 'a' to a function, with a definition like: void function(some_type a);

    however, the effective sizeof allocated memory shall be 256, but its only 4, which is nothing but the sizeof of a pointer.

    How can I possibly declare and allocate a memory for a variable of type "some_type".

    Thanks

    Amar

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Amar Akshat
    however, the effective sizeof allocated memory shall be 256, but its only 4, which is nothing but the sizeof of a pointer.
    Are you sure? How did you check?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Declaring a typedef of an array doesn't change the underlying implementation that all arrays are passed as pointers. You might not have any [] or * in the function declaration (as written), but they're still there.

    Eg.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef long some_type[64];
    
    void function(some_type a) {
      printf("%ld\n", sizeof(a) );
    }
    
    int main(void) {
      some_type a;
      some_type* pa = malloc(sizeof(*pa));
      long arr[64];
      function(a);
      function(*pa);
      function(arr);
      return 0;
    }
    A typedef is just an alias for some other type. If you want, you can write out the long form and get exactly the same results.

    If you want to pass arrays "by value" (only really worth doing for small arrays), then you can hide the array in a struct, like
    Code:
    struct sometype {
        long arr[64];
    };
    Remember to keep an eye on the size of your structs. Passing structs by value involves memcpy, and that will get expensive if you're doing it a lot with large structures.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocate memory to create 2D array
    By nonlinearly in forum C Programming
    Replies: 15
    Last Post: 11-10-2011, 02:22 AM
  2. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  3. Allocate memory for an Array
    By Coding in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2008, 06:18 PM
  4. typedef for pointer to array of char
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2003, 04:21 PM
  5. How to allocate memory in multidimensional array?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-15-2001, 10:07 AM

Tags for this Thread