Okay, to be honest, I don't know if its the free but thats where, after a lot of printf the code stops working.

First off, I declare in main: unsigned char** imgSet and int n=0.

void loadImage(unsigned char*** imgSet, int *n) {
	char fileName[80];
    printf("Enter filename: ");
    scanf("%s",fileName);
	addImage(imgSet,&(*n));
    readImage(&fileName,&(*imgSet)[(*n)-1]);
	printf("File %s has %d rows and %d columns\n",fileName,(*imgSet)[(*n)-1][0]+1,(*imgSet)[(*n)-1][1]+1);
}

void addImage(unsigned char*** imgSet, int *n) {
	unsigned char **temp;
	temp = (unsigned char **)malloc(((*n)+1)*sizeof(unsigned char *));
	
	int i;
	for(i=0;i<(*n);i++)
		temp[i] = (*imgSet)[i];
	
	free((void *)(*imgSet));
	
	(*imgSet) = temp;
	
	(*n)++;
}

I pass to loadImage using: loadImage(&imgSet, &n). My first initial load works, however, when I attempt to load a new image, I get a fault. I actually tried to run thru all the first 2D index at the beginning of each loop iteration in main (eg imgSet[0]). I also get a fault there.

Why can't they just easily explain malloc online? Everything about it is so obtuse, it's ridiculous. Perhaps I'm finding the wrong sites...

I don't mean to be harsh, but it looks like you're doing the same thing as last week. It's time to put that shovel away, and quit digging a deeper hole, isn't it?

You have a notion that you can do this, a certain way, and clearly, you can't. I can't quite wrap my head around what it is exactly that you're trying to do, but at some point it will become evident, that you need to step back, and use a different approach.

This is the Borland help file and example, from Turbo C/C++. This is as basic and clear as it gets, I believe:

malloc Allocates memory.


Syntax:
void *malloc(size_t size);

Prototype in:
alloc.h stdlib.h

Remarks:
malloc allocates a block of size bytes from
the memory heap. It allows a program to
allocate memory explicitly as it's needed, and
in the exact amounts needed.

The heap is used for dynamic allocation of
variable-sized blocks of memory. Many data
structures, such as trees and lists, naturally
employ heap memory allocation.

All the space between the end of the data
segment and the top of the program stack is
available for use in the small data models,
except for a small margin immediately before
the top of the stack.

This margin is intended to allow the
application some room to make the stack
larger, in addition to a small amount needed
by DOS.

In the large data models, all the space beyond
the program stack to the end of available
memory is available for the heap.

Return Value:
On success, malloc returns a pointer to the
newly allocated block of memory.

If not enough space exists for the new block,
it returns null. The contents of the block are
left unchanged.

If the argument size == 0, malloc returns
null.

Portability:
malloc is available on UNIX systems and is
defined in ANSI C.

See Also:
allocmem coreleft farmalloc realloc
calloc farcalloc free

Example:

#include <stdio.h>
 #include <string.h>
 #include <alloc.h>
 #include <process.h>

 int main(void)
 {
    char *str;

    /* allocate memory for string */
    if ((str = malloc(10)) == NULL)
    {
       printf("Not enough memory to allocate buffer\n");
       exit(1);  /* terminate program if out of memory */
    }

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
 }

Ignore the DOS comments about where the heap resides - it's not relevant, nor accurate for Windows.

When malloc returns an address, to the pointer, any prior address that pointer had, is gone - long gone - off to pointer heaven. Making the malloc'd memory request twice as big, the second time around (and etc.), does not change that, in the least. You'll just crash the system, before long. (or as soon as you try to access a former pointer address).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.