invalid address alignment
Author |
Message |
uzmargo #1 / 6
|
 invalid address alignment
Greetings, Can some one explain what BUS error: "invalid address alignment means". I am getting a core dump because of this error. Thank you.
|
Sat, 08 Nov 2003 05:59:42 GMT |
|
 |
Arthur H. Gol #2 / 6
|
 invalid address alignment
Quote:
> Greetings, > Can some one explain what BUS error: "invalid address alignment means". I > am getting a core dump because of this error.
On many platforms, alignment matters. For example, if we assume 4-byte longs and 8-byte doubles, it is required that longs begin at at address divisible by 4 and doubles at an address divisible by 8. It has to do with the way that loads and stores are done on a particular processor. In most cases, however, compilers, knowing the architecture of the machine you're compiling for, won't let this happen; they assure that data is aligned correctly. It is most likely that you have a bug in your code. Likely causes: Trying to store data via an uninitialized pointer (or one that has not had memory allocated to it) Using _really_ funky casts... things like that. Why not post a minimal, compilable snippet of code that displays the problem? It will make diagnosis much much easier! HTH, --ag -- Artie Gold, Austin, TX (finger the cs.utexas.edu account for more info)
-- I am looking for work. Contact me.
|
Sat, 08 Nov 2003 07:03:51 GMT |
|
 |
Mark McIntyr #3 / 6
|
 invalid address alignment
Quote: >Greetings, >Can some one explain what BUS error: "invalid address alignment means". I >am getting a core dump because of this error.
it means you have a bug in your program - most likely you're trying to read to or write from memory htat you don't own. Look for uninitialised variables, esp pointers, and for walking off the ends of arrays. -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
|
Sat, 08 Nov 2003 07:01:34 GMT |
|
 |
Eric Sosma #4 / 6
|
 invalid address alignment
Quote:
> Greetings, > Can some one explain what BUS error: "invalid address alignment means". I > am getting a core dump because of this error.
This is Question 16.8 in the comp.lang.c Frequently Asked Questions (FAQ) list http://www.eskimo.com/~scs/C-faq/top.html --
|
Sat, 08 Nov 2003 21:14:55 GMT |
|
 |
uzmargo #5 / 6
|
 invalid address alignment
Thank you for responses. here is the code snippet I have a problem with: while (mr != NULL) { if (mr->pd != NULL) { if (keyword_exist("made for TV",mr->kd) == 1){ if (mr->pd->origin != NULL) { free(mr->pd->origin); } mr->pd->origin = (char *) calloc(40, sizeof(char)); strcpy(mr->pd->origin,"Made for TV"); } else if (keyword_exist("made for cable",mr->kd) == 1) { if (mr->pd->origin != NULL) { free(mr->pd->origin); } mr->pd->origin = (char *) calloc(40, sizeof(char)); strcpy(mr->pd->origin,"Made for Cable"); } else if (keyword_exist("direct to video",mr->kd) == 1) { if (mr->pd->origin != NULL) { free(mr->pd->origin); } mr->pd->origin = (char *) calloc(40, sizeof(char)); strcpy(mr->pd->origin,"Direct to Video"); } else /* Always fails here when trying to diallocate mr->pd->origin */ /* Note, that it does not fail the first time it hits the statement, */ /* but rather after some time */ if (mr->pd->origin != NULL) { free(mr->pd->origin); } mr->pd->origin = (char *) calloc(40, sizeof(char)); strcpy(mr->pd->origin,""); } mr = mr->next; Quote: }
/**************************************************************** Searches a list of keywords looking for a match to kword keyword_exist(+kword, +kd) Return : True/False ****************************************************************/ int keyword_exist(char *kword, keyword_data *kd) { while (kd != NULL) { if (strcmp(kword,kd->name) == 0) return(1); else kd = kd->next; } return(0); Quote: }
****************************************************************/ Quote:
>> Greetings, >> Can some one explain what BUS error: "invalid address alignment means". I >> am getting a core dump because of this error. >On many platforms, alignment matters. For example, if we assume 4-byte >longs and 8-byte doubles, it is required that longs begin at at address >divisible by 4 and doubles at an address divisible by 8. It has to do >with the way that loads and stores are done on a particular processor. >In most cases, however, compilers, knowing the architecture of the >machine you're compiling for, won't let this happen; they assure that >data is aligned correctly. >It is most likely that you have a bug in your code. >Likely causes: >Trying to store data via an uninitialized pointer (or one that has not >had memory allocated to it) >Using _really_ funky casts... >things like that. >Why not post a minimal, compilable snippet of code that displays the >problem? It will make diagnosis much much easier! >HTH, >--ag >-- >Artie Gold, Austin, TX (finger the cs.utexas.edu account for more info)
>-- >I am looking for work. Contact me.
|
Sat, 08 Nov 2003 22:24:13 GMT |
|
 |
Chris Tor #6 / 6
|
 invalid address alignment
Quote:
>here is the code snippet I have a problem with: >while (mr != NULL) { > if (mr->pd != NULL) { > if (keyword_exist("made for TV",mr->kd) == 1){ > if (mr->pd->origin != NULL) { > free(mr->pd->origin); > } > mr->pd->origin = (char *) calloc(40, sizeof(char)); > strcpy(mr->pd->origin,"Made for TV"); > } > else if (keyword_exist("made for cable",mr->kd) == 1) { > if (mr->pd->origin != NULL) { > free(mr->pd->origin); > } > mr->pd->origin = (char *) calloc(40, sizeof(char)); > strcpy(mr->pd->origin,"Made for Cable"); > } > else if (keyword_exist("direct to video",mr->kd) == 1) { > if (mr->pd->origin != NULL) { > free(mr->pd->origin); > } > mr->pd->origin = (char *) calloc(40, sizeof(char)); > strcpy(mr->pd->origin,"Direct to Video"); > } > else
There is an open "{" missing here, which tells me that the code you just posted is not actually the code you are running -- this code would not compile, due to the missing "{". Still, assuming that is the only change introduced, let us add it back: { Quote: > /* Always fails here when trying to diallocate mr->pd->origin */ > /* Note, that it does not fail the first time it hits the >statement, */ > /* but rather after some time */ > if (mr->pd->origin != NULL) { > free(mr->pd->origin); > } > mr->pd->origin = (char *) calloc(40, sizeof(char)); > strcpy(mr->pd->origin,""); > } > mr = mr->next; >}
The casts on the calloc() calls are suspicious -- calloc() already returns a valid type that never needs casting -- but are unlikely to be the source of the problem. The lack of a test on the result of each calloc() is more likely to be the source of the problem. If your system runs out of memory "after some time", one of the calloc() calls might return NULL, and then a subsequent strcpy() could scramble something. The most likely problem, however, is somewhere else entirely. Nonetheless, the above code could be considerably simplified. Consider this replacement: some_type *pd; keyword_data *kd; char *newtext; for (mr = head; mr != NULL; mr = mr->next) { pd = mr->pd; if (pd == NULL) /* what does a NULL "pd" mean anyway? */ continue; kd = mr->kd; if (keyword_exist("made for TV"), kd) newtext = "Made for TV"; else if (keyword_exist("made for cable", kd)) newtext = "Made for Cable"; else if (keyword_exist("direct to video", kd)) newtext = "Direct to Video"; else newtext = ""; if (pd->origin != NULL) free(pd->origin); /* * There is no obvious reason to use exactly 40 bytes per string * (for instance, you could malloc strlen(newtext)+1 bytes), but * I have retained this behavior anyway. */ pd->origin = malloc(40); if (pd->origin == NULL) panic("failed to allocate 40 bytes"); assert(strlen(newtext) < 40); strcpy(pd->origin, newtext); } Quote: >int keyword_exist(char *kword, keyword_data *kd) >{ > while (kd != NULL) { if (strcmp(kword,kd->name) == 0) return(1); else kd >= kd->next; } > return(0); >}
I would rewrite this loop too, e.g.: int keyword_exist(/*const*/ char *kword, keyword_data *kd) { for (; kd != NULL; kd = kd->next) if (strcmp(kword, kd->name) == 0) return 1; return 0; } or perhaps even: int keyword_exist(/*const*/ char *kword, keyword_data *kd) { for (; kd != NULL && strcmp(kword, kd->name) != 0; kd = kd->next) continue; return kd != NULL; } although none of this affects the original loop (the one that uses calloc()). -- In-Real-Life: Chris Torek, Wind River Systems
|
Sun, 09 Nov 2003 04:28:35 GMT |
|
|
|