473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer Allocation

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?

Nov 15 '05 #1
5 1554

Pacher R. Dragos wrote:
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[i]=buff;

}

will serve the purpose

Nov 15 '05 #2
> 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[i]=buff;

}

will serve the purpose


You didn't understand me:
for *type[counter]=buf[i] 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[i] = 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 */

Nov 15 '05 #3
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.

Nov 15 '05 #4

vire wrote:
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

Nov 15 '05 #5
On 26 Oct 2005 00:02:42 -0700, "Pacher R. Dragos"
<re**********@gmail.com> wrote:
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>>
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
25
by: MC | last post by:
Hi I have the following piece of code. int *p; p = malloc(10); int *j; j = p; free(p); Is this legal ? will j still point to the same
6
by: Rodrick Brown | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { typedef struct { char **test;
21
by: Roman Werpachowski | last post by:
I can't understand this: double * x = new double; const double * p = x; delete p; works. Why am I allowed to delete a const pointer? On the same note: why am I allowed to do this: class...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
7
by: morz | last post by:
i just search for a while in c++ groups but cannot find good answer. i have code like this: int **ptr; ptr = new char *; ptr = new int(5); ptr = new int(16);
17
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1,...
3
by: aeo3 | last post by:
Hi All, Now, I am trying to build a project, I need to expand an array of pointer to classes. Moreover, this array includes some elements I want to delete them. So, I create another array, copy the...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.