1

So, I'm pretty inexperienced in C and am having trouble with this.

I have to initialize a certain number of objects as well as initialize two variables in each object. I have pre-written code that compiles and runs for a single object.

I had initially converted line:

MyObject object;

to

MyObject object[n]; //where n = # of objects needed

I altered other lines similarly such as:

mem = (unsigned char *)malloc( MEM_SIZE);
...
memset( &object, 0, sizeof(MyObect));
object.memory = mem;

to

mem = (unsigned char *)malloc(MAX_MEM_SIZE + (n*sizeof(MyObject)));
...
for( i = 0; i < n; i++){
    memset( &object[i], 0, sizeof(MyObect));
    object[i].memory = mem;
    object[i].var1 = i;
    object[i].var2 = n;
}

I keep getting a segmentation fault when I try to run the compiled program, even when I try to run it using only a single object and am not sure if it's a complete misunderstanding of what I want to do or something simple.

Mauren
  • 1,955
  • 2
  • 18
  • 28
Caulay
  • 43
  • 7
  • 1
    I'm confused about your code. What is `MEM_SIZE`? Why aren't you allocating `n * sizeof(MyObject)`? Can you show `MyObject` definition? – Mauren Mar 16 '14 at 19:45
  • 1
    Don't cast `malloc`s. – arshajii Mar 16 '14 at 19:45
  • 1
    To extend on what @arshajii said: [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Cristian Ciupitu Mar 16 '14 at 19:51
  • It is harmless but unnecessary to cast a malloc because it returns type void*, which can be assigned to any pointer without having to cast it. – James Mar 16 '14 at 19:57
  • 1
    We need the `struct`'s definition. What is `MyObject.memory`? A pointer? – EOF Mar 16 '14 at 19:59
  • It's hard to say what's wrong, but assigning the same 'mem' to every object[i] does not look right. If you are changing from one object to an array of them, I'd guess the malloc should be the same as it was before but inside the loop. – James Mar 16 '14 at 20:01
  • @Mauren Sorry about the vagueness, it's just not feasible to post the entire code involved. mem is an _unsigned char_ pointer, and MEM_SIZE is the maximum memory space that can be allocated per object. MyObject is a typedef struct that contains 6 _unsigned short_ (two of which are var1 and var2) and 1 _unsigned char_ (which is *memory that I mentioned above). – Caulay Mar 16 '14 at 20:02
  • It's hard to tell what is wrong without seeing all code. If you're on a *nix system, I'd advise to debug using `gdb`/`valgrind`. – Mauren Mar 16 '14 at 20:07
  • @Mauren Thanks for the info. I did run the program through gdb and found out where it was causing the seg. fault, although it wasn't as obvious or as helpful as I wanted. It turned out that I had: `MyObject_execute( &object[i] )` in a for loop where `i` determined how long the program ran and was trying to execute objects that didn't exist. – Caulay Mar 16 '14 at 23:41

1 Answers1

0
MyObject object[n];
for( i = 0; i < n; i++){
    char *mem = malloc(MEM_SIZE);
    memset( &object[i], 0, sizeof(MyObject));
    object[i].memory = mem;
    object[i].var1 = i;
    object[i].var2 = n;
}

You need to allocate a char* for every MyObject separately. Also, remember to free() each allocation afterwards.

Alternatively, you could malloc() a single, large buffer and manually assign parts of it to the MyObjects:

MyObject object[n];
mem = malloc(MEM_SIZE*n);
for( i = 0; i < n; i++){
    memset( &object[i], 0, sizeof(MyObject));
    object[i].memory = mem+n*MEM_SIZE;
    object[i].var1 = i;
    object[i].var2 = n;
}

But you'd have to be careful, and only free(object[0].mem), and only after all objects are no longer used.

Either way, you can hoist the memset() out of the loop:

MyObject object[n];
memset(object,0,sizeof(MyObject)*n);
...
EOF
  • 6,273
  • 2
  • 26
  • 50