LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-30-2006, 02:46 PM   #1
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Rep: Reputation: 15
Unhappy a quetion about MALLOC


I have a problem with malloc segfaulting with too many access. so I said give me a heap. I allocated

char * helpstr[81920];i
this is an array of pointers..


heapstr=malloc(1,81920*80);


ignoring the pointer problem I pass in a length and increment my heapstr.

return((char *)&heapstr[input_size + current_ptr]


since this was allocated at topmost level --
I never return from this routine.


but the mallock -> xxxx4228.

myalloc (0) -> xxxx4228.

myalloc (80) - xxxx4232.


infact It seems that the compiler is playing fast and loose with stack. an array is suposed to calculate x+k*value ->> x = 4228
k=80 , value =1
4228+80 = 4304 not 4232.

and the pointers I try to stuff in vars are NOT AVAILABLE.


1. malloc seems to have a limit of some 500 or some simliar number of accesses. It does not have a problem with a VERY LARGE block.


2. running my program
the pointers are fuzzy and do not work correctly it looks to me like I have a default pointer -> x and when I say &x it does not give me the address of x but the address of the default pointer.



so my question is how do I unravel my problem with pointers and malloc. all I want is to allocat 2500 strings. a heap is a choice and should work. but the problem is the stack and No I cannot use malloc to do this it segfaults with out cause.

josephus
 
Old 07-30-2006, 04:59 PM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Quote:
heapstr=malloc(1,81920*80);
Check the man pages for malloc.
malloc takes only one argument:

Code:
some_pointer = malloc(number_of_bytes_to_allocate);
Quote:
ignoring the pointer problem I pass in a length and increment my heapstr.

return((char *)&heapstr[input_size + current_ptr]


since this was allocated at topmost level --
I never return from this routine.


but the mallock -> xxxx4228.

myalloc (0) -> xxxx4228.

myalloc (80) - xxxx4232.
What pointer problem? You never return from what routine? The few lines of code that you have posted are without context. Could you post the entire function that you are having problems with?

Quote:
1. malloc seems to have a limit of some 500 or some simliar number of accesses. It does not have a problem with a VERY LARGE block.


2. running my program
the pointers are fuzzy and do not work correctly it looks to me like I have a default pointer -> x and when I say &x it does not give me the address of x but the address of the default pointer.



so my question is how do I unravel my problem with pointers and malloc. all I want is to allocat 2500 strings. a heap is a choice and should work. but the problem is the stack and No I cannot use malloc to do this it segfaults with out cause.
1. malloc has no such limit, though you may (obviously) run out of memory to allocate.

2. What do you mean by fuzzy pointers? A default pointer? If you say &x, the address of x will be returned.

You can use malloc to allocate memory; that is what the function is for. Nothing segfaults without cause.

Last edited by zhangmaike; 07-30-2006 at 05:03 PM.
 
Old 07-30-2006, 05:04 PM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by dogbird
char * helpstr[81920];i
this is an array of pointers..


heapstr=malloc(1,81920*80);


ignoring the pointer problem I pass in a length and increment my heapstr.
You CANNOT assign something to an array. This makes absolutely no sense.

What are you trying to allocate? You either have an array of pointers and allocate a chunk of memory for each of them separately; or you have an array of arrays and you allocate a big chunk of memory for it (which would be declared differently anyway).

Also, use CODE tags.
 
Old 07-30-2006, 10:16 PM   #4
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
I call a routine called parse.
I never return from this call.

ok, why can I allocate 81920 but not 80*1500
what I see is heapstr = malloc(81920);

char * mymall( size} {

currentptr = nextptr;
nexptr = nextptr+size;
printf(" %d \n",&heapstr[currentptr+size]);
819284224
return(char *)&heapstr[currentptr+size] ;



later ...
yytext varies from 1 to 80 characters.

data[1] = (char *)myalloc(sizeof(yytext));
printf(" %d \n",&data[j])
819284228




819284232

notice that the pointer only increases by 4 this cannot be the string I point to.

also the history j-1 is lost.


maybe I am doing something wrong but the code incriments by 4 regardless what value I pass to the routine.

so obvously the array reference is not working correctly

note: * in[*] is not working in this version of GCC.
this problem maybe related in some way

josephus

Its not what you know that gets you in trouble, its what you know that aint so.
 
Old 07-30-2006, 11:27 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I still don't understand what you are saying. Can you show us actual code? You name things all differently and it's confusing.

* heapstr is an array of pointers. Why? What do those pointers do?
* You are trying to assign something to it. Why? You can't assign something to an array.
* Maybe you wanted heapstr to just be a pointer? Or something?
*
Code:
&heapstr[currentptr+size] ;
This is a pointer to some element of heapstr. As currently declared it would be char** (pointer to pointer). What good is this?
 
Old 07-31-2006, 01:42 AM   #6
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
that was the actual code

char *heapstr[81920];



heapstr = malloc(819280);
81924224


later I try to put a string into this heap.
for i = 1; i<563 ; i++) {
and save the pointer
temp = yylex();
data[i]= mymalloc(80);
data[0]= yytext;
:


all I am doing is saving strings.

if I use calloca and malloc I get a segfault. It varies from place to place depending on how much I allocate for other things.


I have tons of virtual memory.

It is not clear that I cannot write into that array which is a pointer heap.

josephus
 
Old 07-31-2006, 02:03 AM   #7
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I still don't understand. But I think perhaps you want this:
Code:
char *heapstr = malloc(81920);
which allocates 81920 char's
 
Old 08-05-2006, 01:33 AM   #8
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
I added [81920][80] and the code stopped failing.


now I have a string with "0 1 eof ACC 0 0"

it is in data.

p (char *)data
0 1 eof ACC 0 0


fscanf((char *)data," %d %d %s %s %d %d", &state,&id,(char *)who,(char *)what,&x1,&x2);

%400049648d6 --uflow () in lib.so


I really do not understand this error -- it should read six terms 2 digits, 2 strings, 2 more digits. As far as I can guess, this is correct.

josephus
 
Old 08-05-2006, 12:38 PM   #9
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
fscanf reads from a file pointer (FILE*), hence the f in the name. You want sscanf, which reads from a string (char*).

See man sscanf.
 
Old 08-05-2006, 06:40 PM   #10
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
my mistake. it confuses me from time to time. I understand now. It is trying to remember all the system calls. You cant look anything up unless you KNOW ITS NAME.
josephus
 
Old 08-05-2006, 11:11 PM   #11
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Quote:
You cant look anything up unless you KNOW ITS NAME.
Using the -K option with man will let you search manpages by keyword rather than by name (but this takes a long time).

The apropos command will search the whatis database for keywords (faster, but less thorough).

Combining either of the above commands with grep can be useful in finding commands or functions if you do not yet know their names.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Server quetion 2 mattar31 Linux - Newbie 2 07-23-2006 04:05 AM
Guarddog quetion jadukor Slackware 6 03-15-2006 08:18 AM
metamodes quetion amadkow Linux - Software 0 01-29-2005 04:46 PM
fetchmail quetion primal-id Linux - Software 1 05-10-2003 02:37 PM
Another Sound Quetion Neo22589 Linux - Software 0 03-22-2003 12:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration