cbuf with char *s

E

Eric Sosman

cerr said:
But if I declare a pointer globally its content should be transparent
to every thread in this process, no?

It's not at all clear what you mean by "transparent" (pun
intended, but serious). Some pointers are opaque, some are pale
translucent pearly pink (the greenish ones are very rare and
expensive), but I can't recall ever seeing a transparent pointer.

Perhaps the good people in the other forums TO WHICH YOU HAVE
ALREADY BEEN REFERRED are better able to do pointer spectrography.
[...]
Thanks for hints and suggestions!

Hint: Try asking on a different forum.

Suggestion: Try asking on a different forum.

Plea: Try asking on a different forum.
 
C

cerr

Okay.  But the post you just quoted pointed out specifically that you were
ignoring your allocated memory and just using the same pointers to everything,
which was the actual source of your problem.


Maybe.  It seems unlikely, though.


Okay.

Then my suggestion is this:  Separate this out into a test program which
doesn't use any threads or anything else, and just creates the data
structure, and then reads it to verify that you're getting the results you
want.  If you aren't, then you'll have a much smaller, self-contained,
program you can use to figure out what's going wrong.

Yep,
This works just fine:

#include <stdio.h>
#include "cbuf.h"
#define LogQ_SIZE 1024
volatile struct
{
unsigned int m_getIdx;
unsigned int m_putIdx;
char* m_entry[ LogQ_SIZE ];
} LogQ;

int main (void)
{
CBUF_Push(LogQ, "Test 1\n");
printf("%s",CBUF_Pop(LogQ));
return 0;
}
 
C

cerr

Okay.  But the post you just quoted pointed out specifically that you were
ignoring your allocated memory and just using the same pointers to everything,
which was the actual source of your problem.


Maybe.  It seems unlikely, though.


Okay.

Then my suggestion is this:  Separate this out into a test program which
doesn't use any threads or anything else, and just creates the data
structure, and then reads it to verify that you're getting the results you
want.  If you aren't, then you'll have a much smaller, self-contained,
program you can use to figure out what's going wrong.


Yup, I followed your suggestion and put this little app together which
works just fine:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "cbuf.h"
#define LogQ_SIZE 1024
volatile struct
{
unsigned int m_getIdx;
unsigned int m_putIdx;
char* m_entry[ LogQ_SIZE ];
} LogQ;
char **msglist;
char **temp;
int i=0;

int main (void)
{
char *buf="Test1";
temp = realloc(msglist,(CBUF_Len(LogQ)+1)*sizeof(*temp));
if (temp==NULL){
printf("Error reallocating memory for msglist\n");
for (i=CBUF_Len(LogQ);i>=0;i--)
free(msglist);
free(msglist);
return;
}
msglist=temp;
msglist[CBUF_Len(LogQ)] = malloc (strlen(buf)+1);
if (msglist[CBUF_Len(LogQ)]==NULL){
printf("Error allocating memory for the string in msglist\n");
return;
}
strcpy(msglist[CBUF_Len(LogQ)],buf);
CBUF_Push( LogQ, msglist[CBUF_Len(LogQ)] );
printf("push \"%s\" onto LogQ(%d)\n",buf,CBUF_Len(LogQ));
printf("%s",CBUF_Pop(LogQ));
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top