Thread: pointer to array with dynamic allocation

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but where things are located in that block of memory depends on how you calculate the location in the memory - particularly when dealing with 2D arrays. Since you are also COPY NY+1 elements more than your original code, I'd say that it is possible that it's going wrong for that reason.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi again,

    No, even with that fixed (allocating the coherent number of elements), I still obtian the same allocation. The problem is surely in the part of routine where I assign the variable:

    Code:
    k=0;
    		for(j = 0; j < NY; ++j){
    			for(i = 0; i < NX; ++i){
    				vars[0][k] = zonal[j][i];
    			k++;
    			}
    		}
    and I don't know how to modify it correctly

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's what I meant by "copy" - you are writing NY * NX elements, not (NY-1) * (NX-1) elements to the new array. So the stride (step between each line) in the 1D layout is going to be different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi,

    yes, I understand what you mean, but I correctly it accordingly. Is the loop conceptually correct or am I assigning the wrong part of memory then?
    thank you again

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The original loop from the original code produces:
    Code:
    1 2 3 4
    1 2 3 4
    1 2 3 4
    Your new code produces:
    Code:
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    Now, if you use the write method to extract this, you will get:
    Code:
    1 2 3 4 
    5 1 2 3
    4 5 1 2
    written to disk. This assumes that I have understood things correctly. Does this make some sense compared to what you are seeing?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi again Mat,

    The error was exactly on that! Thank you so much! I did different tries to prove it against the original code, and yes, it was the NX-1, NY-1 where I was allocating a bigger memory!

    I can't believe I still make certain mistakes after years of programming This dynamic allocation is very powerful, but delicate at the same time if not taken well care of

    Thank you again for helping patiently

    Best regards
    CFD

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, it ISN'T the allocation itself that is wrong - it is the loop that copies the zonal data into the variables[0][k] that goes wrong because you are incrementing k one more than the write loop does for each row.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Correct, now it's all clear;

    thank you again
    Simon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  4. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM