have this pointer
Char *ptr
i allocate memory as
ptr = (char *) malloc (sizeof(char) * 100);
say later on in the program i may or may not need to store more characters so i'll use realloc to assign more memory. How can i find out how much memory is currently assigned to ptr so i cant find if i need to resize it or not.
Name:
Anonymous2008-10-21 23:50
1. You don't have to cast the return value of malloc.
2. You store the size in an integer for later use. Perhaps create a struct with an integer for the size and the pointer.
Your pointer is a pointer to Char. Since it's not given what `Char' is, I assume it's an opaque type, therefore you should allocate N * sizeof Char where N > 0.
I think you're trying to write a string library; don't do it unless it's an assignment.
Take a look at something like this http://bstring.sourceforge.net/
if you want a good string management library in C.
Else, the starting scheme is
struct String { char *s; size_t size; };
>>7
Yes. The semantics of void * differ for each language.
Name:
Anonymous2008-10-22 8:27
GUYS I JUST DISCOVerED SOMETHING THATS BEEN AROUND FOR 30 YEARS ITS SOOOOOOOOOO COOOOOOOOOL EVERYONE SHOULD USE $CURRENT_POPULAR_LANGUAGE!!!!!
>>7
Yes. The C way is great for C programs, and the C++ way is great for C++ programs. However, since malloc() is a C function and should almost never be used in a C++ program, it's better to assume C semantics and state that the cast is not required.
When you think about it, casts are never really required for a pointer, because all pointers take the same amount of memory, but allowing conversions to and from void* pointers without error or warning makes things easier.
Name:
Anonymous2008-10-22 12:09
>>15
I free malloced blocks with delete, then shit on the whole thing, then swallow and regurgitate it, then use goto to go back to the first step.
Name:
Anonymous2008-10-22 12:11
>>15
>because all pointers take the same amount of memory
>>4,6
"It is not
legal to add two pointers, or to multiply or divide or shift or mask them, or to add float or
double to them, or even, except for void *, to assign a pointer of one type to a pointer of
another type without a cast."
Suck on this. K&R 2nd edition.
Name:
Anonymous2008-10-22 12:37
what the fuck
Name:
Anonymous2008-10-22 12:45
I'm hopin' you don't take this the wrong way
But your body is bangin' got me attracted in a strong way
Name:
Anonymous2008-10-22 12:57
>>18
Suck on your mom's dick instead of on obsolete books for a change.
Name:
Anonymous2008-10-22 13:22
>>21
Is that even relevant to the topic or just your way of saying "Your mothers got a penis"
Name:
Anonymous2008-10-22 13:43
reinterpret_cast<>() is a beautiful construct.
Name:
Anonymous2008-10-22 13:45
>>23
I throw up a little in my mouth each time I see this. Does that mean I'm not cut out for Sepples?
Name:
Anonymous2008-10-22 14:10
struct life {
struct car car;
struct girlfriend gf;
struct computer comp;
struct food food;
}
struct life *
create_my_life(void)
{
struct life *life;
life = malloc(sizeof *life);
if(life == NULL) {
exit(EXIT_FAILURE);
}
return life;
}
Name:
Anonymous2008-10-22 14:49
>car girlfriend
No thank you, I pay enough bills at it is.
Having a bunch of useless horse-shit doesn't mean you have a life. Or maybe "having a life" is a weak idea to begin with.
Name:
Anonymous2008-10-22 14:55
>>26
The idea of living a standard-conforming life is highly overrated in today's society.
Name:
Anonymous2008-10-22 15:17
Life is about having fun. If you can maximize the average amount of fun that you have over your life, then you have a good life. But if you fucking sit at home all day and be a COMPUTER NERD then you will have a bad life.
Name:
Anonymous2008-10-22 15:29
OP -- if you want to write sepples, you write
std::vector<char> x(100);
char * ptr = &x[0]; // if you have a 'C' based API that needs a raw pointer
remembering to catch std::bad_alloc, and resetting ptr each time you change the size of the vector.
>>30
As long as he doesn't modify the contents of the vector (which is unlikely if he's passing a pointer into a C function) then nothing is going to go wrong. Implicit reallocs -- another reason why Sepples is unscientific and ultimately destructive.
Name:
Anonymous2008-10-22 17:34
You're all doing it wrong.
OP, you can usually find the size of malloc'd memory 4 bytes before its start, so subtract 4 from the pointer and read that as an int. If it appears negative, just change its sign.
If you're compiling for 64-bit, you might have to change change 4 to 8.
>>17
Ah, so Sepples' pointers can be different sizes. Great. That's why Sepples requires casts to/from void*, and C doesn't, because in C all pointers are the same size.
OK, people, we've solved it. Nothing to see here.
Name:
Anonymous2008-10-23 17:55
>>45
Wrong. The char pointers and void * must have the same size, and so do all struct pointers; but otherwise different pointer types can have different sizes.
Name:
Anonymous2008-10-23 19:39
>>46
In C++, yes. Show me some C code that demonstrates pointers of different sizes, and I'll believe you.
>>47
all pointers are the same size in most implementations, but the only thing the standard guarantees about the sizes of pointers is that void * and intptr_t (and therefore also intmax_t) are large enough to hold the value of any pointer.
Name:
Anonymous2008-10-23 20:47
>>45
No, it's just that Sepples requires explicit downcasts. This is because Sepples strives to have a stronger type system than C, even if only slightly.