473,406 Members | 2,273 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,406 software developers and data experts.

2D array with different column types / Dumbass can figure out segfault

Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
#include <string.h>

void*** a;
void f();
typedef struct
{
char* name;
int idx;
} A_Type;

int main()
{
f();
}

void f()
{
int i;
a = malloc(sizeof(void**) * 2);
a[0] = malloc(sizeof(int) * 10);
a[1] = malloc(sizeof(A_Type*) * 10);

A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
tmp->name = "My Name";
tmp->idx = i;

memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
a[1][i] = tmp;
}
}

Sep 6 '06 #1
8 3188
no****@gmail.com wrote:
Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
#include <string.h>
//void*** a;
void **a;
void f();
typedef struct
{
char* name;
int idx;
} A_Type;

int main()
{
f();
}

void f()
{
int i;
//a = malloc(sizeof(void**) * 2);
a = malloc(sizeof(void*) * 2);
a[0] = malloc(sizeof(int) * 10);
a[1] = malloc(sizeof(A_Type*) * 10);

A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
tmp->name = "My Name";
tmp->idx = i;

memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
a[1][i] = tmp;
}
}
Konstantin
Sep 6 '06 #2
no****@gmail.com wrote:
Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
#include <string.h>
//void*** a;
void **a;
void f();
typedef struct
{
char* name;
int idx;
} A_Type;

int main()
{
f();
}

void f()
{
int i;
//a = malloc(sizeof(void**) * 2);
a = malloc(sizeof(void*) * 2);
a[0] = malloc(sizeof(int) * 10);
a[1] = malloc(sizeof(A_Type*) * 10);

A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
tmp->name = "My Name";
tmp->idx = i;
// memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
// you could do
// memcpy(&(a[0][i]), &i, sizeof(int));
// but why not simply:
a[0][i] = i;
a[1][i] = tmp;
}
}
Konstantin
Sep 6 '06 #3

Konstantin Miller wrote:
no****@gmail.com wrote:
Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
#include <string.h>
//void*** a;
void **a;
void f();
typedef struct
{
char* name;
int idx;
} A_Type;

int main()
{
f();
}

void f()
{
int i;
//a = malloc(sizeof(void**) * 2);
a = malloc(sizeof(void*) * 2);
a[0] = malloc(sizeof(int) * 10);
a[1] = malloc(sizeof(A_Type*) * 10);

A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
tmp->name = "My Name";
tmp->idx = i;
// memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
// you could do
// memcpy(&(a[0][i]), &i, sizeof(int));
// but why not simply:
a[0][i] = i;
a[1][i] = tmp;
}
}

Konstantin
Hi,

Thanks but as reasonable as that looks, its not working:

test.c:34: warning: dereferencing 'void *' pointer
test.c:34: error: invalid use of void expression
test.c:35: warning: dereferencing 'void *' pointer
test.c:35: error: invalid use of void expression

Lines 34 and 35 being:

a[0][i] = i;
a[1][i] = tmp;

Sep 6 '06 #4
no****@gmail.com wrote:
>
Konstantin Miller wrote:
>no****@gmail.com wrote:
Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
#include <string.h>
//void*** a;
void **a;
void f();
typedef struct
{
char* name;
int idx;
} A_Type;

int main()
{
f();
}

void f()
{
int i;
//a = malloc(sizeof(void**) * 2);
a = malloc(sizeof(void*) * 2);
a[0] = malloc(sizeof(int) * 10);
a[1] = malloc(sizeof(A_Type*) * 10);

A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
tmp->name = "My Name";
tmp->idx = i;
// memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
// you could do
// memcpy(&(a[0][i]), &i, sizeof(int));
// but why not simply:
a[0][i] = i;
a[1][i] = tmp;
}
}

Konstantin

Hi,

Thanks but as reasonable as that looks, its not working:

test.c:34: warning: dereferencing 'void *' pointer
test.c:34: error: invalid use of void expression
test.c:35: warning: dereferencing 'void *' pointer
test.c:35: error: invalid use of void expression

Lines 34 and 35 being:

a[0][i] = i;
a[1][i] = tmp;
Hi!

That's right, you can not dereference a void pointer!
Change those lines to

((int*)a[0])[i] = i;
((A_Type**)a[1])[i] = tmp;

This must work!

Konstantin
Sep 6 '06 #5
<no****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.
It is possible but ugly, difficult, and error-prone. What are you trying to
do?

#define NUMINTS 5
#define NUMDBLS 10

void ** a;
a = malloc (2 * sizeof *a);
a[0] = malloc (NUMINTS * sizeof (int));
a[1] = malloc (NUMDBLS * sizeof (double));

// now, to set an int:
((int *)a[0])[3] = 42;

// and to set a double:
((double *)a[1])[6] = 3.141592;

The reason is that a's type is pointer-to-pointer-to-<something>. You can't
give it a concrete type since it's different in different columns. But you
can't dereference a pointer-to-<somethingwithout knowing what the
<somethingis. So, at some point in the expression you have to cast the
pointer to a concrete pointer type in order to dereference it.

If you have a fixed list of types, and you want an array for each type, why
don't you do just that?

int *a = malloc (NUMINTS * sizeof *a);
double *b = malloc (NUMDBLS * sizeof *b);

Or, alternatively, you could declare a struct:

struct mystruct {
int *iarray;
double *darray;
};

struct mystuct a;
a.iarray = malloc (NUMINTS * sizeof *a.iarray);
a.darray = malloc (NUMINTS * sizeof *a.darray);

Philip

Sep 6 '06 #6

Konstantin Miller wrote:
no****@gmail.com wrote:

Konstantin Miller wrote:
no****@gmail.com wrote:

Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
#include <string.h>

//void*** a;
void **a;
void f();
typedef struct
{
char* name;
int idx;
} A_Type;

int main()
{
f();
}

void f()
{
int i;
//a = malloc(sizeof(void**) * 2);
a = malloc(sizeof(void*) * 2);
a[0] = malloc(sizeof(int) * 10);
a[1] = malloc(sizeof(A_Type*) * 10);

A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
tmp->name = "My Name";
tmp->idx = i;

// memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
// you could do
// memcpy(&(a[0][i]), &i, sizeof(int));
// but why not simply:
a[0][i] = i;
a[1][i] = tmp;
}
}

Konstantin
Hi,

Thanks but as reasonable as that looks, its not working:

test.c:34: warning: dereferencing 'void *' pointer
test.c:34: error: invalid use of void expression
test.c:35: warning: dereferencing 'void *' pointer
test.c:35: error: invalid use of void expression

Lines 34 and 35 being:

a[0][i] = i;
a[1][i] = tmp;

Hi!

That's right, you can not dereference a void pointer!
Change those lines to

((int*)a[0])[i] = i;
((A_Type**)a[1])[i] = tmp;

This must work!

Konstantin
Lovely stuff. Thanks for your patience and your help.

Sep 6 '06 #7
On 6 Sep 2006 03:26:45 -0700, no****@gmail.com wrote:
>Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.
snip code

It would be much simpler if you did it with an array of struct. Each
element of the array would be a row while each member of the struct
would be a column. You could have as many columns as you want, each
with an independent type.
Remove del for email
Sep 7 '06 #8
Groovy hepcat no****@gmail.com was jivin' on 6 Sep 2006 03:26:45 -0700
in comp.lang.c.
2D array with different column types / Dumbass can figure out
segfault's a cool scene! Dig it!
>Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
Well, you could use a union, I suppose. But it looks like what you
really need is a 1D array of struct. The first member of this struct
should be your int, and the second member your other struct. Something
like this:

#include <stdlib.h>

#define NUM 10

typedef struct
{
char* name;
int idx;
} A_Type;

struct allinfo
{
int foo;
A_Type bar;
};

int main(void)
{
struct allinfo a[NUM];
int i;

for(i = 0; i < NUM; i++)
{
a[i].foo = i;
a[i].bar.name = "My Name";
a[i].bar.idx = i;
}

return 0;
}
>following seg faults for me and I cant figure out what I need to
change.

Thanks.

#include <malloc.h>
No such header in standard C. What you need is this:

#include <stdlib.h>
>#include <string.h>

void*** a;
Why is this defined here, at file scope, instead of within a
function? There's no reason I can see to define this here. It would be
better defined in f().
>void f();
Prototypes have been around since 1989. And they're much better than
the alternative. There's no reason to use K&R style function
declarations anymore.

void f(void);
>typedef struct
{
char* name;
int idx;
} A_Type;

int main()
int main(void)
>{
f();
Why just have main() call f()? Seems rather pointless to me. It
would probably be better to put the guts of f() in main() instead. But
that's up to you.
You're not returning anything? You have an int function but you
don't return an int. That's not only not recommended, but actually
illegal in C90. Go on; return something. It's not hard.

return 0;
>}

void f()
void f(void)
>{
int i;
a = malloc(sizeof(void**) * 2);
a[0] = malloc(sizeof(int) * 10);
Wrong type. Since a is a void***, a[0] is a void**. *(a[0]) is,
therefore, a void*. But you are allocating enough space for 10 ints,
not 10 void*s.
a[1] = malloc(sizeof(A_Type*) * 10);
Wrong type. Since a is a void***, a[1] is a void**. *(a[1]) is,
therefore, a void*. But you are allocating enough space for 10
A_Type*s, not 10 void*s.
You have three malloc() calls here, none of which are checked for
failure. This is a very bad situation. malloc() can fail for unforseen
reasons; and when it does, you must take appropriate action to ensure
you don't dereference a null pointer.
Since the number of elements is known beforehand, why allocate
memory dynamically? An array would be better, I think.
A_Type* tmp;
for(i = 0; i < 10; i++)
{
tmp = malloc(sizeof(A_Type));
Another unchecked malloc() call. Bad!
tmp->name = "My Name";
tmp->idx = i;

memcpy(a[0][i], &i, sizeof(int)); /* SEG FAULT HERE */
a[1][i] = tmp;
}
}
Your use of void*** is flawed, I'm afraid. There are basically three
ways I can see around this: the way I have shown above, using another
struct; correcting what you are actually trying to do, using void* and
allocating a pointer to int or pointer to A_Type, as needed, and
assigning that to the void*; and using a union containing an int and
an A_Type, and simply using a 2D array of these. There may be other
ways I haven't thought of, but these should be enough. Of course, I
very much favour the first of those solutions. The other two are very
messy.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Sep 10 '06 #9

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

Similar topics

0
by: Ole Hansen | last post by:
Hi, Is it at all possible to insert BLOBs using the Array Interface? Today I have an application using the array interface. It works fine but so far I haven't been using BLOBs. I insert...
7
by: Yodai | last post by:
Hi all... I am trying to construct an 2x14 array that compares a given character with the 1st column of data type "t" and returns the second column's appropriate value "v". I've been trying this,...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
37
by: Richard Heathfield | last post by:
candide said: They aren't. An array is an array. An address is a pointer value. These are not the same thing. If you mean that &array and &array are the same, they aren't. They have different...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.