Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: [Win32] pthreads and memory allocation

by Marshall (Abbot)
on Nov 22, 2011 at 06:20 UTC ( #939374=note: print w/replies, xml ) Need Help??

in reply to [Win32] pthreads and memory allocation

I am having trouble understanding even the basics of this C code.
TaskCode() returns a pointer to anything and accepts a pointer to anything.

There is nothing returned from this C subroutine,
void *TaskCode(void *argument).

tid = *((int *) argument);
means: argument is "cast" as a pointer to integer.
Then that result is de-referenced to get the "tid". A **int.

The OP's code is a memory leak because there is no pointer or other way to use the memory allocated by
"s = (char*) malloc(100);".

void *TaskCode(void *argument) { int tid; char * s; tid = *((int *) argument); // tid = **argument; printf("thread %d: calling malloc\n", tid); s = (char*) malloc(100); printf("thread %d: malloc called\n", tid); return NULL; }
"s" is a memory pointer returned by malloc(). Now a malloc() call should have a conditional statement to see that it actually worked. But the big thing here is that the subroutine TaskCode() returns bullshit. I don't see how "s", the pointer to a new memory allocation of 100 bytes can ever be used outside of TaskCode()

Replies are listed 'Best First'.
Re^2: [Win32] pthreads and memory allocation
by syphilis (Chancellor) on Nov 22, 2011 at 07:22 UTC
    It's just a demo to illustrate that malloc() causes a crash on Win32 (but not elsewhere, afaik).
    Perhaps I should have declared char * useless_and_pointless; instead of char * s;.

    Now a malloc() call should have a conditional statement to see that it actually worked

    Yes, if you're wanting to make use of the memory that you've requested be allocated, I think that's a good idea. For the purposes of this demo, however, I don't really care whether the malloc succeeds or fails - I just want it to not cause a crash.
    (Besides, on Win32, I'm not even going to see the result of that "conditional statement" because it never gets executed - due to afore-mentioned crash.)

    Similarly, any memory leak is of no concern to me wrt this particular exercise.

      Perhaps I should have declared char * useless_and_pointless; instead of char * s;.

      Yes, I figure that would be correct and I figure that we understand each other.

      The basic issue is: "s = (char*) malloc(100);". "s" is cast as a pointer to a memory address. "s" is guaranteed to point to at least 100 bytes (consecutive).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://939374]
[ambrus]: Now it has five people instead of three, some sort of call number ticket system where people wait a lot for their food to get ready (it's the same kinds of grilled meat and fish on the same equipment, it won't actually fry slower),
[ambrus]: it's slow as hell, and the food costs significantly more.
[ambrus]: We no longer eat there.
LanX Everybody quotes it, some people read it, and a few people go by it
[ambrus]: LanX: why would I read it? isn't it a book for managers? I don't want to be a manager.
[ambrus]: I don't quote it either.
[erix]: it's really a software book, albeit about experiences from the 60s-80s. People haven't changed all that much though.
[erix]: a software project book, is probably more exact
[ambrus]: Yes, but I'm not managing software teams either.
[ambrus]: I'm not only not a restaurant manager, but also not a software manager.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2017-09-22 13:55 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (264 votes). Check out past polls.