Pointer Allocation

P

Pacher R. Dragos

Consider the following code:

char *type[200];
char buf[1];
..............
read(handle, buf, 1);
*type[counter++]=buf[0];

And what I need is to put the buf into the type[counter] but it thrown
an cannot read memory exception..

What I would like to have in memory is:
type[0]="text1";
type[1]="text2";

I need some way of inline arithmetics to do it?
 
H

hotadvice

Pacher said:
Consider the following code:

char *type[200];
char buf[1];
..............
read(handle, buf, 1);
*type[counter++]=buf[0];

And what I need is to put the buf into the type[counter] but it thrown
an cannot read memory exception..

What I would like to have in memory is:
type[0]="text1";
type[1]="text2";

I need some way of inline arithmetics to do it?
type is an array of pointer to character arrays.
buff is a character array;

size of buff should be increased
e.g.
char buff[MAX_STRING_SIZE];

after this put this inside a loop

{
buff = update_string();
type=buff;

}

will serve the purpose
 
P

Pacher R. Dragos

char buff[MAX_STRING_SIZE];

If I do that i have to change hundrets of lines of code,
even if the code it's very modular

I intentionatelly do it like that!
after this put this inside a loop

{
buff = update_string();
type=buff;

}

will serve the purpose


You didn't understand me:
for *type[counter]=buf inside a loop
and inside another one I increase the counter

But I can't add succesive values to *type[0] so I need some kind of
pointer arithmetics:

Check for the word here:

while( i < 100 )
{
read(dhandle, tbuf, 1);
i++;
header = tbuf[0];
/*here!!!*/ //++(++(*type[indice])) = tbuf[0]; /*
atentie posibile erori obscure */
if (tbuf[0]=='#')
{
//printf("_found end_\n"); /* DEBUG ### */

/* new search for keywords */

int local = 0;
indice++;

while ( local < 200 )
{
read(dhandle, tbuf, 1);
if ( tbuf[0] == '~' )
break;
}

if ( /**/ == /**/ )
{ //printf("_keyword_start_"); /* DEBUG */
do
{
read(dhandle, tbuf, 1);
source[k] = tbuf[0];
k++;
}
while( /**/ != /**/ );

//printf("_curent_source_%s_", source); /* DEBUG
*/
/*source[k-2]='&';*/ /* tre sa fac ceva cu
bufferul ala prost */
 
V

vire

char* type[200];
yes , there are 200 pointers , but which address do they point to ?
so you have to figure out them before you read the memory.
 
P

Pacher R. Dragos

vire said:
char* type[200];
yes , there are 200 pointers , but which address do they point to ?
so you have to figure out them before you read the memory.

They just hold data like

char *pointer[2]={"text one", "another hot text", "bla bla"};

So i must fill them with my special searched words.

Maybe a malloc will work ? ?? hmmm
 
B

Barry Schwarz

Consider the following code:

char *type[200];
char buf[1];
..............
read(handle, buf, 1);
*type[counter++]=buf[0];

type is an array of 200 pointers, none of which point any where.

type[counter++] is one of those pointers which has not been
initialized to point somewhere.

*type[counter++] attempts to dereference this uninitialized pointer
which invokes undefined behavior.
And what I need is to put the buf into the type[counter] but it thrown
an cannot read memory exception..

What I would like to have in memory is:
type[0]="text1";
type[1]="text2";

I need some way of inline arithmetics to do it?

type[counter] = malloc(amount of storage necessary to hold string);
strcpy(type[counter++], buf); /* or maybe strncpy */


<<Remove the del for email>>
 

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
474,170
Messages
2,570,927
Members
47,469
Latest member
benny001

Latest Threads

Top