Thread: Clarification of arrays and memory use

  1. #1
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50

    Clarification of arrays and memory use

    I read a post a couple of days ago, but I cannot remember the exact title - so I'll just start a new one instead of replying on that thread.

    In that post I believe that Salem alluded to the fact that memory management becomes more important the longer a program executes. A couple of replies down, Prelude said that all arrays and pointers should have memory properly allocated to them. I might've read this wrong, but it got me thinking.

    Does the compiler automatically allocate memory for me when I make a variable declaration?

    Code:
    char cty[31];
    char *pcty;
    Now, obviously I have to allocate the amount of memory I may need to pcty (a pointer), but do I have to also do this to cty (an array)? I may have just confused myself getting these replies mixed up, but somebody please help clarify my thoughts!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Arrays always have space allocated to them, so you always know that the memory exists, and how much there is.

    > char *pcty;
    The compiler reserves storage for the pointer itself (in this respect, its no different from any other type of variable).

    However, you still need to assign a value
    Code:
    char arr[20];   // an array
    char *p1 = arr;  // p1 points to the start of arr
    char *p2 = &arr[10];  // p2 points to the middle of arr
    char *p3 = malloc ( 20 );  // p3 points to 20 chars in dynamic memory
    Both p1 and p3 can be indexed with p[0] to p[19] and all would be well. p2 on the other hand only has p[0] to p[9] to play with.

    Like any variable, you need to initialise it before you use it. For an int say, the usual worst that can happen is you get junk printed
    Code:
    int random;
    printf( "%d\n", random );
    Start doing any serious work with the int, and pretty soon it will be clear that not all is well.

    Uninitialised pointers are far more dangerous, because the immediate problem isn't apparent (ie, it seems to work)
    Code:
    int *random;
    *random = 2;
    printf( "%d\n", *random );
    Assuming that the pointer dereference doesn't actually kill your program, chances are pretty high that it will consistently print 2.

    Like I said previously, this "seems to work" grace period can last an awful long time (like years) before it bites you.
    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.

  3. #3
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Thank you, Salem, for that reply. I was simply confusing myself with what I read. Maybe you could help me with another problem.

    I'm currently writing a program that will execute for almost an hour and I'm starting to think about performance. I'm working mostly with arrays to hold data that is found in a text file. The text file is defined to have an exact structure - each line is exactly 81 bytes long and within each line there are "fields" that hold the data I need.

    I'm using 12 arrays to hold information from each line read. From a performance perspective, would it be better to use arrays delcared to have an exact size or to use pointers and dynamically allocate the memory I may need?

    What other performance issues should be recognize when designing an application with such a long execution time?

    Thank you.

  4. #4
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    You could make a structure with pointers to each field, i.e.

    [code]
    typedef struct {
    char *data;
    char *field;
    } text;
    [code]

    then init the thing:

    Code:
    text->data = malloc(81 * sizeof(char) + 12 * sizeof(char *));
    text->field[0] = text->data[wherever field #0 starts];
    text->field[1] = text->data[wherever field #1 starts];
    etc.
    So to print the first char in field #4, use:
    Code:
    putchar(*(text->field[4]));
    You could even use a much larger chunk than 81 chars, and use pointers to each line, then pointers to each field. That allows you to get direct access to each line/field within the chunk. (You could load in a 10MB file, process it, and write it back in one clean sweep, for example.)

    -Joe

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > and I'm starting to think about performance
    Well now is also the time to stop thinking about it.
    click me
    The kind of tradeoff you mention will have minimal effect on the overall performance of the program, unless you spend the whole hour calling malloc/free.

    Satisfy the first two points, then get the program finished, debugged and working.

    Then use a profiler to determine real (not guessed) hot spots, and make measured changes to your code to improve things.
    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. how to create adjacent arrays in memory?
    By sudeepta in forum C++ Programming
    Replies: 3
    Last Post: 07-28-2008, 09:03 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM