Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

allocating memory

Name: Anonymous 2008-10-21 23:16

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: Anonymous 2008-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.

Name: Anonymous 2008-10-21 23:52

int bytes_allocated = 0;
char *ptr = NULL;
if(ptr = (char *)malloc(100)) bytes_allocated = 100;
...
if(ptr = realloc(ptr, 200)) bytes_allocated = 200;

Name: Anonymous 2008-10-22 1:37

>>2
malloc returns void* on standard-conforming implementations, so you have to cast it to the appropriate type.

Name: Anonymous 2008-10-22 2:12

>>4
malloc returns void *on standard-conforming implementations so you don't have to cast it to the appropriate type.

Name: Anonymous 2008-10-22 2:13

>>5
Why don't you turn on -Wall and try again.

Name: Anonymous 2008-10-22 4:03

>>4
>>5
>>6
This is one of those things that's different between C and C++, isn't it?

Name: Anonymous 2008-10-22 6:20

Strongly typed is the way to go.

Name: Anonymous 2008-10-22 7:04

>>8
what's so great about strangely typed?

Name: Anonymous 2008-10-22 7:58

>>9
HASKELL is.

Name: Anonymous 2008-10-22 8:22

>>1

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: Anonymous 2008-10-22 8:27

GUYS I JUST DISCOVerED SOMETHING THATS BEEN AROUND FOR 30 YEARS ITS SOOOOOOOOOO COOOOOOOOOL EVERYONE SHOULD USE $CURRENT_POPULAR_LANGUAGE!!!!!

Name: Anonymous 2008-10-22 8:32

currentPopularLanguage = "Haskell"

Name: Anonymous 2008-10-22 8:46

>>13 case sensitive

Name: Anonymous 2008-10-22 11:49

>>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: Anonymous 2008-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: Anonymous 2008-10-22 12:11

>>15
>because all pointers take the same amount of memory

Not necessarily.

#include <iostream>

using namespace std;

class A {
public:
    int foo()
    {
        return 1;
    }
};

int bar()
{
    return 2;
}

int main()
{
    cout << "sizeof(&A::foo) = " << sizeof(&A::foo) << "\n";
    cout << "sizeof(&bar) = " << sizeof(&bar) << "\n";
    return 0;
}

Name: Anonymous 2008-10-22 12:16

>>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: Anonymous 2008-10-22 12:37

what the fuck

Name: Anonymous 2008-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: Anonymous 2008-10-22 12:57

>>18
Suck on your mom's dick instead of on obsolete books for a change.

Name: Anonymous 2008-10-22 13:22

>>21
Is that even relevant to the topic or just your way of saying "Your mothers got a penis"

Name: Anonymous 2008-10-22 13:43

reinterpret_cast<>() is a beautiful construct.

Name: Anonymous 2008-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: Anonymous 2008-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: Anonymous 2008-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: Anonymous 2008-10-22 14:55

>>26
The idea of living a standard-conforming life is highly overrated in today's society.

Name: Anonymous 2008-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: Anonymous 2008-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.

Name: Anonymous 2008-10-22 15:45

>>29 char * ptr = &x[0];
Enjoy your disaster waiting to happen.

Name: Anonymous 2008-10-22 16:35

>>30
I will, along with my BUFFA OVERFLOWed anus.

Name: Anonymous 2008-10-22 16:42

>>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: Anonymous 2008-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.

Name: Anonymous 2008-10-22 17:39

>>33
UNDEFINED BEHAVIOR.

Name: Anonymous 2008-10-22 18:22

>>6
Why don't you turn on -xc -std=c89 -pedantic -Wall -W -Wno-missing-field-initializers -Wundef -Wendif-labels -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wdisabled-optimization -O2 -pipe -march=native and try again.

Name: Anonymous 2008-10-22 19:51

>>35
Whoops, forgot my -funroll-loops

Name: Anonymous 2008-10-22 19:54

>>4,6,18

~$ touch foo.c
~$ echo "#include <stdio.h>" > foo.c
~$ echo "#include <stdlib.h>" >> foo.c
~$ echo "int main(){" >> foo.c
~$ echo "char * hello;" >> foo.c
~$ echo "hello = malloc(sizeof(char) * 100);" >> foo.c
~$ echo "return 0;}" >> foo.c
~$ echo "" >> foo.c
~$ gcc foo.c -ansi -Wall -o foo&&./foo
~$


>>2
size_t

Name: Anonymous 2008-10-22 20:22

>>37
Better to use ssize_t just in case memory ever gets that large.

Name: Anonymous 2008-10-23 0:44

>>37
man cat

Name: Anonymous 2008-10-23 2:21

>>37
man vim

Name: Anonymous 2008-10-23 3:49

>>37,39
man gcc

Name: Anonymous 2008-10-23 4:01

Better to use ssize_t just in case memory ever gets that large.
no.
http://www.opengroup.org/onlinepubs/000095399/basedefs/sys/types.h.html
size_t
    Used for sizes of objects.
ssize_t
    Used for a count of bytes or an error indication.

size_t is the correct type to use in this case, since it won't be used to store an error indication.
also, malloc() and realloc() take size_t arguments, not ssize_t:
http://www.opengroup.org/onlinepubs/000095399/functions/malloc.html
http://www.opengroup.org/onlinepubs/000095399/functions/realloc.html

>>39-40
man ed

Name: Anonymous 2008-10-23 5:49

>>30
WHich is different from malloc() how exactly?  Apart from vector<> giving you automatic clean-up, as well as the same contiguity guarantee.

Name: Anonymous 2008-10-23 16:20

>>40,42
man emacs

Name: Anonymous 2008-10-23 16:59

>>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: Anonymous 2008-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: Anonymous 2008-10-23 19:39

>>46
In C++, yes. Show me some C code that demonstrates pointers of different sizes, and I'll believe you.

Name: Anonymous 2008-10-23 20:13

>>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: Anonymous 2008-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.

Name: Anonymous 2008-10-23 21:24

Sepples strives to have a stronger type system than C
http://dis.4chan.org/read/prog/1223722258

Name: Anonymous 2008-10-23 21:40

>>21
Enjoy your Sepplesox, you raging slow-ass-fucking faggot.

Name: Anonymous 2008-10-23 21:44

>>1
Using Char is a syntax error.

Name: Anonymous 2008-10-23 23:09

>>52
Or a type mismatch.

Name: Sigmund F. 2008-10-24 1:30

>>53
Or a nasty case of class envy.

Name: Anonymous 2008-10-24 10:28

>>54
Well you know, sometimes an instance is just an instance.

Name: Anonymous 2009-07-21 2:04

-wundef.

Name: Anonymous 2010-12-06 9:32

Back to /b/, ``GNAA Faggot''

Don't change these.
Name: Email:
Entire Thread Thread List