Pointer to a Pointer to a struct

R

Rodrick Brown

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

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));
strncpy(p[0]->test[0],"Hello World",20);
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.
--
Unix Systems Engineer
The City of New York
Dept. of Information Technology
http://www.nyc.gov/doitt
rbrown[(@)]doitt.nyc.gov
http://www.rodrickbrown.com
 
J

Joona I Palaste

Rodrick Brown said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main(void)
{
typedef struct
{
char **test;
}Testing;
Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));

You haven't allocated any memory for p[0], let alone for p[0]->test.
strncpy(p[0]->test[0],"Hello World",20);

And anyway, sizeof(Testing) bytes might very well be too little to
store "Hello World".
printf("%s\n",p[0]->test[0]);
return EXIT_SUCCESS;
}
Why does the following crash ?
Not sure what i'm missing please advise thanks.

I think the fault is that you don't properly understand double
pointers.
 
M

Michael Mair

Joona said:
Rodrick Brown said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));


You haven't allocated any memory for p[0], let alone for p[0]->test.

strncpy(p[0]->test[0],"Hello World",20);


And anyway, sizeof(Testing) bytes might very well be too little to
store "Hello World".

printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.


I think the fault is that you don't properly understand double
pointers.

Joona I Palaste means "pointers to pointers" as in ** and not
as in double *. Just to clarify.

Read section 6 of the C FAQ (on arrays and pointers):
http://www.eskimo.com/~scs/C-faq/s6.html

Cheers
Michael
 
M

Mark F. Haigh

Michael said:
Joona I Palaste wrote:



Joona I Palaste means "pointers to pointers" as in ** and not
as in double *. Just to clarify.
<snip>

To nitpick, I think Joona meant "double indirection". Not that it
really matters.


Mark F. Haigh
(e-mail address removed)
 
J

jacob navia

Joona said:
Rodrick Brown said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));


You haven't allocated any memory for p[0], let alone for p[0]->test.

The "testing" structure contains only one member:
a pointer that points to a pointer to characters.


The size of a pointer is (assumed) 4 (32 bit system).

Then, your structure is just 4 bytes long.

Another thing would be:
typedef struct { char Test[256]; }Testing;

*THEN* you would be able to do your copy
I think the fault is that you don't properly understand double
pointers.

Exactly
 
A

Al Bowers

Rodrick said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));

p would typically point to an array of Testing pointers. In
this case the array is one,p[0]. You next need to allocate
storage for p[0]. You will need to go through the same
routine with the member, char **test.

See the corrections below.
p[0]->test[0] = malloc(sizeof(Testing));
strncpy(p[0]->test[0],"Hello World",20);
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.

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

typedef struct
{
char **test;
}Testing;

int main(void)
{
Testing **p;

if((p = malloc(sizeof*p)) == NULL) exit(EXIT_FAILURE);
if((p[0] = malloc(sizeof *p[0])) == NULL)
{
free(p);
exit(EXIT_FAILURE);
}
if((p[0]->test = malloc(sizeof *p[0]->test)) == NULL)
{
free(p[0]);
free(p);
exit(EXIT_FAILURE);
}
if((p[0]->test[0] = malloc(20)) == NULL)
{
free(p[0]->test);
free(p[0]);
free(p);
exit(EXIT_FAILURE);
}
strncpy(p[0]->test[0],"Hello World",20);
printf("p[0]->test[0] = \"%s\"\n",p[0]->test[0]);
free(p[0]->test[0]);
free(p[0]->test);
free(p[0]);
free(p);
return EXIT_SUCCESS;
}
 
B

Barry Schwarz

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

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));

You have allocated the wrong amount of space. The object that p
points to must be a pointer to Testing (Testing*). You have allocated
space large enough to hold a Testing, not a Testing*. It is exactly
this problem that has led to the oft repeated recommendation to use
code of the form
p = malloc(sizeof *p);
p[0]->test[0] = malloc(sizeof(Testing));

p[0] is the first object that p points to. It is of type Testing*
which is why your syntax is correct. However, p[0] has never been
initialized to point anywhere. You need an object of type Testing
that p[0] can point to, either by definition or allocation.

After p[0] points to such an object, you must then initialize the
member test to point to some char* so that you can initialize that
char* with the allocated address.

Why would you have a char* point to an object of this size.
strncpy(p[0]->test[0],"Hello World",20);

This will obviously only copy 12 characters but will then attempt to
set the following 8 to '\0'. Unfortunately, it is very unlikely that
test[0] points to an area of memory large enough to hold these 20
characters. Chances are size of testing is 4. This would invoke
undefined behavior.
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.

At several points, you are attempting to dereference uninitialized
pointers. You also overflow memory you allocate.


<<Remove the del for email>>
 
J

John Bode

Rodrick Brown said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));

You have a mismatch here. You're allocating memory for an object of
type Testing, but assigning the resulting pointer to an object of type
pointer to pointer to Testing.

type of p == Testing **, value == return value of malloc()
type of p[0] == Testing *, value == indeterminate
p[0]->test[0] = malloc(sizeof(Testing));

You've missed a step in your allocation -- p points somewhere
meaningful after the first malloc() (even with the mismatch), but p[0]
does not, so you're attempting to dereference an invalid pointer.
This is why you are crashing.
strncpy(p[0]->test[0],"Hello World",20);

Same thing here. You've skipped an allocation step (although you've
already crashed by now anyway; besides, you likely haven't allocated
enough memory to hold the string if you're going by the size of the
struct).
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.

You're doing double indirection, so you have to do double allocation:

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

int main (void)
{
typedef struct {
char **test;
} Testing;

Testing **p;

/* type of p[0] == Testing * */
p = malloc(sizeof p[0]);
if (!p)
return 1;

/* type of *p[0] == Testing */
p[0] = malloc(sizeof *p[0]);
if (!p[0])
return 1;

/* type of p[0]->test[0] == char * */
p[0]->test = malloc(sizeof p[0]->test[0]);
if (!p[0]->test)
return 1;

/* type of *p[0]->test[0] == char */
p[0]->test[0] = malloc (sizeof *p[0]->test[0] * 20);
if (!p[0]->test[0])
return 1;

strcpy (p[0]->test[0], "Hello World");

printf ("%s\n", p[0]->test[0]);

free(p[0]->test[0]);
free(p[0]->test);
free(p[0]);
free(p);

return 0;
}
 
A

aditya

Rodrick Brown said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(void)
{
typedef struct
{
char **test;
}Testing;

Testing **p;
p = malloc(sizeof(Testing));
p[0]->test[0] = malloc(sizeof(Testing));
strncpy(p[0]->test[0],"Hello World",20);
printf("%s\n",p[0]->test[0]);

return EXIT_SUCCESS;
}

Why does the following crash ?
Not sure what i'm missing please advise thanks.

strncpy(p[0]->test[0],"Hello World",20);

you have not allocated sufficent amount of memory to the test.

Therefore,when you'll copy the string "hello world" to test,

you will be overwriting the address of the main(void) function.

As a result,the program will crash as soon as return statement will
be executed.

Aditya
 

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

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top