initializing a 2D array in C

Started by Shadow, April 29, 2009, 01:52:11 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Shadow

would this work? I want a N x 4 array of doubles. I am still getting used to this pointer business. Out of place curly breackets are there to avoid forum tags.

  double **particleList;

  particleList = (double **) malloc(N * sizeof(double *));
  if(NULL == particleList)
  {
     free(ParticleList);
     printf("Memory allocation failed while allocating for particleList[].\n");
     abort();
  }
  for(i = 0; i < N; i++)
  {
    particleList{i} = (double *) malloc(4 * sizeof(double));
    if(NULL == particleList{i})
    {
     free(particleList{i});
     printf("Memory allocation failed while allocating for particleList{x}[].\n");
     abort();
    }
  }
<=holbs-.. ..-holbs=> <=holbs-..

bjornredtail

Ahhh... Pointer notation! *brain implodes*


Acctually, I understand what it does... But, the problem is unless you know the size of both arrays at the start, you can't do it all in a single malloc call, since you don't know the size of the inner 'array'. In this case, you are basically allocating a space for n number of double pointers. Each of those would have to have space allocated to it if you wanted to use it.
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

Shadow

#2
I think I did it without pointers - all it took was initializing the array in a different function and then passing it to be modified in this one. Which works even better. Incidentally, coding is realy cool when you start to get the hang of it. I cna now generate the x,y,z coordinates of all the particles in a cubic fcc crystal lattice of almost any size (much over 200,000 points and my computer segfaults, but I assume the lab can handle bigger ones).
<=holbs-.. ..-holbs=> <=holbs-..

bjornredtail

Quote from: Shadow on April 30, 2009, 05:00:04 PM
I think I did it without pointers - all it took was initializing the array in a different function and then passing it to be modified in this one. Which works even better. Incidentally, coding is realy cool when you start to get the hang of it. I cna now generate the x,y,z coordinates of all the particles in a cubic fcc crystal lattice of almost any size (much over 200,000 points and my computer segfaults, but I assume the lab can handle bigger ones).

You probably have a resource leak then. Try Valgrind on your code...
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

Shadow

<=holbs-.. ..-holbs=> <=holbs-..

bjornredtail

Valgrind is a tool designed to track your memory usage, to report stuff like memory leaks...
http://valgrind.org/

Though, on second though, I might have been wrong about that being a memory leak. Are you checking errno after malloc for E_NOMEM? You could be right about it not having the memory to allocate... Then, when it attempts to use the memory it thought it allocated.. SEGFAULT! (or, rather, SIGSEGV on Unix systems).


I find it amazing that you have managed to pick up so much about C so quickly. It's a real beast of a language to learn if you don't know what's going on underneath.
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

Shadow

#6
It's actually a pretty simple algorithm once you work out when to increment things. I don't know much about C - I understand a lot of theory witohut having a clue how to go about implementing it. That was a pain. But right now, even if there was a memory leak, I don't see how that could be the cause of the segfault. Because all the program currently does is make an array, populate it, print it and exit. So the memory is freed automatically after the exit, so how would that leak have any effect even if it is there? Plus I didn't malloc it, so I can't free it anyway, right? [\i] is to avoid forum tag.



/*generates the coordinates of all poarticles in an fcc lattice. In the definitions,PARTICLE_MAX must be an integer and represents the
number of cells in each side of the cube, or the maximum diameter in cell lengths of a sphere built within the cube.
N is (4 * PARTICLE_MAX ^ 3 + 6 * PARTICLE_MAX ^ 2 + 3 * PARTICLE_MAX + 1). Any number generated by that formula  with PARTICLE_MAX an
integer will give a perfect fcc cube. */


#include <stdio.h>
#define PARTICLE_MAX 5
#define DISTANCE 0.7071067811865475244
#define N 666

void fcc_generator(double particleList[N][4])
{
  int i,particleCount,rowCount,planeCount;
  double x,y,z;
  x = 0.0;
  y = 0.0;
  z = 0.0;
  i = 0;
  particleCount = 0;
  rowCount = 0;
  planeCount = 0;

  for (i = 0; i < N; i++)
  {
    particleList[\i][0] = x;
    particleList[\i][1] = y;
    particleList[\i][2] = z;
    particleList[\i][3] = x*x + y*y + z*z;
    particleCount++;
    x += 2 * DISTANCE;
   
    if ((particleCount == PARTICLE_MAX + 1) && ((rowCount % 2 == 0 && planeCount % 2 == 0) || (rowCount % 2 != 0 && planeCount % 2 != 0)))
    {
      particleCount = 0;
      rowCount++;
      y += DISTANCE;
      x = DISTANCE;
    }
    else if ((particleCount == PARTICLE_MAX) && ((rowCount % 2 == 0 && planeCount % 2 != 0) || (rowCount % 2 != 0 && planeCount % 2 == 0)))
    {
      particleCount = 0;
      rowCount++;
      y += DISTANCE;
      x = 0;
    }
    else ;

    if (rowCount == 2 * PARTICLE_MAX + 1)
    {
      rowCount = 0;
      z += DISTANCE;
      y = 0;
      planeCount++;
    }
  }
}   

int main()
{
    int i = 0;
    double particleMatrix[N][4];
    fcc_generator(particleMatrix);

    for (i = 0; i < N; i++)
    {
      printf("%3.5f\t\t%3.5f\t\t%3.5f\t\t%3.5f\t\t\n",particleMatrix[\i][0],particleMatrix[\i][1],particleMatrix[\i][2],particleMatrix[\i][3]);
    }
  return 1;
}
<=holbs-.. ..-holbs=> <=holbs-..

bjornredtail

Oh, no malloc. Therefore, no 'leak' per-se.

Um... I'll look at the code later.
0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

Shadow

#8
Problem solved - the array was too big to be passed in its entirety, so I had to use malloc after all. Works fine now. Apparently you can't pass a parameter bigger than 8MB to a function. Not that any sane person would want towhen you can just pass the pointer. Which took me a while to learn.
<=holbs-.. ..-holbs=> <=holbs-..

Ungatt Trunn II

See it's this kind of stuff it would take me months to even slightly understand, maybe more....
DIE HIPPIE DIE

bjornredtail

Yeah, that will chew through your stack frame pretty quickly. Plus, it's inefficient. That's why C++ has references..

0==={=B=J=O=R=N=R=E=D=T=A=I=L==>
AKA, Nevadacow
First person to ever play RWL

"Program testing can be used to show the presence of bugs, but never to show their absence!"-Edsger W. Dijkstra

Visit http://frostnflame.org today!

Shadow

Thanks for the valgrind tip btw, that is what solved it for me.
<=holbs-.. ..-holbs=> <=holbs-..