| | |
pointer assignment
![]() |
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
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.
...
...
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
•
•
•
•
Thanks, I had not pionted "glblclrtab". This is just an array to store the color values from file.
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.
When you think you have done a lot, then be ready for YOUR downfall.
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
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:
The malloc functions allocates [icode]sizeof(3*tmp)[/code] bytes.
The C Standard:
Suppose the variable tmp is int, integer literal 3 is int by definition. Therefore a type of an expression
If you want allocate tmp*3 bytes, write
3. What's a horrible name - glblclrtab! Try to pronounce it
...
[code=c]
your source texts
[/code]
2. You don't understand operator sizeof functionality. See what happens in your (incorrect) memory allocation:
c Syntax (Toggle Plain Text)
glblclrtab= (char *)malloc(sizeof(3*tmp));
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.
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
c Syntax (Toggle Plain Text)
glblclrtab= malloc(3*tmp); /* No need to convert void* to char* in C explicitly */
... •
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
I made the changes as:
I could eliminate the error that was coming from free(). The program nolonger aborts.
However, I still do not get the values as stored.
c Syntax (Toggle Plain Text)
glblclrtab= malloc(3*tmp);
However, I still do not get the values as stored.
Last edited by m24r; Dec 23rd, 2008 at 3:34 pm.
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
I made the changes as:
I could eliminate the error that was coming from free(). The program nolonger aborts.
However, I still do not get the values as stored.
c Syntax (Toggle Plain Text)
char *glblclrtab; glblclrtab= malloc(3*tmp); for (int i=0; i<(3*tmp); i++) { // effort to sort the data from file, size unknown (tmp), I know when I parse the file 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++; } for (int i=0; i<(tmp); i++) { printf("%d::%d, %d, %d \n",i,*(glblclrtab+i),*(glblclrtab + tmp + i),*(glblclrtab+2*tmp+i)); // [U][B]I receive garbage values here[/B][/U] } free (glblclrtab); // program nolonger aborts
However, I still do not get the values as stored.
![]() |
Similar Threads
- Error:Null Pointer assignment (C++)
- getting null pointer assignment error (C)
- In which case "Null Pointer Assignment" will be displayed on the output ?? (C)
- Why this C code is returning Null Pointer Assignment as output (C)
- Assign content to a function pointer (C)
Other Threads in the C Forum
- Previous Thread: upnp related project topics
- Next Thread: Please help me
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






