char *glblclrtab; by itself will not allocate any memory to be use.malloc() must be invoked to set apart as much memory as needed to hold the data you intend to assigned (point) to it.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
1. Please, USE code tag for your snippets properly:
[code=c]
your source texts
[/code]
2. You don't understand operator sizeof functionality. See what happens in your (incorrect) memory allocation:
glblclrtab= (char *)malloc(sizeof(3*tmp));
The malloc functions allocates[icode]sizeof(3*tmp)[/code] bytes.
The C Standard:
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand.
Suppose the variable tmp is int, integer literal 3 is int by definition. Therefore a type of an expression tmp*3 is int. Now sizeof(tmp*3) == sizeof(int) . For example, you allocate 4 bytes only in 32-bit environment.
If you want allocate tmp*3 bytes, write
glblclrtab= malloc(3*tmp);
/* No need to convert void* to char* in C explicitly */
3. What's a horrible name -glblclrtab! Try to pronounce it ;)...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
If I read the code right, the intent is to split up color table values?
bytes in the file come as r,g,b,r,g,b...
You want the array to contain r,r,r...g,g,g...b,b,b...
Is that close?
if so, what do you think of:
char *glblclrtab;
glblclrtab= malloc(3*tmp);
for (int i=0; i < tmp; i++)
{
glblclrtab[i] = fgetc( ipFile);
glblclrtab[i + tmp] = fgetc( ipFile);
glblclrtab[i + 2 * tmp] = fgetc( ipFile);
}
for (int i = 0; i < tmp; i++)
{
printf("%d::%d, %d, %d \n",
i,glblclrtab[i], glblclrtab[tmp + i],glblclrtab[2*tmp+i]);
}
free (glblclrtab); // program nolonger aborts
note that *(glblclrtab + i) is functionally equivalent to glblclrtab[i] but the second is way easier to read.
What was the variable clrcnt in your code?
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
malloc is the right solution, but you can treat a pointer to allocated memory like an array of the same type. As I said, the expressions are interchangeable, I just prefer the second.
isn't tmp the number of bytes between sections?
the code never updated clrcnt so if it was zero, all data is stored in the first entry in the table. If it was non-zero, all the data is stored at that index.
This would work
int clrcnt = 0;
for (int i=0; i<(3*tmp); i++)
{
if (tmp2==3)
{
tmp2=0;
clrcnt++;
}
i1=fgetc( ipFile );
if (tmp2==0) {
*(glblclrtab + clrcnt) = (char)i1;
}
else if (tmp2==1) {
*(glblclrtab + tmp +clrcnt) = (char)i1;
}
else if (tmp2==2) {
*(glblclrtab + 2*tmp + clrcnt)= (char)i1;
}
tmp2++;
}
This is cleaner:
int clrcnt = 0;
for (int i=0; i<tmp; i++)
{
i1=fgetc( ipFile );
*(glblclrtab + clrcnt) = (char)i1;
i1=fgetc( ipFile );
*(glblclrtab + tmp +clrcnt) = (char)i1;
i1=fgetc( ipFile );
*(glblclrtab + 2*tmp + clrcnt)= (char)i1;
clrcnt++;
}
now that i and clrcnt always have the same value:
for (int clrcnt=0; clrcnt<tmp; clrcnt++)
{
i1=fgetc( ipFile );
*(glblclrtab + clrcnt) = (char)i1;
i1=fgetc( ipFile );
*(glblclrtab + tmp +clrcnt) = (char)i1;
i1=fgetc( ipFile );
*(glblclrtab + 2*tmp + clrcnt)= (char)i1;
}
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
> I thought arrays are always assigned contiguous blocks of memory.
They are.
As are all blocks of memory returned by malloc / calloc / realloc
The link posted by death_oclock in the previous post is just a load of rubbish.
They seem to think that because the OS might map parts of the array to different pages of memory, that it has any effect on a C program. It DOESN'T. The virtual address is contiguous, and that's all that matters.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953