943,822 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2602
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 21st, 2008
0

pointer assignment

Expand Post »
I am a novice in C. I am trying to assign a value from a file to a pointer as below. However I am gettign a seg fault. Can somebody explain ?

int i1;
char *glblclrtab;
...

for (int i=0; i<20; i++)
{ while ( (i1= fgetc(ipFile))==NULL);
*(glblclrtab+i)= char (i1);
...
}

The pointer assignment in causing segmentation fault.

PLZ HELP
Thanks in Advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008
Dec 21st, 2008
0

Re: pointer assignment

What's glblclrtab pointing to? It'll have a NULL value or a garbage value which is causing segmentation fault. After you make it point to a character array, you can use *(glblclrtab+i)= (char) i1; or glblclrtab[i]= (char) i1; for simplicity.

By the way, use code tags while posting your code.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Dec 21st, 2008
0

Re: pointer assignment

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.
Last edited by Aia; Dec 21st, 2008 at 4:00 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 21st, 2008
0

Re: pointer assignment

Thanks, I had not pionted "glblclrtab". This is just an array to store the color values from file.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008
Dec 23rd, 2008
0

Re: pointer assignment

Click to Expand / Collapse  Quote originally posted by Aia ...
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.
I am using malloc now, however when I read the data back it gives garbage data. Following is the part of the code:

...
...
glblclrtab= (char *)malloc(sizeof(3*tmp));

for (int i=0; i<(3*tmp); i++)
{
if (tmp2==3)tmp2=0;
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++;
}
free (glblclrtab);


When I use free as shown above, program dumps backtrace memory range in the end and aborts.

Thanks in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008
Dec 23rd, 2008
0

Re: pointer assignment

Click to Expand / Collapse  Quote originally posted by m24r ...
Thanks, I had not pionted "glblclrtab". This is just an array to store the color values from file.
You want 'glblclrtab' to be an array, but C compiler have no idea what your intentions are. So allocate memory before storing.
As arrays have predefined space in the memory, we do not allocate memory for them but in your case its need to be done manually.

You have no idea where 'glblclrtab' is pointing in the memory space. and neither anyone in the world have.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Dec 23rd, 2008
0

Re: pointer assignment

That is is reason I used the malloc before storing in the for() statement. However, I cannot use array as the size is not predefined. I have to use it on the fly. I had assigned glblclrab as char pointer as:

char *glblclrtab;

I do not know what is the right way of using the pointer here.
Last edited by m24r; Dec 23rd, 2008 at 2:06 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008
Dec 23rd, 2008
0

Re: pointer assignment

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:
  1. glblclrtab= (char *)malloc(sizeof(3*tmp));
The malloc functions allocates [icode]sizeof(3*tmp)[/code] bytes.
The C Standard:
Quote ...
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
  1. glblclrtab= malloc(3*tmp);
  2. /* No need to convert void* to char* in C explicitly */
3. What's a horrible name - glblclrtab! Try to pronounce it ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 23rd, 2008
0

Re: pointer assignment

I made the changes as:
  1. glblclrtab= malloc(3*tmp);
I could eliminate the error that was coming from free(). The program nolonger aborts.

However, I still do not get the values as stored.
Last edited by m24r; Dec 23rd, 2008 at 3:34 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008
Dec 23rd, 2008
0

edited: pointer assignment

I made the changes as:
  1. char *glblclrtab;
  2. glblclrtab= malloc(3*tmp);
  3. for (int i=0; i<(3*tmp); i++)
  4. { // effort to sort the data from file, size unknown (tmp), I know when I parse the file
  5. if (tmp2==3)tmp2=0;
  6. i1=fgetc( ipFile );
  7. if (tmp2==0) {*(glblclrtab + clrcnt) = (char)i1;}
  8. else if (tmp2==1) {*(glblclrtab + tmp +clrcnt) = (char)i1;}
  9. else if (tmp2==2) {*(glblclrtab + 2*tmp + clrcnt)= (char)i1;}
  10. tmp2++;
  11. }
  12.  
  13. for (int i=0; i<(tmp); i++)
  14. { printf("%d::%d, %d, %d \n",i,*(glblclrtab+i),*(glblclrtab + tmp + i),*(glblclrtab+2*tmp+i));
  15. // [U][B]I receive garbage values here[/B][/U]
  16. }
  17. free (glblclrtab); // program nolonger aborts
I could eliminate the error that was coming from free(). The program nolonger aborts.

However, I still do not get the values as stored.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: upnp related project topics
Next Thread in C Forum Timeline: Please help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC