![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#12 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
If you want to push it, we could also make isa an array of size_t instead of an array of pointers, viz; #include <stdlib.h>
int main()
{
size_t isa[30];
/* declare and initialise opcode, args, type */
/* and later with a value of k ..... */
isa[k] = (size_t) insertinstrlist((instr *)(isa + k), opcode, args, type);
} |
|
|
|
|
|
|
#13 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Generally, I am a peaceful person. But some things wind me up.
And there are ways to shoot people without a gun ...... |
|
|
|
|
|
#15 |
|
Programmer
|
Really grateful for all the help you guys have given me, am much more confident with pointers now. Just a couple more q's...
You've got me a little worried with talk of memory leaks! After I've initialised the isa array and entered all the values into it, and this bit of code works in that it prints out what I'd expect it to: char opcode[5];
strcpy(opcode, isa[13].opcode);
printf("%s", opcode);Does that rule out the possibility of a memory leak? Also, would be very grateful if someone could explain what is meant by the term "cast". I see it everywhere and it's kind of assumed in every tutorial I read that I'll know what it means so feel a bit stupid asking that, but still...! Thanks again. |
|
|
|
|
|
#16 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
Quote:
int i = 65; char c; c = (char) i; You can cast all kind of types, in C++ you can do more casts, like static_cas, const_cast, dynamic_cast and reinterpret_cast.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
||
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Let me expand just a tad, to avoid a misinterpretation: the cast doesn't change the type of the variable it's applied to, it forces that variable to be interpreted as another type, if possible. In Ruben's example, i remains an int. Its value is demoted to char and assigned to c on the fly, so to speak.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#18 |
|
Programmer
|
Ok thanks, I always like to make sure I understand what's going wrong in programs when I get stuck as well as just getting them to work, so I don't come pestering you guys again with the same problem!
Thanks again for all the help. |
|
|
|
|
|
#19 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
A memory leak is what happens if we do this; some_type *x = malloc(some_size);
// later lose track of x and never delete it#include <stdlib.h>
int *Allocate()
{
return malloc(2*sizeof(int));
}
int main()
{
int i;
for (i = 0; i < 5; ++i)
Allocate();
/* we no longer keep track of memory allocated by Allocate() so cannot
release it. That memory is said to be leaked */
}Quote:
It may seem like I'm talking about conversions, not casting, here. But bear with it. In practice, possible conversions from one type to another fall into three categories; 1) Conversions that are always possible. These are specified in the C standard as "implicit conversions". For example, the following code yields no complaints from a self-respecting compiler; void SomeFunction(int);
main()
{
char x =65; /* Letter A in ASCII character set */
int y;
y = x; /* implicit conversion of x to int */
SomeFunction(x); /* implicit conversion of x to int */2) Conversions that are sometimes possible. These are usually flagged as conversions that lose precision (eg converting from a larger type to a smaller type). An example would be; void SomeFunction(char);
main()
{
int x = 165; /* value bigger than can be held in a char */
char y;
x = y; /* loss of precision here */
SomeFunction(x); /* loss of precision here */void SomeFunction(char);
main()
{
int x = 165; /* value bigger than can be held in a char */
char y;
x = (char)y; /* loss of precision here */
SomeFunction((char)x); /* loss of precision here */void SomeFunction(char);
main()
{
int x = 165; /* value bigger than can be held in a char */
char y;
if (x > -128 && x < 127) /* check valid range of char */
{
x = (char)y;
SomeFunction((char)x);
}
else
do_something_else();3) Conversions that are, technically, disallowed by the standard but we want to do them anyway. void SomeFunction(long *);
int main()
{
short *px;
char a[2], *pa;
pa = a;
px = pa; /* Error. Code will not be allowed to compile */
*px = 100;
SomeFunction(pa); /* Error. Code will not be allowed to compile */
}void SomeFunction(short *);
int main()
{
short *px;
char a[2], *pa;
pa = a;
px = (short *)pa;
*px = 100;
SomeFunction((short *)pa);
}void SomeFunction(long *);
int main()
{
long *px;
char a[2], *pa;
pa = a;
px = pa; /* Error. Code will not be allowed to compile */
*px = 70000;
SomeFunction(pa); /* Error. Code will not be allowed to compile */
}void SomeFunction(long *);
int main()
{
long *px;
char a[2], *pa;
pa = a;
px = (long *)pa;
*px = 70000; /* Undefined behaviour here, as 70000 cannot be stored in two bytes */
SomeFunction((long *)pa);
}Basic message: just because we can use a cast to beat the compiler into submission and force it to do a conversion, doesn't mean we should. As a general rule, if a compiler complains, it is doing you a service. Just telling it to shut up, and not fixing the underlying problem, is asking for trouble. Yes, casts can be (and, in some cases, have to be) used for conversion. But you - the programmer - need to be sure the conversion is actually valid as their real purpose is to stop the compiler complaining about things it wouldn't normally allow. |
||
|
|
|
|
|
#20 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Very nicely explained grumpy.
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|