Thread: Peculiar malloc / command argument question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Peculiar malloc / command argument question

    Here is my problem - I need to create an array of size X, where X is an argument from the command line (argv[1]), and each array row can hold 80 bytes worth of data. But I must use malloc to make sure that there is enough space to store this array, in case the user supplies some very large number in the command line argument. My problem is that I can't figure out how to specify that each row can only hold 80 bytes, and that array sizes have to be constants at compile time, but i'm getting my size from a command line argument. Here is what I have so far (some error checking code has been omitted):

    Code:
    char (*aa)[80]; // 80 bytes
    int stackSize;
    
    stackSize = argv[1];
    
    aa = malloc(stacksize * 80);
    // will return null if malloc cannot allocate enough space
    This is what someone else has helped me with - I do not understand why 80 would be in the brackets? I thought whatever size (how many rows) I wanted the array to be (i.e. what I get from the command line argument) would be in the brackets, but I can't do this because it is not known at compile time.

    Any help is much appreciated

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why not ask your friend what he was doing?

    What's the difference between a pointer to an array and a pointer to the first element of an array? To the computer, nothing. To the compiler, it's a lot.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    So my code is creating a pointer to an array with 80 rows. Say the user supplied 10 in the command line argument, he wanted the stack to have 10 rows. malloc would find memory space for 800 bytes, since 10 rows of potentially 80 bytes each would be 800. So then would I create an array of size 10 and point it to the malloc space I just allocated? Something like this?

    Code:
    int main(int argc, char *argv[])
    {
    	char (*aa)[80];
        int stackSize;
    
        stackSize = atoi(argv[1]);
    
        aa = malloc(stackSize * 80);
    
        char bb[stackSize];
    
        aa = bb;
    }
    I am still confused with the '80' in char (*aa)[80] however. Why does aa even need to be an array if it is just going to be pointing to a 800 byte location of memory?

    edit: more thinking...

    Code:
    char (*aa)[80];
    int stackSize;
    
    stackSize = atoi(argv[1]);
    // error checking omitted
    
    aa = malloc(stackSize * 80);
    
    char (*bb)[stackSize];
    
    bb = aa;
    So if stackSize was 10, then aa would point to a memory location of 800 bytes, bb would be created to point to an array of size 10, and then that array would be pointed to the allocated 800 bytes worth of memory.
    Last edited by Beowolf; 09-10-2007 at 10:03 PM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    I have no idea what your friend is doing either. That almost looks like an array of pointers to functions, returning type char.

    Allocating a single chunk is nothing. It's just malloc (rowsize * numrows).

    Do you need to access this memory as discreet rows of 80 bytes where each row has it's own pointer? I've seen that done for better memory management when allocating lots of small items individually would create a performance degrading memory fragmentation.


    NOTE: when you assign bb to aa using aa = bb, you're assigning the address of bb to aa and losing track of aa's allocation. That's a memory leak.

    I'm looking more at your post.

    I think what you want is:

    char *bb[argv[1]];

    or...

    numrows = argv[1];
    char *bb[numrows];


    Then you loop through to assign each row pointer to each 80 byte segment.

    Code:
    for (i = 0; i < argv[1]; i++)
        bb[i] = &aa[i*rowsize];
    I haven't tested this, so while my ideas work, they rarely work the first time. But that seems about right.
    Last edited by Cynic; 09-10-2007 at 11:20 PM. Reason: adding more help.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > aa = malloc(stacksize * 80);
    Have you read the FAQ yet?
    So it would be
    aa = malloc ( stacksize * sizeof *aa );

    It's the dynamic memory equivalent of
    char aa[stacksize][80];

    > I do not understand why 80 would be in the brackets?
    Precedence
    char (*aa)[80]; declares aa as a pointer to an array of 80 chars
    char *aa[80]; declares aa as an array of 80 pointers to char
    The ( ) are needed to associate the array size with the thing being pointed to, rather than the pointer itself.
    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. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM
  4. another malloc question
    By Ian in forum Linux Programming
    Replies: 4
    Last Post: 10-16-2001, 08:46 PM
  5. malloc question
    By Ian in forum Linux Programming
    Replies: 3
    Last Post: 10-13-2001, 01:31 AM