Thread: Pointer jumping over adress in "uneven" array.

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    16

    Pointer jumping over adress in "uneven" array.

    Pointer jumping over adress in "uneven" array.-namnl-s-png

    I have no idea why my pointer skips over the colour addresses, it jumps to the next size address when I use Pointer++.
    I have tried changing the the char array to 4 bytes instead of 32 and whatnot but it doesn't help.

    If I set the Pointer = the first colour address, it skips over the size addresses and only get the colour addresses.

    I know using 2 arrays would easily solve everything, but sadly I must use only 1.

    I'll post whatever relevant code here:

    Pointer jumping over adress in "uneven" array.-namnl-s2-jpg
    Last edited by Sinery; 01-01-2015 at 07:45 PM. Reason: Reasons.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Your malloc call is totally wrong and is a perfect example of why you shouldn't cast the return of malloc(); not to mention the size calculation is wrong.
    Secondly, please do not post PICTURES of your code. Just post the damn code in [code] tags.

    Aside:
    Why the hell are you taking 'ArraySize' as a parameter to your function, but immediately discarding it and setting it to 1000? Hoping that is just debugging or something..

    Here is how you should be allocating your struct array:
    Code:
    struct MyStruct2 * ArrayPointer;
    ArrayPointer = malloc(sizeof *ArrayPointer * ArraySize);
    /* Or my personal favorite as it initialize the array of pointers to zero.. */
    ArrayPointer = calloc(sizeof *ArrayPointer, ArraySize);
    Last edited by nonpuz; 01-01-2015 at 08:16 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The short explanation is that you have failed to understand the basics of pointer arithmetic. Longer answer follows.


    Basic pointer arithmetic: incrementing a pointer to something goes to the next instance of that something. So incrementing ArrayPointer does not jump to the second member of the struct, it jumps to the next instance of the struct.

    In any event
    • The malloc() call should be "ArrayPointer = malloc(sizeof(MyStruct2) * ArraySize)" or the equivalent, less error prone "ArrayPointer = malloc(sizeof(*ArrayPointer) * ArraySize)". The reason this is right (and the size you provide is potentially wrong) is that the size of a struct can exceed the sum of sizes of its members (e.g. due to struct padding). This is also the reason you can't rely on struct members having particular offsets (i.e. your assumption that ShirtColour is offset by 4 is not always valid).
    • Note also that the type conversion on the malloc() is not needed in C - if it is, it means either that there is no "#include <stdlib.h>" (the real error) or that the C compiler is a C++ compiler (in which case, the #include <stdlib.h> is still required). In fact, using the type conversion if there is no #include <stdlib.h> causes the code to exhibit undefined behaviour.
    • The two printf() lines at the end of your second loop should be
      Code:
          printf("Colour: %s\n", ArrayPointer->ShirtColour);
          printf("Colour adress : %d // Not size adress+4, instead it jumps to next size address.\n", ArrayPointer->ShirtColour);
    • Even the above is not strictly correct. Printing a pointer should not be done with %d. Use %p instead and convert the address to a void pointer. For example, assuming you want to print some_address;
      Code:
          printf("Some Address: %p\n", (void *)some_address);
      If you are using an ancient compiler/library that doesn't support %p, then a workaround is
      Code:
          printf("Some Address: %d\n", (int)some_address);
      Never do this unless you absolutely have to (i.e. you're using an ancient compiler) as, strictly speaking, it invokes undefined behaviour.
    .
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    16
    Quote Originally Posted by nonpuz View Post
    Your malloc call is totally wrong and is a perfect example of why you shouldn't cast the return of malloc(); not to mention the size calculation is wrong.
    Secondly, please do not post PICTURES of your code. Just post the damn code in [code] tags.

    Aside:
    Why the hell are you taking 'ArraySize' as a parameter to your function, but immediately discarding it and setting it to 1000? Hoping that is just debugging or something..

    Here is how you should be allocating your struct array:
    Code:
    struct MyStruct2 * ArrayPointer;
    ArrayPointer = malloc(sizeof *ArrayPointer * ArraySize);
    /* Or my personal favorite as it initialize the array of pointers to zero.. */
    ArrayPointer = calloc(sizeof *ArrayPointer, ArraySize);
    Sorry about that, did search for a code tag but must have missed it.

    About the parameter, yeah I was testing how my memory responded to me setting it to different numbers manually like that but forgot about it and left it like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  2. Replies: 39
    Last Post: 07-28-2013, 08:24 PM
  3. "Subscript requires array or pointer type."
    By nrr5094 in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2013, 06:08 PM
  4. Replies: 18
    Last Post: 03-07-2013, 06:55 AM
  5. Replies: 14
    Last Post: 11-08-2010, 01:47 AM